Dot-Net

NUnit GUI Runner 和 Apartment State

  • February 28, 2012

如何在 NUnit GUI 執行器中設置公寓狀態?我正在嘗試使用 WatiN 執行單個 NUnit 測試,並且收到以下消息:

MyNamespace.LoginTests.CanLogin:

System.Threading.ThreadStateException : CurrentThread 需要將其 ApartmentState 設置為 ApartmentState.STA 才能使 Internet Explorer 自動化。

您需要在程序集的 app.config 文件中添加一些配置(如果沒有,請創建一個新配置)以告訴 NUnit 以 STA 身份執行:

<?xml version="1.0" encoding="utf-8" ?>

<configuration>
   <configSections>
       <sectionGroup name="NUnit">
           <section name="TestRunner" 
                    type="System.Configuration.NameValueSectionHandler"/>
       </sectionGroup>
   </configSections>

   <NUnit>
       <TestRunner>
           <add key="ApartmentState" value="STA" />
       </TestRunner>
   </NUnit>
</configuration>

原始來源

從 NUnit 2.5 開始,在測試中使用RequiresSTA屬性。

引用自:https://stackoverflow.com/questions/2463461