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

后端 未结 10 788
有刺的猬
有刺的猬 2021-02-01 05:31

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

相关标签:
10条回答
  • 2021-02-01 06:08

    Some frameworks provide automatic execution of specific unit tests based on heuristics such as last-modified time. For Ruby and Rails, AutoTest provides much faster and responsive execution of the tests -- when I save a Rails model app/models/foo.rb, the corresponding unit tests in test/unit/foo_test.rb get run.

    I don't know if anything similar exists for other platforms, but it would make sense.

    0 讨论(0)
  • 2021-02-01 06:09

    Data Point -- Python Regression Tests

    Here are the numbers on my laptop for running "make test" for Python 2.5.2:

    • number of tests: 3851 (approx)
    • execution time: 9 min, 6 sec
    • execution rate: 7 tests / sec
    0 讨论(0)
  • 2021-02-01 06:16

    I tend to focus more on readability of my tests than speed. However, I still try to make them reasonably fast. I think if they run on the order of milliseconds, you are fine. If they run a second or more per test... then you might be doing something that should be optimized.

    Slow tests only become a problem as the system matures and causes the build to take hours, at which point you are more likely running into an issue of a lot of kind of slow tests rather than one or 2 tests that you can optimize easily... thus you should probably pay attention RIGHT AWAY if you see lots of tests running hundreds of milliseconds each (or worse, seconds each), rather than wait till it gets to the hundreds of tests taking that long point (at which point it is going to be really hard to solve the problem).

    Even so, it will only reduce the time between when your automated build issues errors... which is ok if it is an hour later (or even a few hours later), I think. The problem is running them before you check in, but this can be avoided by selecting a small subset of tests to run that are related to what you are working on. Just make sure to fix the build if you check in code that breaks tests you didn't run!

    0 讨论(0)
  • 2021-02-01 06:16

    How long is too long for an individual unit test?

    I'd say it depends on the compile speed. One usually executes the tests at every compile. The objective of unit testing is not to slow down, but to bring a message "nothing broken, go on" (or "something broke, STOP").

    I do not bother about test execution speed until this is something that starts to get annoying.

    The danger is to stop running the tests because they're too slow.

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

    First thing to do is to manage to find out why they are too slow, and wether the issue is in the unit tests or in the code under test ?

    I'd try to break the test suite into several logical parts, running only the part that is supposedly affected by the code I changed at every compile. I'd run the other suites less often, perhaps once a day, or when in doubt I could have broken something, and at least before integrating.

    0 讨论(0)
  • 2021-02-01 06:21

    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.

    0 讨论(0)
  • 2021-02-01 06:21

    If we're talking strictly unit tests, I'd aim more for completeness than speed. If the run time starts to cause friction, separate the test into different project/classes etc., and only run the tests related to what you're working on. Let the Integration server run all the tests on checkin.

    0 讨论(0)
提交回复
热议问题