SetupFixture 不仅仅包含 Setup / TearDown
是否可以将 Tests
添加到 [SetupFixture]
,除了 [SetUp]/[ 之外,每次运行时也会自动调用该测试拆解]
?
例如:
[SetupFixture]
public class SetupFixture {
[SetUp]
public void Setup() {
StaticObject.DoInit();
}
[SomethingThatIsCalledAutomaticallyToo]
public void Setup_StaticObjectNamePropertyIsInitialized()
{
// Arrange
var expected = "ObjectName!";
// Act
var actual = StaticObject.Name;
// Assert
Assert.AreEqual(expected.equals(actual));
}
// More Checks....
[TearDown]
public void TearDown() {
StaticObject.DoEqualize();
}
}
Is it possible to add Tests
to a [SetupFixture]
which are also called automatically on each run except for [SetUp]/[TearDown]
?
E.g:
[SetupFixture]
public class SetupFixture {
[SetUp]
public void Setup() {
StaticObject.DoInit();
}
[SomethingThatIsCalledAutomaticallyToo]
public void Setup_StaticObjectNamePropertyIsInitialized()
{
// Arrange
var expected = "ObjectName!";
// Act
var actual = StaticObject.Name;
// Assert
Assert.AreEqual(expected.equals(actual));
}
// More Checks....
[TearDown]
public void TearDown() {
StaticObject.DoEqualize();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于程序集或命名空间,SetupFixture 只会运行一次,具体取决于您放置它的位置。
为什么不拥有一个可供其他测试继承的抽象基类。每当我有一个具有多个实现的抽象类时,我都会有一个用于测试基本功能的基本测试,然后从它继承我想要测试的每个实现。基础装置中的测试将为每个实现运行。
The SetupFixture is run once and only once for either the assembly or namespace depending on where you have put it.
Why not have an abstract base class which you other tests inherit from. Whenever I have a abstract class with multiple implementations I have a base test for testing the base functionality and then inherit from it for each of the implementations I want to test. The tests in the base fixture will be run for each implementation.
如果您仍想对其进行断言,则可以保留该方法作为测试。如果您想在每次测试之前或之后调用它,您可以像普通方法调用一样在该方法内调用它。
You could keep the method as a test if you still want to make assertions on it. If you want to call it before or after each test, you can just call it inside that method like a normal method call.