0

Here is Timeout exception checking on the main code. Although the Timeout Exception occurs, Console shows abc.

However, In the Unit Test, when this Program meet a test case that has TimeoutException, It stop. How can I fix it? please.

try
{
    Stopwatch sw = new Stopwatch();
    if (sw.ElapsedMilliseconds > 3) throw new TimeoutException();
}
catch (TimeoutException)
{
    Console.WriteLine("Time Over!");
}
return abc;
[Test()]
public void TestCase()
{
  ...  
}
OBO
  • 57
  • 1
  • 6
  • I think you are after ExpectedException attribute? – andyb952 Jul 22 '19 at 09:04
  • maybe help you this question https://stackoverflow.com/questions/741029/best-way-to-test-exceptions-with-assert-to-ensure-they-will-be-thrown –  Jul 22 '19 at 09:21
  • It would be helpfull, if you post the whole method of the main code and the implementation of the test method. Without the implementation of the test method it is difficult to see, why the unit test stops – J.Gerbershagen Jul 22 '19 at 13:49
  • You can't write a good unit test for this. Should it throw the exception or not? The answer depends on whether or not it takes 3ms to check the stopwatch immediately after creating it. You can't control that. It's possible that it could throw the exception once but not throw it the next time. We write tests to verify that our code does something expected. If we write code with a race condition (we don't know which thing will happen first or how long something will take) then it's unpredictable. We can't write a test for it. – Scott Hannen Jul 22 '19 at 16:15

0 Answers0