Do the SetUp and TearDown methods need [RequiresSTA] if tests have it? - nunit

I can run my WatiN tests with ReSharper without issues. Each test has the RequiresSTA attribute and runs fine.
When I try run all the tests in the class (TestFixture) I get the following error:
One or more child tests had errors
Exception doesn't have a stacktrace
<testname> ignored: Invalid signature for SetUp or TearDown method: TestSetup
<testname> ignored: Invalid signature for SetUp or TearDown method: TestSetup
<testname> ignored: Invalid signature for SetUp or TearDown method: TestSetup
The error doesn't indicate what I need to change to make it work.
If I then select all the tests that have been ignored in the Unit Test Sessions window, I can run them without problems.
What must I change to allow me to run all the tests in the TestFixture?

I have met the same problem. I changed SetUp() and TearDown() methods to be public, then it worked.

I've always set the apartment state in the App.config file of my solution and the NUnit GUI runner runs entire fixtures as expected.
App.config starts off like this.
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="NUnit">
<section name="TestRunner" type="System.Configuration.NameValueSectionHandler"/>
</sectionGroup>
</configSections>
<NUnit>
<TestRunner>
<!-- Valid values are STA,MTA. Others ignored. -->
<add key="ApartmentState" value="STA"/>
</TestRunner>
</NUnit>
<appSettings>
........
Edit: I'm using Watin2.1 and NUnit 2.5.

Related

Only NUnit error: Could not load file or assembly 'EntityFramework'

I get an error message in NUnit when I try to run a specific test. The error looks like this:
ChatProj.Tests.MessageRepositoryTests.Logg_LoggWorking_AssertView:
System.IO.FileLoadException : Could not load file or assembly 'EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
Here is a picture
I've looked at the 2 projects in my solution, and I'm refrencing EF 6.0.0.0 in both. I've tried to look up the PublicKeyToken=b77a5c561934e089 and this is what I found in my solution. It's extra weird because I've no problem debugging or building the webapp, and the other unittests are testing within the same project (same EF version) and not reciving this error (as you can see in the picture from before).
Any idea what could be causing this problem?
UPDATE:
I think I figured it out. I was using nuget.org/packages/FakeDbSet which was using a command like : var mockItemList = new InMemoryDbSet { new Message {MessageID = 5000, Name ="Erland", MessageString ="Foo Bar", MessageDate = DateTime.Now} }; which somehow is an old way of faking entity framework code. Can be seen here: i.imgur.com/f6dnJbq.png
First, ensure that the EF DLL is actually getting pulled into the bin folder for the test application. If it is and the DLL is for EF v6, do you see version redirects for EF in your app.config for the test project? Something like this?
<configuration>
<!-- Leave the rest of the configuration file alone and just add the runtime area as follows: -->
<runtime>
<assemblyBinding>
<dependentAssembly>
<assemblyIdentity name="EntityFramework" publicKeyToken="b77a5c561934e089" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
If not, try pasting that into the app.config.
Sorry, it's hard to fully diagnose without more information.

Cannot run nunit tests with Nant

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?
One possible and simple solution:
set this:
[assembly: RequiresSTA]
in AssemblyInfo.cs file.
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

Define Entity Framework SetInitializer in web.config

Right now, in development I have the following code in the Global.asax.cs file that calls the Entity Framework (v4.1) SetInitializer with my SeedSampleData method. This all works perfectly.
However, I would like to store the SetInitializer "strategy" parameter through a web.config setting so that I can create a deployement script that will automatically set it to new System.Data.Entity.CreateDatabaseIfNotExists<EfDbContext>() instead of my seed method during production deployment.
The reason for wanting to move this to the web.config is that when I roll out a new deployment to the production server I want to make sure that I don't accidentally leave my seed initializer in the code.
protected void Application_Start()
{
//TODO: Figure out how to move the following lines to web.config and have a deployment script modify it when going to production.
//This line is for production
//System.Data.Entity.Database.SetInitializer(new System.Data.Entity.CreateDatabaseIfNotExists<EfDbContext>());
//This line is for development
System.Data.Entity.Database.SetInitializer(new Domain.Concrete.SeedSampleData());
//... Remainder of Application_Start calls here...
}
If you update to EF 4.3 (which is a good idea anyway), then you can use something like this in your web config:
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.3.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
<entityFramework>
<contexts>
<context type=" Blogging.BlogContext, MyAssembly">
<databaseInitializer type="Blogging.MyCustomBlogInitializer, MyAssembly" />
</context>
</contexts>
</entityFramework>
</configuration>
Rowan wrote about it in detail here: http://blogs.msdn.com/b/adonet/archive/2012/01/12/ef-4-3-configuration-file-settings.aspx
If you really want to keep using 4.1, then there is an older syntax that you can use instead. I wrote about it here: http://blog.oneunicorn.com/2011/03/31/configuring-database-initializers-in-a-config-file/
If I understand correctly, your SeedSampleData initializer is used only for debug purposes?
I don't know if there is a config parameter to control this, but you can use preprocessor directives:
#if DEBUG
System.Data.Entity.Database.SetInitializer(new Domain.Concrete.SeedSampleData());
#else
System.Data.Entity.Database.SetInitializer(new System.Data.Entity.CreateDatabaseIfNotExists<EfDbContext>();
#endif
(assuming, of course, that you don't deploy debug assemblies in production...)

Running WatiN tests with Nunit error?

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?
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.
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?

Running WatiN tests with ReSharper testrunner

I'm getting started with WatiN to test my web interface. The problem I'm having is the following:
When I start the tests from within TestDriven.net, I have no problem. If I use the ReSharper test runner, I get this predictable AppartmentState exception.
I tried using the different options described here: http://watin.sourceforge.net/apartmentstateinfo.html#testdriven. Nothing helps.
Any suggestions?
I've use Resharper test runner in most of my watin test projects. To get it to work use the same method as for nunit:
http://watin.sourceforge.net/apartmentstateinfo.html#nunit
App.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="NUnit">
<section name="TestRunner" type="System.Configuration.NameValueSectionHandler"/>
</sectionGroup>
</configSections>
<NUnit>
<TestRunner>
<!-- Valid values are STA,MTA. Others ignored. -->
<add key="ApartmentState" value="STA" />
</TestRunner>
</NUnit>
</configuration>
With NUnit 2.5, RequiresSTA attribute should be the tool of choice.
Using Resharper 5.1 with VisualStudio 2010 Ultimate I found that I needed to change my Resharper options to disable "Shadow-copy assemblies being tested" (found in Resharper --> Options --> Tools --> Unit Testing).
Additionally, I also found that the correct naming for the config file is the one which includes the dll extension (so: assemblyname.dll.config).