Reputation: 4738
All is in the title, and I wonder if it is a good practice or not :
[TestMethod]
public void Compute_Array_In_Less_1_Second()
{
Stopwatch watcher = new Stopwatch();
int[] A = Enumerable.Range(0, 50000).Select(i => GetRandomNumber(MIN_INT, MAX_INT)).ToArray();
watcher.Start();
int[] R = Program.MethodThatReturnsAnArray(A);
watcher.Stop();
if (watcher.ElapsedMilliseconds > 1000)
Assert.Fail("The method runs in more 1 second");
}
Upvotes: 6
Views: 4746
Reputation: 31464
No. It couples your unit tesing failure/success to machine/environment. You don't want somebody with slower machine to have that unit test failing because of... well, slower machine?
Those tests might still have their place, but not as an unit tests - probably more of a functional or integration tests, which are not run by every developer in your team.
Upvotes: 2
Reputation: 20640
It depends on the application.
If it looks like you are going to lose data because some external process is running too slow, you might want to throw an exception.
But you still have to handle it. Why not just handle it where it occurs?
Upvotes: 0
Reputation: 301367
You should be using the appropriate mechanisms provided by your testing framework, like: http://nunit.org/index.php?p=timeout&r=2.5
But note that you don't want to be doing this everywhere ( to measure performance) but to test that the unit is actually finishing in time or times out if it needs to do that.
Upvotes: 3
Reputation: 882
I would think it would depend partly on what is being tested. If it is something that could potentially time out (say, getting a network connection, that really should happen within a particular time), it might be appropriate. As mentioned by Magrangs, timing things is really more performance testing than unit testing. Unit testing just checks to see if the thing running is actually doing what it is supposed to - not checking if it is running as fast as it is supposed to.
Upvotes: 0
Reputation: 273571
No it's not.
Unit tests are not performed under 'normal' conditions so the results would be useless.
Use Unit-testing to verify the correct semantics of your code.
Set up a performance test (usually end-to-end) under conditions close to the production environment.
Upvotes: 9