RequiresMTAAttribute (NUnit 2.5)

The RequiresMTAAttribute is used on a test method, class or assembly to specify that the tests should be run in the multi-threaded apartment. It causes creation of a new thread if the parent test is not already running in the MTA.

Note: On test methods, you may also use the MTAThreadAttribute. Although the runtime only recognizes this attribute on the entrypoint of an executable assembly, many users have expected it to work on tests, so we treat it as a synonym.

Examples


// An MTA thread will be created and used to run
// all the tests in the assembly
[assembly:RequiresMTA]

...

// TestFixture requiring a separate thread
[TestFixture, RequiresMTA]
public class FixtureRequiringMTA
{
  // An MTA thread will be created and all
  // tests in the fixture will run on it
  // unless the containing assembly is
  // already running on an MTA Thread
}

[TestFixture]
public class AnotherFixture
{
  [Test, RequiresMTA]
  public void TestRequiringMTA()
  {
    // A separate thread will be created for this test
	// unless the containing fixture is already running 
	// in the MTA.
  }
}

See also...