<p>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? </p>

<p>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? </p>

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

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

<hr>

<p><strong>Response roundup:</strong> 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 &lt;10ms up to 0.5 and 1 second per test, or just keeping the entire suite of commonly run tests under 10 seconds. </p>

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

[{'answer_id': 10491, 'author': 'Mike Stone', 'author_id': 122, 'author_profile': 'https://Stackoverflow.com/users/122', 'pm_score': 1, 'selected': False, 'text': "<p>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.</p>\n\n<p>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).</p>\n\n<p>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!</p>\n"}, {'answer_id': 10492, 'author': 'Lance Fisher', 'author_id': 571, 'author_profile': 'https://Stackoverflow.com/users/571', 'pm_score': 2, 'selected': False, 'text': "<p>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.</p>\n"}, {'answer_id': 10496, 'author': 'Andrew Grant', 'author_id': 1043, 'author_profile': 'https://Stackoverflow.com/users/1043', 'pm_score': 1, 'selected': False, 'text': "<p>We're currently at 270 tests in around 3.something seconds. There are probably around 8 tests that perform file IO.</p>\n\n<p>These are run automatically upon a successful build of our libraries on every engineers machine. We have more extensive (and time consuming) smoke-testing that is done by the build machine every night, or can be started manually on an engineers machine.</p>\n\n<p>As you can see we haven't yet reached the problem of tests being too time consuming. 10 seconds for me is the point where it starts to become intrusive, when we start to approach that it'll be something we'll take a look at. We'll likely move the lower level libraries, which are more robust since they change infrequently and have few dependencies, into the nightly builds, or a configuration where they're only executed by the build machine.</p>\n\n<p>If you find it's taking more than a few seconds to run a hundred or so tests you may need to examine what you are classifying as a unit test and whether it would be better treated as a smoke test.</p>\n\n<p>your mileage will obviously be highly variable depending on your area of development.</p>\n"}, {'answer_id': 10503, 'author': 'Dale Ragan', 'author_id': 1117, 'author_profile': 'https://Stackoverflow.com/users/1117', 'pm_score': 0, 'selected': False, 'text': '<p>I judge my unit tests on a per test basis, not by by # of tests per second.  The rate I aim for is 500ms or less.  If it is above that, I will look into the test to find out why it is taking so long.</p>\n\n<p>When I think a test is to slow, it usually means that it is doing too much.  Therefore, just refactoring the test by splitting it up into more tests usually does the trick.  The other times that I have noticed my tests running slow is when the test shows a bottleneck in my code, then a refactoring of the code is in order.</p>\n'}, {'answer_id': 10513, 'author': 'Jim', 'author_id': 1208, 'author_profile': 'https://Stackoverflow.com/users/1208', 'pm_score': 6, 'selected': True, 'text': "<p>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).  </p>\n\n<p>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.</p>\n\n<p>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.</p>\n"}, {'answer_id': 10718, 'author': 'Mark Harrison', 'author_id': 116, 'author_profile': 'https://Stackoverflow.com/users/116', 'pm_score': 1, 'selected': False, 'text': '<p>Data Point -- Python Regression Tests</p>\n\n<p>Here are the numbers on my laptop for running "make test" for Python 2.5.2:</p>\n\n<ul>\n<li>number of tests: 3851 (approx)</li>\n<li>execution time: 9 min, 6 sec</li>\n<li>execution rate: 7 tests / sec</li>\n</ul>\n'}, {'answer_id': 305153, 'author': 'philant', 'author_id': 18804, 'author_profile': 'https://Stackoverflow.com/users/18804', 'pm_score': 0, 'selected': False, 'text': '<blockquote>\n  <p>How long is too long for an individual\n  unit test?</p>\n</blockquote>\n\n<p>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 "<em>nothing broken, go on</em>" (or "<em>something broke, STOP</em>"). </p>\n\n<p>I do not bother about test execution speed until this is something that starts to get annoying. </p>\n\n<p>The danger is to stop running the tests because they\'re too slow. </p>\n\n<blockquote>\n  <p>Finally, when you do decide the tests\n  need to run faster, what techniques do\n  you use to speed up your tests?</p>\n</blockquote>\n\n<p>First thing to do is to manage to find out why they are too slow, and <strong>wether the issue is in the unit tests or in the code under test ?</strong> </p>\n\n<p>I\'d try to break the test suite into several logical  parts, running only the part that is  <em>supposedly</em> 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 <strong>before integrating</strong>.</p>\n'}, {'answer_id': 306063, 'author': 'Jeffrey Fredrick', 'author_id': 35894, 'author_profile': 'https://Stackoverflow.com/users/35894', 'pm_score': 3, 'selected': False, 'text': '<p>The goal is 100s of tests per second. The way you get there is by following Michael Feather\'s <a href="http://www.artima.com/weblogs/viewpost.jsp?thread=126923" rel="noreferrer">rules of unit tests</a>.</p>\n\n<p>An important point that came up in a past <a href="http://www.citconf.com/" rel="noreferrer">CITCON</a> discussion is that if your tests aren\'t this fast it is quite likely that you aren\'t getting the design benefits of unit testing.</p>\n'}, {'answer_id': 313692, 'author': 'Daniel Schierbeck', 'author_id': 20321, 'author_profile': 'https://Stackoverflow.com/users/20321', 'pm_score': 0, 'selected': False, 'text': "<p>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 <code>app/models/foo.rb</code>, the corresponding unit tests in <code>test/unit/foo_test.rb</code> get run.</p>\n\n<p>I don't know if anything similar exists for other platforms, but it would make sense.</p>\n"}, {'answer_id': 7453549, 'author': 'Hugo', 'author_id': 724176, 'author_profile': 'https://Stackoverflow.com/users/724176', 'pm_score': 0, 'selected': False, 'text': "<p>One of the most important rules about unit tests is they should run <strong><em>fast</em></strong>.</p>\n\n<blockquote>\n  <p>How long is too long for an individual unit test?</p>\n</blockquote>\n\n<p>Developers should be able to run the whole suite of unit tests in seconds, and definitely not in minutes and minutes. Developers should be able to quickly run them after changing the code in anyway. If it takes too long, they won't bother running them and you lose one of the main benefits of the tests. </p>\n\n<blockquote>\n  <p>What kind of execution rate do you aim for with your unit tests (# test per second)? </p>\n</blockquote>\n\n<p>You should aim for each test to run in an order of milliseconds, anything over 1 second is probably testing too much.</p>\n\n<p>We currently have about 800 tests that run in under 30 seconds, about 27 tests per second. This includes the time to launch the mobile emulator needed to run them. Most of them each take 0-5ms (if I remember correctly). </p>\n\n<p>We have one or two that take about 3 seconds, which are probably candidates for checking, but the important thing is the whole test suite doesn't take so long that it puts off developers running it, and doesn't significantly slow down our continuous integration build. </p>\n\n<p>We also have a configurable timeout limit set to 5 seconds -- anything taking longer will fail.</p>\n"}]
['https://Stackoverflow.com/questions/10486', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/906/']