Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically timeout a test in MSTest

Tags:

c#

mstest

tfs

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.

like image 981
Christo Avatar asked Sep 20 '12 12:09

Christo


2 Answers

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

like image 154
Marko Avatar answered Oct 02 '22 11:10

Marko


It is not really "programmatically" but you could:

  • Set Time Limits for Running Tests in Visual Studio
  • 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.

like image 34
Elena Avatar answered Oct 02 '22 13:10

Elena