0

Question is taken from here: SO - Timeout in c# tests

I created a simple unit test like this:

[Timeout(1000)][TestMethod]
public void TestMethod1()
{
    try
    {
        System.Threading.Thread.Sleep(2000);
    }
    finally
    {
        Console.WriteLine("Executed");
    }
}

When I run the test, the finally block is not executed. But when I debug it, it does. Why is this happening?

Community
  • 1
  • 1
chaliasos
  • 9,659
  • 7
  • 50
  • 87
  • perhaps problem is in `Console.Writeline` - check solution provided in this [question](http://stackoverflow.com/questions/7461808/how-to-console-writeline-from-testmethod) – Nogard Nov 22 '12 at 14:53

3 Answers3

4

You specify the test timeout at 1000 ms, while your method sleeps for 2000 ms. This results in your test method being prematurely force-closed by the test framework, so it doesn't leave the Sleep call and doesn't have time to reach finally block. Debugging probably disables the timeout attribute.

Honza Brestan
  • 10,637
  • 2
  • 32
  • 43
1

The Timeout attribute simply does not apply to Debugging sessions.

Keysharpener
  • 486
  • 3
  • 14
1

Timeout is disabled during debugging, so you get to see Console.WriteLine() output.

But i still think finally is executed in vs2010. If you try to show a message box System.Windows.Forms.MessageBox.Show("finally") you should be able to see it pops up.

Console.WriteLine output is lost after timeout in vs2010.

waterbear
  • 69
  • 1
  • 11