何时使用 TestFixtureSetUp 属性而不是默认构造函数? [英] When do I use the TestFixtureSetUp attribute instead of a default constructor?

查看:61
本文介绍了何时使用 TestFixtureSetUp 属性而不是默认构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NUnit 文档没有告诉我何时使用带有 TestFixtureSetup 的方法以及何时在构造函数中进行设置.

公共类MyTest{私人 MyClass myClass;公共 MyTest(){myClass = new MyClass();}[测试夹具设置]公共无效初始化(){myClass = new MyClass();}}

TestFixtureSetup 与默认构造函数是否有任何好的/坏的做法,或者没有任何区别?

解决方案

我认为这是 nUnit 团队尚未解决的问题之一.然而,有一个优秀的 xUnit 项目 看到了这个确切的问题并决定使用构造函数是个好东西关于测试装置初始化.>

对于 nunit,我在这种情况下的最佳实践是使用 TestFixtureSetUpTestFixtureTearDownSetUpTearDown 文档中描述的方法.

我认为当我不认为 nUnit 测试装置是普通类时,它也对我有帮助,即使您使用该构造定义它.我认为它们是固定装置,这让我克服了心理障碍,让我忽略了这个问题.

The NUnit documentation doesn't tell me when to use a method with a TestFixtureSetup and when to do the setup in the constructor.

public class MyTest
{
    private MyClass myClass;

    public MyTest()
    {
        myClass = new MyClass();
    }

    [TestFixtureSetUp]
    public void Init()
    {
        myClass = new MyClass();
    }
}

Are there any good/bad practices about the TestFixtureSetup versus default constructor or isn't there any difference?

解决方案

I think this has been one of the issues that hasn't been addressed by the nUnit team. However, there is the excellent xUnit project that saw this exact issue and decided that constructors were a good thing to use on test fixture initialization.

For nunit, my best practice in this case has been to use the TestFixtureSetUp, TestFixtureTearDown, SetUp, and TearDown methods as described in the documentation.

I think it also helps me when I don't think of an nUnit test fixture as a normal class, even though you are defining it with that construct. I think of them as fixtures, and that gets me over the mental hurdle and allows me to overlook this issue.

这篇关于何时使用 TestFixtureSetUp 属性而不是默认构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆