简体   繁体   中英

Programmatically timeout a test in MSTest

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.

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

It is not really "programmatically" but you could:

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.

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.

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:

  1. Throw an AssertFailedException indicating the reason as string.

  2. Sub-class the UnitTestAssertException with a custom UnitTestTimeOutException and throw that.

  3. Use Assert.Fail as @Darin-Dimitrov suggested .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM