我正在使用xUnit 1.9运行一堆测试用例--所有这些测试用例都共享到一个资源的相同连接,但它们分为三种不同的类别,要求连接处于三种不同的状态。
我创建了一个治具类来处理连接,并创建了三个不同的类来保存需要三种不同连接状态的三类测试用例。
现在,我只相信通过构造函数只创建一次夹具对象,只通过构造函数连接一次,最后通过Dispose方法只断开一次连接。我说得对吗?
如何在每个类(方法组)中只设置一次连接状态,而不是每次设置每个方法的状态(将代码添加到每个类构造函数)?
虚拟代码:
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();
...
}发布于 2016-04-18 21:51:00
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);
}
}
}https://stackoverflow.com/questions/36638569
复制相似问题