Reputation: 21355
So I have a very simple setup to initialize a LocalDB database to run some integration tests.
In my setup, I'm initializing my tests as follows:
[AssemblyInitialize]
public static void AssemblyInitialize(TestContext context)
{
AppDomain.CurrentDomain.SetData(
"DataDirectory", Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ""));
System.Data.Entity.Database.SetInitializer(new MyDatabaseInitializer());
}
public class MyDatabaseInitializer : System.Data.Entity.DropCreateDatabaseAlways<Context>
{
protected override void Seed(Context context)
{
// Add entities to database.
context.Blogs.AddOrUpdate(x => x.Title, new Blog
{
BlogId = Guid.NewGuid(),
Title = "some Title"
});
context.SaveChanges();
}
}
I have three tests like these:
[TestMethod]
[DeploymentItem("Database1.mdf")]
[DeploymentItem("UnitTestProject1.dll.config")]
public void initialize()
{
}
[TestMethod]
public void TestMethod1()
{
var ctx = new Context();
var res = ctx.Blogs.ToList();
res.Should().NotBeNull().And.HaveCount(1);
}
[TestMethod]
public void it_should_be_able_to_add_a_new_blog()
{
var ctx = new Context();
ctx.Blogs.Add(new Blog {BlogId = Guid.NewGuid(), Title = "OMFG " + DateTime.Now.ToShortDateString()});
ctx.SaveChanges();
ctx.Blogs.Should().HaveCount(2);
}
And I have an OrderedTest file that looks like:
<?xml version="1.0" encoding="UTF-8"?>
<OrderedTest name="orderedtest1" storage="c:\users\jolmos\documents\visual studio 2012\projects\consoleapplication9\unittestproject1\orderedtest1.orderedtest" id="cc545e78-c463-4f38-88fc-ac8b49b420be" xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010">
<TestLinks>
<TestLink id="d0380f08-e99f-ebae-e5cd-9ae8196521cb" name="initialize" storage="bin\debug\unittestproject1.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="5b688acc-bf10-4c7e-4b60-69d8596bdd90" name="TestMethod1" storage="bin\debug\unittestproject1.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<TestLink id="0c248cf5-fa4f-6c24-b151-3ffdbae30f10" name="it_should_be_able_to_add_a_new_blog" storage="bin\debug\unittestproject1.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</TestLinks>
</OrderedTest>
The tests work great btw...but...
For some reason my tests run twice
So I have a break-point on each test and when I click Test | Debug | All Tests, the tests are executed twice even the AssemblyInitialize
method is executed twice =(
I just found that the tests are been created in two different folders under my TestResults
folder with a difference of 1 second
The name in the picture below is just my network username
What am I missing in my configuration?
Upvotes: 4
Views: 4622
Reputation: 9783
For some reason my tests run twice
Your tests run twice because they run as simple unit test and as part of the ordered test.
even the AssemblyInitialize method is executed twice
If you open the bin\debug
folder you will see that your orderedtest is contained in there as orderedtest1.orderedtest
. You have already opened it, so you saw that it is a simple xml file. This file is not compiled into the assembly.
When visual studio starts executing your tests, it picks the unit tests first. So, it initialize the assembly they are contained. When they are finished, it does not know that the ordered test contains tests from the same assembly, so the AssemblyCleanup
is executed. When it starts executing the orderedtest, it must re-initialize the Assembly in order to test the unit tests contained to it.
Upvotes: 4
Reputation: 14938
This is because your Ordered Test is itself a test, so running all tests means that all of the individual tests get run as well as the Ordered test, hence why those tests contained within it will be run twice.
You can use the Test List Editor to selectively run tests (Test\Windows\Test List Editor).
Upvotes: 3