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
Build/Test Issues
Inherited TestInitialize is being run for test method on base class
Started by rneeft on 8,705 views
Remco NCrunch Developer
#6516
09 Oct 2014 21:41 UTC
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
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
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
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
Post a reply
Log in to reply.