Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can unit tests have a execution time criteria?

Tags:

unit-testing

Is it good practice to have a unit test that specifies how long a certain function takes to return a value. I'm unit testing a curl function and want to specify that it needs to time out after 5 seconds. Is this in line with the way unit tests work?

like image 568
Rich Bradshaw Avatar asked Nov 20 '08 10:11

Rich Bradshaw


People also ask

How long should unit tests take to run?

Typically the response I get when I ask this question is each test should take anywhere from 0.01 seconds to 1 second max to run in isolation.

What does the timely rule of unit testing mean?

Timely: Unit tests should be written just before the production code that makes the test pass. This is something that you would follow if you were doing TDD (Test Driven Development), but otherwise it might not apply. I personally do not use TDD and therefore I always write my tests after writing my production code.

What do you have to avoid in tests in unit testing?

Avoid Logic in Tests Doing so reduces the chance of introducing bugs into the test. The focus must remain on the end result, not on circumventions of implementation details. Without too many conditions, tests are also likely to be deterministic.


2 Answers

Testing your function plus curl plus the server is more integration test than unit test. That being said, you don't have to be that dogmatic, if you only have one test like that this certainly is viable. I've written, and still have tests that are not pure unit tests, but they do their job. I'll keep them as far as they don't go in my way.

The only thing I'd be bothered with is the five seconds timeout, which is very long for an unit test. Now, it depends on how often it occurs. Did you consider a basic test (e.g. pinging the server) prior launching curl to avoid starting a needless test.

If you're looking for alternatives, what about splitting your test in two parts: 1/ test how curl is invoked ; 2/ test what your function does with the result ? That way you'll be isolated from the server and you won't need any timeout.

like image 143
philant Avatar answered Oct 19 '22 01:10

philant


It depends. If the function is specified to time out after 5 sec then this is something to unit test. I don't think it's a good idea to measure performance in unit tests like this because it adds too many other reasons for unit tests to fail. If you start running unit tests in paralel on your build server you dont want the extra load to fail your tests for example.

like image 31
Mendelt Avatar answered Oct 19 '22 02:10

Mendelt