RequiresSTAAttribute (NUnit 2.5)

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

Note: On test methods, you may also use the STAThreadAttribute. 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 STA thread will be created and used to run
// all the tests in the assembly
[assembly:RequiresSTA]

...

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

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

See also...