RequiresSTA特性用於測試方法、類、程序集中指定測試應該在單線程中運行。若是父測試不在單線程中運行則會建立一個新的線程。less
Note:函數
在測試方法上也能夠使用STAThread特性。儘管運行時指揮在執行程序集的入口識別這個特性,可是許多用戶但願再測試上工做,因此咱們把它做爲一個同義詞。測試
// 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. } }
RequiresThread特性指示一個測試方法、類或者程序集應該在一個獨立的線程中運行。還能夠在構造函數中指定須要的線程。ui
Note:這個特性常常和ApartmentState參數一塊兒使用,配合使用來建立一個新的線程。若是不合適用ApartmentState來建立線程,還還能夠使用RequiresSTA特性或者RequiresMTA特性。this
// 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. } }