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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With