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");
}
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.
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.
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