C# 何时使用 TestFixtureSetUp 属性而不是默认构造函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/212718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 18:20:39  来源:igfitidea点击:

When do I use the TestFixtureSetUp attribute instead of a default constructor?

c#unit-testingnunit

提问by Paco

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

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

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 TestFixtureSetupversus default constructor or isn't there any difference?

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

采纳答案by casademora

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

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

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

对于NUnit的,在这种情况下,我最好的做法是使用TestFixtureSetUpTestFixtureTearDownSetUp,和TearDown如文档中描述的方法。

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.

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

回答by Sam Wessel

Why would you need to use a constructor in your test classes?

为什么需要在测试类中使用构造函数?

I use [SetUp]and [TearDown]marked methods for code to be executed before and after each test, and similarly [TestFixtureSetUp]and [TestFixtureTearDown]marked methods for code to be executed only once before and after all test in the fixture have been run.

我使用[SetUp][TearDown]代码标记方法前和每次测试后执行,同样[TestFixtureSetUp][TestFixtureTearDown]标示为代码的方法来进行前,在灯具的所有测试已运行后只执行一次。

I guess you could probably substitute the [TestFixtureSetUp]for a constructor (although I haven't tried), but this only seems to break from the clear convention that the marked methods provide.

我想您可能可以用 代替[TestFixtureSetUp]构造函数(尽管我还没有尝试过),但这似乎只是违反了标记方法提供的明确约定。

回答by casademora

I have often wondered what the need for [TestFixtureSetUp]was, given that there is a simple, well understood first class language construct that does exactly the same thing.

我经常想知道需要什么[TestFixtureSetUp],因为有一个简单的、易于理解的一流语言结构,它可以做完全相同的事情。

My preference is to use constructors, to take advantage of the readonly keyword ensuring member variables cannot be reinitialised.

我更喜欢使用构造函数,利用 readonly 关键字确保成员变量不能重新初始化。

回答by ripper234

I think I have a negative good answer - the reason to use a constructor instead of the attribute is when you have an inheritence between test classes.

我想我有一个否定的好答案 - 使用构造函数而不是属性的原因是当您在测试类之间有继承时。

Only one method annotated with [TestFixtureSetup]will be called (on the concrete class only), but the other fixture initializers will not. In this case I'd rather put the initialization in the constructor, which has a well-defined semantics for inheritance :)

只会[TestFixtureSetup]调用一个注释的方法(仅在具体类上),但其他夹具初始化器不会。在这种情况下,我宁愿将初始化放在构造函数中,它具有明确定义的继承语义:)

回答by Ergwun

One thing you can't do with [TestFixtureSetup]that you can do in the constructor is receive parameters from the [TestFixture].

[TestFixtureSetup]您不能在构造函数中做的一件事是从[TestFixture].

If you want to parameterise your test fixture, then you will have to use the constructor for at least someof the set-up. So far, I've only used this for integration tests, e.g. for testing a data access layer with multiple data providers:

如果你想参数化你的测试装置,那么你必须至少在一些设置中使用构造函数。到目前为止,我只将它用于集成测试,例如用于测试具有多个数据提供者的数据访问层:

[TestFixture("System.Data.SqlClient",
  "Server=(local)\SQLEXPRESS;Initial Catalog=MyTestDatabase;Integrated Security=True;Pooling=False"))]
[TestFixture("System.Data.SQLite", "Data Source=MyTestDatabase.s3db")])]
internal class MyDataAccessLayerIntegrationTests
{
    MyDataAccessLayerIntegrationTests(
        string dataProvider,
        string connectionString)
    {
        ...
    }
}

回答by oderibas

There is difference between constructor and method marked with [TestFixtureSetUp]attribute. According to NUnit documentation:

[TestFixtureSetUp]属性标记的构造函数和方法之间存在差异。根据 NUnit 文档:

It is advisable that the constructor not have any side effects, since NUnit may construct the object multiple times in the course of a session.

建议构造函数没有任何副作用,因为 NUnit 可能在会话过程中多次构造对象。

So if you have any expensive initialization it is better to use TestFixtureSetUp.

因此,如果您有任何昂贵的初始化,最好使用TestFixtureSetUp.

回答by nonexistent myth

The constructor and the SetUpmethods are used differently:
The constructor is run only once.
However, the SetUpmethods are run multiple times, before every test case is executed.

构造函数和SetUp方法的使用方式不同:
构造函数只运行一次。
但是,在SetUp执行每个测试用例之前,这些方法会运行多次。

回答by Shankar

[TestFixtureSetUp]and [TestFixtureTearDown]are for whole test class. runs only once.

[TestFixtureSetUp]并且[TestFixtureTearDown]适用于整个测试班。只运行一次。

[SetUp]and [TearDown]are for every test method(test). runs for every test.

[SetUp]并且[TearDown]适用于每种测试方法(测试)。为每个测试运行。

回答by Novaterata

An important difference between constructor and TestFixtureSetUp is that, in NUnit 2 at least, constructor code is actually executed on test enumeration, not just test running, so basically you want to limit ctor code to only populating readonly, i.e. parameter, values. Anything that causes side-effects or does any actual work needs to either be wrapped in a Lazy or done in the TestFixtureSetUp / OneTimeSetUp. So, you can think of the constructor as solely a place to configure the test. Whereas the TestFixtureSetUp is where the test fixture, the required initial state of the system before tests are run, is initialized.

构造函数和 TestFixtureSetUp 之间的一个重要区别是,至少在 NUnit 2 中,构造函数代码实际上是在测试枚举时执行的,而不仅仅是测试运行,所以基本上你想限制 ctor 代码只填充只读,即参数,值。任何导致副作用或执行任何实际工作的内容都需要包含在 Lazy 中或在 TestFixtureSetUp / OneTimeSetUp 中完成。因此,您可以将构造函数视为仅用于配置测试的地方。而 TestFixtureSetUp 是测试夹具(运行测试之前所需的系统初始状态)被初始化的地方。