Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit test execution speed (how many tests per second?)

Tags:

What kind of execution rate do you aim for with your unit tests (# test per second)? How long is too long for an individual unit test?

I'd be interested in knowing if people have any specific thresholds for determining whether their tests are too slow, or is it just when the friction of a long running test suite gets the better of you?

Finally, when you do decide the tests need to run faster, what techniques do you use to speed up your tests?

Note: integration tests are obviously a different matter again. We are strictly talking unit tests that need to be run as frequently as possible.


Response roundup: Thanks for the great responses so far. Most advice seems to be don't worry about the speed -- concentrate on quality and just selectively run them if they are too slow. Answers with specific numbers have included aiming for <10ms up to 0.5 and 1 second per test, or just keeping the entire suite of commonly run tests under 10 seconds.

Not sure whether it's right to mark one as an "accepted answer" when they're all helpful :)

like image 270
David Tchepak Avatar asked Aug 13 '08 23:08

David Tchepak


People also ask

How often should unit tests be executed?

Your answers confirm my general conclusion that running all unit tests after every local rebuild is the best practice regardless whether one is practicing TDD (test before) or classic unit testing (test after).

What is unit testing execution?

Unit testing is a software development process in which the smallest testable parts of an application, called units, are individually and independently scrutinized for proper operation. This testing methodology is done during the development process by the software developers and sometimes QA staff.

Should unit tests be fast?

Tests Should Be Fast That defeats the whole purpose of having a suite of unit tests in the first place, which is to boost the developers' confidence to make changes to the code. The tests can't work as the safety net they're supposed to be if they're not run often.

How much unit testing is enough?

It isn't realistic -- or necessary -- to expect 100% code coverage through unit tests. The unit tests you create depend on business needs and the application or applications' complexity. Aim for 95% or higher coverage with unit tests for new application code.


1 Answers

All unit tests should run in under a second (that is all unit tests combined should run in 1 second). Now I'm sure this has practical limits, but I've had a project with a 1000 tests that run this fast on a laptop. You'll really want this speed so your developers don't dread refactoring some core part of the model (i.e., Lemme go get some coffee while I run these tests...10 minutes later he comes back).

This requirement also forces you to design your application correctly. It means that your domain model is pure and contains zero references to any type of persistance (File I/O, Database, etc). Unit tests are all about testing those business relatonships.

Now that doesn't mean you ignore testing your database or persistence. But these issues are now isolated behind repositories that can be separately tested with integration tests that is located in a separate project. You run your unit tests constantly when writing domain code and then run your integration tests once on check in.

like image 186
Jim Avatar answered Sep 22 '22 11:09

Jim