NUnit的测试运行秩序秩序、测试、NUnit

2023-09-02 01:35:13 作者:脑震荡失忆

在默认情况下NUnit的测试按字母顺序运行。有谁知道有什么方法可以设置执行次序?有没有一个属性存在吗?

By default nunit tests run alphabetically. Does anyone know of any way to set the execution order? Does an attribute exist for this?

推荐答案

您的单元测试应该在每次能够独立运行,独立。如果他们满足这一标准,那么顺序并不重要。

Your unit tests should each be able to run independently and stand alone. If they satisfy this criterion then the order does not matter.

有几次不过,你会想先运行某些测试。一个典型的例子是在一些测试的时间比其他人运行的持续集成的局面。我们使用的类别属性,以便我们可以运行它使用的使用的数据库的测试嘲讽领先的测试。

There are occasions however where you will want to run certain tests first. A typical example is in a Continuous Integration situation where some tests are longer running than others. We use the category attribute so that we can run the tests which use mocking ahead of the tests which use the database.

即。把这个在你的快速测试的开始

i.e. put this at the start of your quick tests

[Category("QuickTests")]

如果你有试验这是依赖于一定的环境条件下,考虑的 TestFixtureSetUp 和 TestFixtureTearDown 的属性,它允许你标记方法前和你的测试后执行

Where you have tests which are dependant on certain environmental conditions, consider the TestFixtureSetUp and TestFixtureTearDown attributes, which allow you to mark methods to be executed before and after your tests.