Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Is the execution time of a unit test useful for anything? Is a unit test an appropriate place for any sort of code profiling? Why or why not?

Sample Use Case:

  • Every unit test lists name, pass/fail, and execution time.
  • Time is also calculated per unit test class and total test suite execution time.

How could I use this info?

share|improve this question
    
See also: stackoverflow.com/questions/10486/… –  Hugo Sep 15 '11 at 21:35
    
Timing unit tests in ETL/DW is vital. However, I am almost sure this is not what you are asking about. –  Emmad Kareem Sep 15 '11 at 21:51
    
I actually don't even know what ETL/DW stand for. –  Curious Query Sep 15 '11 at 22:00
    
It's an acronym for extracting, loading, and transforming data for data warehouses (for business intelligence and reporting). –  Turnkey Sep 16 '11 at 2:06

6 Answers 6

up vote 10 down vote accepted

One of the most important rules about unit tests is they should run fast.

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. We currently have about 800 tests that run in around 30 seconds.

So if any tests do start taking too long, you can see which test is taking too long and do something about it.

Our unit test framework tells us how long it took for each test to run. Most of them take 0- 5ms (if I remember correctly), we have one or two that take about 3 seconds.

share|improve this answer
1  
Do your unit tests need to get data from the database? I find that usually slows them down a little bit beyond what you would consider acceptable. –  FrustratedWithFormsDesigner Sep 15 '11 at 21:40
1  
Also consider the use case where a developer uses the tests to debug a failure in the tests. They may have to run them repeatedly to fix their problem. Unit tests should make debugging problems easier not more cumbersome. –  Doug T. Sep 15 '11 at 21:47
1  
@FrustratedWithFormsDesigner We're lucky in that we don't use any databases on this project but I've read they are a kind of special case and can be treated as an external dependency, so you test the interactions with the database and not the data itself, especially as tests should be repeatable and not depend on any previous state. Here's some tips: osherove.com/blog/2004/6/19/… –  Hugo Sep 15 '11 at 21:50
    
@Doug usually a failure affects one or several tests, not the entire suite. you shouldn't need to run the entire suite to debug a specific test –  Eran Galperin Sep 15 '11 at 22:31
1  
With the exception of unit tests which involve the database, if an individual unit test takes more than a second then your unit test may be doing too much. The vast majority of tests should run in the order of milliseconds. –  RichardM Sep 16 '11 at 19:02

It could be useful if you are testing a procedure that is known to be slow. You could write unit tests that test the procedure with different sized inputs and you could monitor the performance of just that piece of code. Of course, whether or not this is applicable in your project really depends on your project. I use when it is easy to determine which function has a bottle-neck, and we want to isolate and test just that function.

share|improve this answer

I can only think of timing out tests if something like an endless loop could occur in the tested code.

If you run unit tests as a part of post-commit checks or a build, the machine handling it may have all kinds of loads that will make precisely timed tests useless. Run such performance tests (not unit tests) in a controlled environment.

It can be useful to collect stats of test execution times to predict running times of large test suites (integration servers, like TeamCity, do this).

share|improve this answer

Is the execution time of a unit test useful for anything?

Yes.

Is a unit test an appropriate place for any sort of code profiling?

Yes.

Why?

Because you need facts and measurements. If a test suite is slow, it's important to know where so you can focus -- separately -- on why.

Code profiling is only done when code is proven to be slow.

Unit tests are an easy, cheap, obvious, painless way to locate (1) slow code and (2) sudden changes in performance.

share|improve this answer

It could be valuable if you were to keep track of average performance for a given test across all test runs. A sudden increase in execution time might signal the introduction of a new bottleneck into the code.

Also in general it is desirable to keep the execution for an entire set of unit tests as fast as possible in order to encourage running the tests frequently. Knowing the execution of individual tests will help identify which tests are taking the most time across all tests.

share|improve this answer

Consider the development of your non-functional requirements:

Functional Requirement: Once the filters have been selected, the report should be generated. Non-Functional: The report should generate in 2 seconds.

If your test's execution time is > 2 seconds, you could even fail the test as a "usability" test.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.