Reputation: 725
I am using xUnit 1.9 to run a bunch of test cases all sharing same connection to a resource, but they are falling into three different categories requiring the connection to be in three different states.
I've created one fixture class that handles the connection and three different classes to hold the three categories of test cases requiring the three different connection states.
Now I trust the fixture object to be created only once and connect only once through the constructor and disconnect only once at the end through the Dispose method. Have I got that right?
How can I set the connection state only once per class(group of methods) rather than setting the state every time for every method (by adding the code to each class constructor)?
Dummy code:
public class Fixture : IDispose
{
public Fixture() { connect(); }
public void Dispose() { disconnect(); }
public void SetState1();
public void SetState2();
public void SetState3();
}
public class TestCasesForState1 : IUseFixture<Fixture>
{
public void SetFixture(fix) { fix.SetState1() } // will be called for every test case. I'd rather have it being called once for each group
[Fact]
public void TestCase1();
...
}
public class TestCasesForState2 : IUseFixture<Fixture>
{
public void SetFixture(fix) { fix.SetState2() } // will be called for every test case. I'd rather have it being called once for each group
[Fact]
public void TestCase1();
...
}
public class TestCasesForState3 : IUseFixture<Fixture>
{
public void SetFixture(fix) { fix.SetState3() } // will be called for every test case. I'd rather have it being called once for each group
[Fact]
public void TestCase1();
...
}
Upvotes: 2
Views: 96
Reputation: 247088
public class Fixture : IDispose {
public Fixture() { connect(); }
public void Dispose() { disconnect(); }
static bool state1Set;
public void SetState1() {
if(!state1Set) {
state1Set = true;
//...other code
changeState(1);
}
}
static bool state2Set;
public void SetState2() {
if(!state2Set) {
state2Set = true;
//...other code
changeState(2);
}
}
static bool state3Set;
public void SetState3() {
if(!state3Set) {
state3Set = true;
//...other code
changeState(3);
}
}
}
Upvotes: 1