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?
Related
I am using NUnit 2.6.3 and .NET framework 4.5.
I have a file, Test.NUnit, in my test project which contains:
<NUnitProject>
<Settings activeconfig="Debug"
appbase="E:\Test" />
<Config name="Debug" binpathtype="Auto">
<assembly path="bin\Debug\Test.dll" />
</Config>
<Config name="Release" binpathtype="Auto" />
</NUnitProject>
When I try to run this file I get an error message
Test load failed System.ApplicationException: Unable to find test in assembly
The problem is similar to this question. However, I can't get the answer to work.
I have a Test.dll.config file in my project which is located in E:\Test.
It contains:
<startup useLegacyV2RuntimeActivationPolicy="true">
<!--<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />>-->
<supportedRuntime version="v4.0.30319" />
</startup>
I have tried both versions of the supportedRuntime to show that I am using .NET framework 4.5.
I had a similar issue. Check your firewall blocking nunit-agent.exe trying to connect 127.0.0.1.
NUnit uses it for running tests and doesn't show the correct error if it can't connect to 127.0.0.1.
I have a class library I have been using for over a year that has recently stopped working after upgrading to EF 6.1.
I have tried various methods for passing in the connect string to my Context class constructor but while it seems to correctly pass the string I invariably receive:
'(((System.Data.Entity.DbContext)(context)).Database.Connection).ServerVersion' threw an exception of type 'System.InvalidOperationException'
and the connection state stays closed.
Here is my AppConfig:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --></configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
</startup>
<connectionStrings>
<add name="MyContext" connectionString="Data Source=Server;Initial Catalog=DBName;Integrated Security=True;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
</connectionStrings>
<entityFramework>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
My test class:
using System.Data.Entity;
namespace SVMIC.IMS.Entity.IMSClaim
{
public class Context:DbContext
{
static Context()
{
Database.SetInitializer<Context>(null);
}
public Context():base("MyContext")
{
}
}
}
and my test app:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SVMIC.IMS.Entity.IMSClaim;
namespace TestApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Context context = new Context();
}
}
}
The database server is SQL Server 2008 R2.
I assume it is something simple changed in 6.1 and I am just missing it, but I am totally stumped.
Ok, I have resolved the issue.
In frustration at not being able to fix the issue with 6.1, nothing I did made any difference to the result, and even building and stepping through the EF source accomplished little so I rolled back to EF 5.0 and rebuilt my code.
EF 5 immediately returned an error message identifying an issue in the schema definition, a view added by another developer that referenced system views was confusing my template, I fixed the issue and was back working in a matter of minutes.
The lesson learned here was, EF 6 needs some serious work on its error handling, as just returning a generic exception error left me chasing off in every direction, while the error message from EF 5 pointed me directly at the issue.
Based on the comments what appears to have happened is:
The original EF Upgrade did not work, so you were still running on the old EF
When you made the code change, the rebuild triggered that you would use the New EF
But the New EF dll's are not available, so you get the error.
Check you config: the EF Version is 6.0 not 6.1, also the Public key token is the same as for .net 2.0.
Just for information on this error
I was configuring Repository and Unit of Work Design Pattern with EF 6.1 with a simple test database named as "School"
It had following 3 tables
BookMaster
Library and
Student
I had created their respective Entity classes, Configurations classes, Repository classes and also created DB Sets in Entities class.
After few days I had created one more table whose above mentioned classes and DbSets were not created.
What I found is providing connectionstring name in base() constructor parameter was giving me following error
'(context.Database.Connection).ServerVersion' threw an exception of type >'System.InvalidOperationException' entity framework 6.1
However if I pass complete connection string as a parameter to base() method then it was working.
After some Trial and Error I deleted the 4th table that was created and It worked like a dream. So may be it is necessary to utilize all the tables and create the above mentioned classes and DbSets which may resolve this issue
If you are working local MDF file, the update the folder path of MDF file in App.config/ Web.config
else copy the dbmx App.cofig connectionStrings and paste to your project App.config/Web.config file.
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.
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...)
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).