Reputation: 1892
I've got a situation where I'm using a unit test to execute an external tool which performs a test. I can determine from the tool's exit code whether the test passed, failed or timed out.
Is there some way that I can fail the unit test which will set the test outcome to timeout instead of failed?
I've tried throwing a TimeoutException, but this has the same result as using an Assert.
Edit: We're linking the unit tests up with test cases in TFS. In Microsoft Test Center a test in a test run can have many states. One of which is the timeout state. I'm trying to fail my test so that it correctly shows up in this state and does not get bunched up with the failed test cases.
Upvotes: 9
Views: 7505
Reputation: 1924
You can set timeout limit per test, using addition of Timeout(millisecond) in Test attribute...
[TestMethod, Timeout(2000)]
Test will fail if execution takes longer than 2 seconds.
Thanks Marko
Upvotes: 20
Reputation: 1946
It is not really "programmatically" but you could:
Use Test Settings in Microsoft Test Center if you run your tests from Microsoft Test Center or using tcm.exe (see Run Tests from the Command Line Using Tcm)
Your test just should wait if it recognized that the external tool returns with the time out exit code (this would be the "programmatic" part.)
So the testing framework will set the test outcome to "Timeout" for you.
Especially if your tests are automated it would be a suitable solution, I suppose.
Upvotes: 3
Reputation: 5398
Your approach throwing a TimeoutException
seems to be a good choice. However the TimeoutException
doesn't derive from UnitTestAssertException.
You may want to do one of the following things:
Throw an AssertFailedException indicating the reason as string.
Sub-class the UnitTestAssertException with a custom UnitTestTimeOutException
and throw that.
Use Assert.Fail
as @Darin-Dimitrov suggested.
Upvotes: 0
Reputation: 1038850
In unit testing you dispose with 2 colors: green and red. There's no timeout color.
So I guess you could manually fail the test in case your external tools timeouts:
Assert.Fail("External tool used to do this test timed out");
The way to detect that your external tools has timed out will of course depend on the external tool you are using and the way you are invoking it from your unit test.
Upvotes: 1