Florian
Florian

Reputation: 4738

Is it a good practice to measure executed time of a method in a unit test to throw an exception if it runs too slow?

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

Answers (5)

k.m
k.m

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

Steve Wellens
Steve Wellens

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

manojlds
manojlds

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

Joel B
Joel B

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

Henk Holterman
Henk Holterman

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

Related Questions