运行 WatiN 测试时出现 Nunit 错误?
当我尝试通过 NUnit ide 运行 WatIn 测试时,收到错误消息:
ConsoleApplication1.Tests.CanBrowseToMicrosoft: System.Threading.ThreadStateException :CurrentThread 需要将其 ApartmentState 设置为 ApartmentState.STA 才能自动化 Internet Explorer。
我创建了一个名为 ConsoleApplication1.exe.config 的应用程序配置文件,如下:
<?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>
我的类 Tests.cs 如下:
[TestFixture]
public class Tests
{
[Test]
public void CanBrowseToMicrosoft()
{
using (var browser = new IE())
{
browser.GoTo("http://www.microsoft.com/");
Assert.That("Microsoft Corporation", Is.EqualTo(browser.Title));
}
}
}
我做错了什么吗?
我遇到的另一个问题是如何让 NUnit 测试结果显示在 vs2008 ide 中,而不必运行 NUnit Gui?
When I try to run a WatIn test trough the NUnit ide, I get the error message:
ConsoleApplication1.Tests.CanBrowseToMicrosoft:
System.Threading.ThreadStateException : The CurrentThread needs to have it's ApartmentState set to ApartmentState.STA to be able to automate Internet Explorer.
I created an application configuration file called ConsoleApplication1.exe.config which is below:
<?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>
My class Tests.cs is below:
[TestFixture]
public class Tests
{
[Test]
public void CanBrowseToMicrosoft()
{
using (var browser = new IE())
{
browser.GoTo("http://www.microsoft.com/");
Assert.That("Microsoft Corporation", Is.EqualTo(browser.Title));
}
}
}
Am I doing something wrong?
The other question I had was how do I get the NUnit test results to show up in vs2008 ide instead of having to run the NUnit Gui?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想通了,因为我正在加载一个名为Tests.nunit的NUnit项目,所以我需要调用应用程序配置文件Tests.config。经过这次更改后,效果很好。
I figured it out, because I was loading an NUnit project called Tests.nunit, I need to call the application configuration file Tests.config. After this change, it worked fine.
有一种更干净的方法来解决您的 STAThread 要求问题,但它需要 NUnit 2.5。
另外,您是否尝试过 TestDriven.Net 从 Visual Studio 运行单元测试?
There is a cleaner way to resolve your issue with STAThread requirement, but it requires NUnit 2.5.
Also, have you tried TestDriven.Net to run unit tests from Visual Studio?