Visual Studio单元测试:SetUp和TearDown

时间:2021-10-01 02:43:59

Instead of [SetUp] and [TearDown] in Nunit what is the alternative in Visual Studio Ultimate 2010 Unit Testing. In Nunit you can imagine setup and teardown methods are as constructors and destructors for the tests in our class.

而不是Nunit中的[SetUp]和[TearDown],Visual Studio Ultimate 2010单元测试中的替代方案是什么。在Nunit中,你可以想象setup和teardown方法是我们班级测试的构造函数和析构函数。

2 个解决方案

#1


110  

A method annotated with [TestInitialize] is run before each test. Likewise [TestCleanup] is after each test.

在每次测试之前运行使用[TestInitialize]注释的方法。同样[TestCleanup]是在每次测试之后。

[ClassInitialize] and [ClassCleanup] are run before and after the 'suite' of tests inside the TestClass.

[ClassInitialize]和[ClassCleanup]在TestClass中的'套件'测试之前和之后运行。

#2


2  

Visual Studio will use MSTest, that is Microsoft's unit testing framework, it is similar to NUnit. In fact, most of them are similar in concepts but different syntax.

Visual Studio将使用MSTest,即Microsoft的单元测试框架,它类似于NUnit。实际上,它们中的大多数在概念上相似但语法不同。

In order to view the comparsion, the creators of xUnit (another unit testing framework) have a list here:

为了查看比较,xUnit(另一个单元测试框架)的创建者在这里有一个列表:

http://xunit.github.io/docs/comparisons.html

http://xunit.github.io/docs/comparisons.html

More specifically what you asked for, TestInitialize is MSTest's equivalent to NUnit's Setup, and the same for TestCleanup and TearDown.

更具体地说,你要求的是,TestInitialize是MSTest相当于NUnit的Setup,而TestCleanup和TearDown相同。

One thing to note, I would stay with NUnit if you are attempting to do automated unit testing or some kind of continous integration. The main reason the various *unit frameworks are favoured over MSTest is because you must have a copy of Visual Studio on the machine you are running the tests on. Fine for your own local machine, different story for a CI server. Visual Studio is a pig of a program, and to install it on a server (which is generally supposed to be as lightweight and fast as it can be), just to run tests is a bit annoying.

有一点需要注意,如果您尝试进行自动化单元测试或某种连续集成,我会留在NUnit中。各种*单元框架比MSTest更受青睐的主要原因是因为您必须在运行测试的计算机上拥有Visual Studio的副本。适用于您自己的本地计算机,CI服务器的不同故事。 Visual Studio是一个程序的猪,并将其安装在服务器上(通常应该尽可能轻量级和快速),只是运行测试有点烦人。

#1


110  

A method annotated with [TestInitialize] is run before each test. Likewise [TestCleanup] is after each test.

在每次测试之前运行使用[TestInitialize]注释的方法。同样[TestCleanup]是在每次测试之后。

[ClassInitialize] and [ClassCleanup] are run before and after the 'suite' of tests inside the TestClass.

[ClassInitialize]和[ClassCleanup]在TestClass中的'套件'测试之前和之后运行。

#2


2  

Visual Studio will use MSTest, that is Microsoft's unit testing framework, it is similar to NUnit. In fact, most of them are similar in concepts but different syntax.

Visual Studio将使用MSTest,即Microsoft的单元测试框架,它类似于NUnit。实际上,它们中的大多数在概念上相似但语法不同。

In order to view the comparsion, the creators of xUnit (another unit testing framework) have a list here:

为了查看比较,xUnit(另一个单元测试框架)的创建者在这里有一个列表:

http://xunit.github.io/docs/comparisons.html

http://xunit.github.io/docs/comparisons.html

More specifically what you asked for, TestInitialize is MSTest's equivalent to NUnit's Setup, and the same for TestCleanup and TearDown.

更具体地说,你要求的是,TestInitialize是MSTest相当于NUnit的Setup,而TestCleanup和TearDown相同。

One thing to note, I would stay with NUnit if you are attempting to do automated unit testing or some kind of continous integration. The main reason the various *unit frameworks are favoured over MSTest is because you must have a copy of Visual Studio on the machine you are running the tests on. Fine for your own local machine, different story for a CI server. Visual Studio is a pig of a program, and to install it on a server (which is generally supposed to be as lightweight and fast as it can be), just to run tests is a bit annoying.

有一点需要注意,如果您尝试进行自动化单元测试或某种连续集成,我会留在NUnit中。各种*单元框架比MSTest更受青睐的主要原因是因为您必须在运行测试的计算机上拥有Visual Studio的副本。适用于您自己的本地计算机,CI服务器的不同故事。 Visual Studio是一个程序的猪,并将其安装在服务器上(通常应该尽可能轻量级和快速),只是运行测试有点烦人。