Timon Post
Timon Post

Reputation: 2879

How do I specify test timeout for custom NUnit Test Runner?

I run unit tests from code, and my tests can be infinitely long. NUnit keeps running until all tests are closed hence my test session never closes.

// Get an interface to the engine
Engine.ITestEngine engine = Engine.TestEngineActivator.CreateInstance();

// Create a simple test package - one assembly, no special settings
Engine.TestPackage package = new Engine.TestPackage(_testProjectAssemblyPath);

// Get a runner for the test package
var runner = engine.GetRunner(package);

XmlNode testSessionResult = runner.Run(listener: new Listener1(), Engine.TestFilter.Empty);

Is it possible to specify a time out without using the Timeout NUnit attribute? Perhaps by specifying .runsettings, or using ISettings?

I tried firing up a task with a timeout, however, this prevents me to see the failing tests, which is mandatory in my use case.

Upvotes: 0

Views: 485

Answers (1)

Charlie
Charlie

Reputation: 13736

The NUnit engine only reads settings from the TestPackage. The other approaches you list are used by runners in order to tailor what they add to the TestPackage.

To set a general timeout, which applies to all the tests in the run, use something like this...

package.AddSetting("DefaultTimeOut", 4000);

The above will give each individual test case 4 seconds before it is cancelled.

Setting the timeout for test cases individually is only possible through the attribute.

Upvotes: 1

Related Questions