Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you speed up java unit tests?

Currently our project has over 3000 unit tests, and "ant testAll" takes well over 20 minutes. besides getting better hardware, are there ways to speed things up?

like image 740
James Geng Avatar asked Jun 18 '09 06:06

James Geng


People also ask

How fast should unit tests run?

Still, it seems as though a 10 second short-term attention span is more or less hard-wired into the human brain. Thus, a unit test suite used for TDD should run in less than 10 seconds. If it's slower, you'll be less productive because you'll constantly lose focus.

What does JUnit test accelerate?

JUnit is a Regression Testing framework which is used to implement unit testing in Java, to accelerate programming speed and increase the quality of code.

Why is unit test slow?

Note the slowest test is due to it using Microsoft Moles to mock/isolate the file system. So with this example, if my unit test suite grows to 10,000 tests, it could take over 3 minutes to run.


1 Answers

The same way you'd speed up any other code. Find out which tests take the most time, and see how they can be optimized.

There are plenty of operations that can be slow, and if you do them 3000 times, it adds up. Sometimes, reusing data between tests is worthwhile (even if you're not supposed to do that in unit tests, it may be necessary if that's what it takes to get your tests to run at an acceptable speed).

Time your tests. Usually, 90% of them will execute almost instantly, and the last 10% will take almost all the time. Find those 10% and see what they're doing.

Run the code through a profiler, and note where the time is being spent. Guessing is a waste of time. What you think the test runner is doing is meaningless. Instead of guessing, find out what it is doing. Then you'll know how to speed it up.

like image 127
jalf Avatar answered Oct 09 '22 11:10

jalf