SetupFixture 不仅仅包含 Setup / TearDown

发布于 2024-09-30 03:31:46 字数 681 浏览 3 评论 0原文

是否可以将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清引 2024-10-07 03:31:46

对于程序集或命名空间,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.

酒几许 2024-10-07 03:31:46

如果您仍想对其进行断言,则可以保留该方法作为测试。如果您想在每次测试之前或之后调用它,您可以像普通方法调用一样在该方法内调用它。

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

    Setup_StaticObjectNamePropertyIsInitialized()
}

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

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文