I have some test-code that is initializing some members which should be done just once at the beginning. This is why I used the constructor for that:
[TestFixture]
public class MyTestClass
{
private readonly IUnitTestGeometryProvider m_GeometryProvider;
public MyTestClass()
{
// do some heavy init-op
}
private IEnumerable<TestCaseData> TestCases()
{
yield return new TestCaseData(this.m_GeometryProvider.GetPolyline())
.Throws(typeof(ArgumentException));
}
[TestCaseSource("TestCases")]
public double Check_GetReducedArea_For_Invalid_Arguments(IGeometry theGeom)
{
return theGeom.GetReducedArea();
}
}
I know of the convention to use the FixtureSetup
-attribute for initializing tests, e.g. from this question on SO. However I noticed that the method TestCases
is executed before the method marked with that attribute so I run into an NullReferenceException
when evaluating the different testcases as the m_GeometryProvider
is null
at this time.
So I debugged my code and set a breakpoint into the constructor. I noticed, that it is executed twice before any test has even been run. I assumed every testcase having its own instance of the MyTestClass
, but as I have three different testcases and the constructor running twice this doesn't explain it.
As the initialization is heavy I´d like to execute it just once. Is there a way to guarantee this? I´d like to avoid a static
member as it often attracts colleagues to heavily use other static
members just because there already is one. Furthermore I´d consider the test-init to be specific for one instance of my MyTestClass
instead of the class itself - assuming there´s just one however.
I'm using NUnit 2.5.10.