开发者

SetupFixture containing more than just Setup / TearDown

开发者 https://www.devze.com 2023-01-23 13:34 出处:网络
Is it possible to add Tests to a [SetupFixture] which are also called automatically on each run except for [SetUp]/[TearDown] ?

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();
    }
}


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.

[SetUp]
public void Setup() {
    StaticObject.DoInit();

    Setup_StaticObjectNamePropertyIsInitialized()
}

[SomethingThatIsCalledAutomaticallyToo]
public void Setup_StaticObjectNamePropertyIsInitialized()
{
    // test stuff
}
0

精彩评论

暂无评论...
验证码 换一张
取 消