[英]Creating nested TestFixture classes with NUnit
我正在尝试根据特定方案将单元测试类划分为逻辑分组。 但是,我需要有一个TestFixtureSetUp
和TestFixtureTearDown
,它将运行整个测试。 基本上我需要做这样的事情:
[TestFixture]
class Tests {
private Foo _foo; // some disposable resource
[TestFixtureSetUp]
public void Setup() {
_foo = new Foo("VALUE");
}
[TestFixture]
public class Given_some_scenario {
[Test]
public void foo_should_do_something_interesting() {
_foo.DoSomethingInteresting();
Assert.IsTrue(_foo.DidSomethingInteresting);
}
}
[TestFixtureTearDown]
public void Teardown() {
_foo.Close(); // free up
}
}
在这种情况下,我在_foo
上得到一个NullReferenceException,大概是因为在执行内部类之前调用了TearDown。
如何实现预期的效果(测试范围)? 是否有一个扩展或NUnit的东西,我可以使用,这将有所帮助? 我宁愿坚持使用NUnit,也不要使用像SpecFlow这样的东西。
您可以为测试创建抽象基类,在那里执行所有安装和拆解工作。 然后,您的方案将从该基类继承。
[TestFixture]
public abstract class TestBase {
protected Foo SystemUnderTest;
[Setup]
public void Setup() {
SystemUnterTest = new Foo("VALUE");
}
[TearDown]
public void Teardown() {
SystemUnterTest.Close();
}
}
public class Given_some_scenario : TestBase {
[Test]
public void foo_should_do_something_interesting() {
SystemUnderTest.DoSomethingInteresting();
Assert.IsTrue(SystemUnterTest.DidSomethingInteresting);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.