Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Inherited TestInitialize is being run for test method on base class
rneeft
#1 Posted : Thursday, October 9, 2014 8:18:50 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 10/9/2014(UTC)
Posts: 2
Location: Netherlands

Thanks: 1 times
Was thanked: 1 time(s) in 1 post(s)
Hi,

I'm trying NCrunch at the moment for my company and I hit an error. It seems that when you having an inherited test class, the TestInitialize method from the inherited class is being run for test methods in the base class.

As an example I've created the following code:

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UnitTestProject1
{
[TestClass]
public class BaseTest
{
protected CalcBuilder builder;

[TestInitialize]
public void TestInitialize()
{
builder = new CalcBuilder().SetX(1)
.SetY(1);
}

[TestMethod]
public void SumShouldBeTwo()
{
Assert.AreEqual(2, builder.Make().Sum());
}
}

public class CalcBuilder
{
int x, y;

public CalcBuilder SetX(int x)
{
this.x = x;
return this;
}

public CalcBuilder SetY(int y)
{
this.y = y;
return this;
}

public Calc Make()
{
return new Calc(this.x, this.y);
}
}

public class Calc
{
private int x, y;

public Calc(int x, int y)
{
this.x = x;
this.y = y;
}

public int Sum()
{
return x + y;
}
}
}

The above code runs without any problem, all the NCrunch indications lights are green. But when I add the following class, the test method is failing in NCrunch:

[TestClass]
public class BaseTest2 : BaseTest
{
[TestInitialize]
public void NewSetup()
{
builder = builder.SetX(2)
.SetY(2);
}
}

When I run the test method in ReSharper or with the Visual Studio test runner, the test is passing. But the NCrunch indication lights are red :(

Is this a bug?

Thank you in advance.
Rick Neeft
Remco
#2 Posted : Thursday, October 9, 2014 9:41:44 PM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 6,976

Thanks: 931 times
Was thanked: 1257 time(s) in 1170 post(s)
Hi Rick,

Thanks for posting!

Could you confirm which version of Visual Studio / MSTest you are using for this? I've just checked the behaviour of VSTest 2013 and confirmed that the behaviour seems to match that of NCrunch. I see BaseTest2.BaseTest:SumShouldBeTwo failing and BaseTest.SumShouldBeTwo passing. MSTest does run inherited TestInitialize methods.

Note that NCrunch will show red indicators on code executed by a test, and this can include inherited test code. In the above case, the markers next to the passing test on the base class are red because this same code is being re-used for the failing test.

Is it possible there is another reason for your tests failing under NCrunch? Note that there are some differences in how NCrunch calls into test runners that can sometimes make tests behave strangely - http://www.ncrunch.net/documentation/considerations-and-constraints_test-atomicity.

Cheers,

Remco
rneeft
#3 Posted : Friday, October 10, 2014 10:56:57 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 10/9/2014(UTC)
Posts: 2
Location: Netherlands

Thanks: 1 times
Was thanked: 1 time(s) in 1 post(s)
Hi Remco,

I'm using VS2013 Update 3 with NCrunch 2.10.0.4.

I see, I have 2 tests in my test explorer. I didn't notice that. It seems VSTest sees the SumShouldBeTwo test method as one from BaseTest and one from BaseTest2. Off course the test is double, it is inherited from its base. (now I feel a bit stupid).

Thank you for your assistance!

Rick
1 user thanked rneeft for this useful post.
Remco on 10/10/2014(UTC)
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

YAF | YAF © 2003-2011, Yet Another Forum.NET
This page was generated in 0.038 seconds.
Trial NCrunch
Take NCrunch for a spin
Do your fingers a favour and supercharge your testing workflow
Free Download