Cannot run nunit tests with Nant

2019-08-14 18:14发布

问题:

I'm trying to launch nunit ui tests with nant and get the error:

Buildfile: file:///c:/UItests/nant.build Target framework: Microsoft .NET Framework 4.0 Target(s) specified: build

build:

[nunit2] 2012/06/20/13:29:52: Exception in set up method: The CurrentThread needs to have it's ApartmentState set to Apar tmentState.STA to be able to automate Internet Explorer. [nunit2] 2012/06/20/13:29:52: Exception in set up method: The CurrentThread needs to have it's ApartmentState set to Apar tmentState.STA to be able to automate Internet Explorer. [nunit2] 2012/06/20/13:29:52: Exception in set up method: The CurrentThread needs to have it's ApartmentState set to Apar tmentState.STA to be able to automate Internet Explorer. [nunit2] 2012/06/20/13:29:52: Exception in set up method: The CurrentThread needs to have it's ApartmentState set to Apar tmentState.STA to be able to automate Internet Explorer.
[nunit2] 2012/06/20/13:29:52: Exception in set up method: The CurrentThread needs to have it's ApartmentState set to Apar tmentState.STA to be able to automate Internet Explorer.

I tried with App.config that comes with my project, like this:

<test assemblyname="UITests.dll" appconfig="UITests.dll.config" />

And also I created config file manually and put it to folder with test and nunit assemblies. But nothing helped. In config file I have:

 <add key="ApartmentState" value="STA" />

Here is nant.build file:

 <?xml version="1.0"?>
 <project name="UITests" default="build">
 <property name="build.dir" value="" />
 <target name="build">
  <nunit2>
<formatter type="Plain" />
<test assemblyname="${build.dir}UITests.dll" appconfig="UITests.dll.config" />
  </nunit2>
</target>
</project>

What I missed?

回答1:

One possible and simple solution:

set this:

[assembly: RequiresSTA]

in AssemblyInfo.cs file.



回答2:

Nant is able to run the tests but the TestSetup is doing something which is not configured properly. Probably try writing and running a simpler test like Assert.That( 10+1, Is.Equals(11)); and running that. If it works then i think the question should be posted with more details on what and how you are testing



标签: nunit nant