NUnit 中的执行顺序是什么?
我一直在对测试驱动开发进行一些研究,发现它非常酷。
我遇到的一件事是,当您编写测试时,设置和测试方法([Setup] 和 [Test])有一个执行顺序。
测试时是否可以使用其他方法,如果可以,它们的执行顺序是什么,例如处置或其他什么? 我看到了测试夹具的设置,但不太熟悉。
示例:
当我运行测试时,它首先执行[设置],然后运行[测试],当它进入下一个测试时,它再次运行[设置],然后进入[测试]。
如果有帮助的话我正在使用 NUnit。
这是我设置的一个截断示例:
using NUnit.Framework;
namespace TestingProject
{
[TestFixture]
public class CustomerService_Tests
{
public string MyAccount = string.Empty;
[SetUp]
public void Setup()
{
MyAccount = "This Account";
}
[Test]
public void Validate_That_Account_Is_Not_Empty()
{
Assert.That(!string.IsNullOrEmpty(MyAccount));
}
[Test]
public void Validate_That_Account_Is_Empty()
{
Assert.That(string.IsNullOrEmpty(MyAccount));
}
}
}
因此,当我运行测试时,它会进行设置,然后是第一个测试,然后是设置,然后是第二个测试。
我的问题是在测试时可以使用哪些其他类型,例如 [Setup] 和 [Test],以及这些类型的执行顺序是什么。
I have been doing some research on test driven development and find it pretty cool.
One of the things I came across was that when you write your tests, there is an order of execution of your setup and test methods ([Setup] and [Test]).
Are there others that you can use while testing and if so what is the execution order of those, such as a dispose or something? I saw test fixture setup, but not too familiar with that one.
Example:
When I run the test, it does the [Setup] first and then runs the [Test] when it goes to the next test it runs the [Setup] again and then goes to the [Test].
I am using NUnit if that helps.
Here is a truncated example of what I have setup:
using NUnit.Framework;
namespace TestingProject
{
[TestFixture]
public class CustomerService_Tests
{
public string MyAccount = string.Empty;
[SetUp]
public void Setup()
{
MyAccount = "This Account";
}
[Test]
public void Validate_That_Account_Is_Not_Empty()
{
Assert.That(!string.IsNullOrEmpty(MyAccount));
}
[Test]
public void Validate_That_Account_Is_Empty()
{
Assert.That(string.IsNullOrEmpty(MyAccount));
}
}
}
So, when I run the tests, it does the setup, and then the first test, then setup and then the 2nd test.
My question is what other types can I use while testing such as [Setup] and [Test] and what is the order of execution for these.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
对于每个包含测试的类(测试装置),您可以指定 4 个特殊方法。 方法的名称并不重要,但您需要使用以下四个属性之一来标记方法以便识别它们。
惯例要求您将方法与属性称为相同的名称,但正如我所说,属性是重要的一点。
请注意,我在这里描述的属性是在 NUnit 中找到的属性,但类似的属性(如果不相同)是在大多数单元测试框架中使用。
这些属性是:
前两个与整个类有关。 用
TestFixtureSetUp
属性标记的方法在类中的第一个测试之前运行一次。执行完类中的所有测试后,将执行一次使用
TestFixtureTearDown
属性标记的方法。您可以使用这两个来准备所有测试都相同的通用数据结构,并且不会被任何测试修改(这很重要)。
最后两个,
SetUp
和TearDown
,用于标记将在每个单独测试之前和之后运行的两个方法。标记有
SetUp
的方法在每次测试之前调用,标记有TearDown
的方法在每次测试后调用。 您可以使用它们来准备通用数据结构,尽管它们对于每个测试都是相同的,但它们会被部分或全部测试更改,因此最好为每个测试准备一个新的副本。将这些方法的执行布置为伪代码给了我们这样的顺序:
这些属性的使用完全是可选的。 您不需要先进行
SetUp
才能进行TearDown
,反之亦然。 它们只是您可能想要执行代码的点。For each class that you have tests in, a test fixture, you can specify 4 special methods. The names of the methods are not really important, but you need to tag the methods with one of the following four attributes in order to identify them.
Convention dictates that you call the methods the same as the attributes though, but as I said, the attributes is the important bit.
Note that the attributes I describe here are the ones found in NUnit, but similar attributes (if not the same) are in use in most unit test frameworks.
The attributes are:
The first two has to do with the class as a whole. The method tagged with the
TestFixtureSetUp
attribute is run once, before the first test in the class.After all the tests in the class has been executed, the method tagged with the
TestFixtureTearDown
attribute is executed, once.You can use these two to prepare common data structures that are the same for all the tests, and aren't modified by any tests (this is important).
The last two,
SetUp
andTearDown
, are used to tag two methods that will be run before, and after each individual test.The method tagged with
SetUp
is called before each test, and the method tagged withTearDown
is called after each test. You can use these to prepare common data structures, that though they are the same for each test, they will be changed by some or all of the tests, so it's best to prepare a new fresh copy for each test.Laying out the execution of these methods as pseudo-code gives us this order:
The usage of these attributes are entirely optional. You do not need to have a
SetUp
in order to have aTearDown
or vice versa. They're just points you might want to execute code at.使用 NUnit(不确定其他),您有以下执行顺序:
TestFixtureSetup
设置
Test
TearDown
设置
测试
TearDown
TestFixtureTearDown
每次运行测试时,它总是按该顺序执行。
如果您看一下下面的代码,您可以看到我正在谈论的内容的精确复制品。 您甚至可以复制并粘贴此代码,它应该可以工作(使用 NUnit,不确定它是否适用于其他代码)。
如果您在调试模式下运行它,并在每个方法上放置断点,您可以在调试时看到执行顺序。
Using NUnit (not sure about others) you have the following order of executions:
TestFixtureSetup
Setup
Test
TearDown
Setup
Test
TearDown
TestFixtureTearDown
Every time you run your tests it will always execute in that order.
If you take a look at the following code, you can see an exact replica of what I am talking about. You can even copy and paste this code and it should work (using NUnit, not sure if it will work with others).
If you run this in debug mode, and put a break point on each of the methods, you can see the order of execution while you debug.
NUnit 3.0 对单元测试属性进行了一些更改,如此处:
因此,执行顺序如下:
NUnit 3.0 has made some changes regarding unit tests attributes as indicated here:
So, execution order is the following:
查看 NUnit 文档。
右侧“属性”下的菜单描述了 [设置]、[测试] 以及开发测试时可以使用的其他属性。
Check out the NUnit documentation.
The menu down the right hand side under "Attributes" describes [Setup], [Test] and other attributes you can use when developing your tests.
请注意,最佳实践是保持单元测试用例彼此独立。 因此它们可以被理解、修改和独立运行。
有些人认为设置和拆卸也是一种不好的做法。
请参阅以下链接了解原因:
Be aware that the best practice is to keep the unit test cases independent of each other. So they can be understood, modified and run independently.
Some consider setup and teardown a bad practice as well.
See these links for the reasoning:
在 NUnit 2.5.1 中,执行顺序再次发生了变化。 我同意单元测试绝不能互相干扰。
In NUnit 2.5.1 the order of execution has changed once more. I agree that unittest must never interfere with each other.