代码改变世界

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

2013-12-09 20:45  Max蚊子  阅读(923)  评论(0编辑  收藏  举报

RequiresSTAAttribute (NUnit 2.5)

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

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特性指示一个测试方法、类或者程序集应该在一个独立的线程中运行。还可以在构造函数中指定需要的线程。

Note:这个特性经常和ApartmentState参数一起使用,配合使用来创建一个新的线程。如果不合适用ApartmentState来创建线程,还还可以使用RequiresSTA特性或者RequiresMTA特性。

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

 

 

网站:feiger.cn         飞鸽博客,关注互联网、站长圈的程序员博客!