Ayaon
Ayaon

Reputation: 3

UnitTestOutcome.Timeout equivalent in NUNIT

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?

Upvotes: 0

Views: 258

Answers (2)

Charlie
Charlie

Reputation: 13681

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. :-)

Upvotes: 1

Francesco B.
Francesco B.

Reputation: 3097

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.

Upvotes: 0

Related Questions