0

I am migrating our Project from MSTest to NUnit. I have a scenario where I need to execute the below condition

testContext.CurrentTestOutcome.Equals(UnitTestOutcome.Timeout)

Can you please suggest the NUnit equivalent to MSTest's UnitTestOutcome.Timeout?

Impulse The Fox
  • 2,638
  • 2
  • 27
  • 52
Ayaon
  • 3
  • 1

2 Answers2

1

The question is not entirely clear. @Francesco B. already interpreted it as meaning "How can I specify a timeout?" and answered accordingly.

I understand you to be asking "How can I detect that my test has timed out?" Short answer - you can't detect it in the test itself. It can only be detected by a runner that is executing your test.

Longer answer...

You can examine the test context in your teardown to see what was the outcome of the test using TestContext.CurrentContext.Result.Outcome. This is useful if your teardown needs to know the test has failed.

However, you will never see an outcome of "timed out" because...

  1. Your teardown is included in what gets timed by the Timeout attribute.
  2. Your teardown won't be called if the test method triggers timeout.
  3. Even if the first two points were not true, there is no "timed out" outcome. The test is marked as a failure and only the message indicates it timed out.

Of course, if I misunderstood the question and you just wanted to know how to specify a timeout, the other answer is what you want. :-)

Charlie
  • 12,928
  • 1
  • 27
  • 31
  • Thanks A Lot @charlie for giving such a detailed description on how TimeOut works with NUnit. I guess I need to implement some alternative approach to handle timeout in NUnit. Coming to my question , no your understanding was correct, I wanted to check the outcome of test result as TimeOut and then perform some action,something similar to the code snippet below if(testContext.CurrentTestOutcome.Equals(UnitTestOutcome.Timeout)) { Console.WriteLine("test outcome when time out " + testContext.CurrentTestOutcome); } – Ayaon Apr 19 '18 at 02:33
0

As per the official documentation, you can use the Timeout attribute:

[Test, Timeout(2000)]
public void PotentiallyLongRunningTest()
{
    ...
}

Of course you will have to provide the timeout value in milliseconds; past that limit, your test will be listed as failed.

There is a known "rare" case where NUnit doesn't respect the timeout, which has already been discussed.

Francesco B.
  • 2,729
  • 4
  • 25
  • 37
  • 1
    Note that the "rare case" only applies to NUnit V2's `ExpectedExceptionAttribute`, which no longer exists in NUnit 3. – Charlie Apr 18 '18 at 15:24
  • @Charlie I'm still amazed by the remarkable (and often remarkably kind too) people that you can "meet" here at StackOverflow :) – Francesco B. Apr 18 '18 at 18:00