[翻译]NUnit---RequiresSTA and RequiresThread Attributes(十七)

RequiresSTAAttribute (NUnit 2.5)

  RequiresSTA特性用于测试方法、类、程序集中指定测试应该在单线程中运行。若是父测试不在单线程中运行则会建立一个新的线程。less

Note:函数

  在测试方法上也能够使用STAThread特性。尽管运行时指挥在执行程序集的入口识别这个特性,可是许多用户但愿再测试上工做,因此咱们把它做为一个同义词。测试

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

  • RequiresThreadAttribute
  • RequiresMTAAttribute

RequiresThreadAttribute (NUnit 2.5)

  RequiresThread特性指示一个测试方法、类或者程序集应该在一个独立的线程中运行。还能够在构造函数中指定须要的线程。ui

Note:这个特性常常和ApartmentState参数一块儿使用,配合使用来建立一个新的线程。若是不合适用ApartmentState来建立线程,还还能够使用RequiresSTA特性或者RequiresMTA特性。this

Examples

// A thread will be created and used to run
// all the tests in the assembly
[assembly:RequiresThread]

...

// TestFixture requiring a separate thread
[TestFixture, RequiresThread]
public class FixtureOnThread
{
  // A separate thread will be created and all
  // tests in the fixture will run on it.
}

[TestFixture]
public class AnotherFixture
{
  [Test, RequiresThread]
  public void TestRequiringThread()
  {
    // A separate thread will be created for this test
  }
  
  [Test, RequiresThread(ApartmentState.STA)]
  public void TestRequiringSTAThread()
  {
    // A separate STA thread will be created for tnis test.
  }
}

See also...

  • RequiresSTAAttribute
  • RequiresMTAAttribute
相关文章
相关标签/搜索