C# 为什么我的 Visual Studio 单元测试中的每个测试都会触发 TestInitialize?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1873191/
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-06 21:31:25  来源:igfitidea点击:

Why does TestInitialize get fired for every test in my Visual Studio unit tests?

c#visual-studiovisual-studio-2010mstestvs-unit-testing-framework

提问by Pure.Krome

I'm using Visual Studio 2010 Beta 2. I've got a single [TestClass], which has a [TestInitialize], [TestCleanup]and a few [TestMethods].

我使用Visual Studio 2010 Beta 2中我有一个单一的[TestClass],其中有一个[TestInitialize][TestCleanup]和几个[TestMethods]

Every time a test method is run, the initialize and cleanup methods are ALSO run!

每次运行测试方法时,初始化和清理方法也会运行!

I was under the impression that the [TestInitialize]& [TestCleanup]should only be run once, per local test run.

我的印象是[TestInitialize]&[TestCleanup]应该只运行一次,每次本地测试运行。

Is that correct? If not, what is the proper way to do this?

那是对的吗?如果没有,这样做的正确方法是什么?

采纳答案by alexn

TestInitializeand TestCleanupare ran before and after each test, this is to ensure that no tests are coupled.

TestInitialize并且TestCleanup在每次测试之前和之后运行,这是为了确保没有测试耦合。

If you want to run methods before and after ALL tests, decorate relevant methods with the ClassInitializeand ClassCleanupattributes.

如果要在所有测试之前和之后运行方法,请使用ClassInitializeClassCleanup属性装饰相关方法。

Relevant information from the auto generated test-file in Visual Studio:

来自 Visual Studio 中自动生成的测试文件的相关信息:

You can use the following additional attributes as you write your tests:

您可以在编写测试时使用以下附加属性:

// Use ClassInitialize to run code before running the first test in the class
[ClassInitialize()]
public static void MyClassInitialize(TestContext testContext) { }

// Use ClassCleanup to run code after all tests in a class have run
[ClassCleanup()]
public static void MyClassCleanup() { }

// Use TestInitialize to run code before running each test 
[TestInitialize()]
public void MyTestInitialize() { }

// Use TestCleanup to run code after each test has run
[TestCleanup()]
public void MyTestCleanup() { }

回答by stijn

this is rather standard behaviour for test suites: setup and teardown before and after each test. The philosophy is that tests should not depend on each other. If you want another behaviour, you should probably use static objects that persist between each test.

这是测试套件的标准行为:在每次测试之前和之后进行设置和拆卸。其理念是测试不应该相互依赖。如果您想要另一种行为,您可能应该使用在每次测试之间持续存在的静态对象。

回答by Pushkar Prabhu

Methods that are marked with [TestInitialize()] attribute are used to prepare aspects of the environment in which your unit test will run. The purpose of this is to establish a known state for running your unit test. You may use the [TestInitialize()] method to copy, alter, or create certain data files that your test will use.

用 [TestInitialize()] 属性标记的方法用于准备单元测试将在其中运行的环境的各个方面。这样做的目的是为运行单元测试建立一个已知状态。您可以使用 [TestInitialize()] 方法来复制、更改或创建您的测试将使用的某些数据文件。

Create methods that are marked with [TestCleanUp{}] attribute to return the environment to a known state after a test has run. This might mean the deletion of files in folders or the return of a database to a known state. An example of this is to reset an inventory database to an initial state after testing a method that is used in an order-entry application.

创建标有 [TestCleanUp{}] 属性的方法,以在测试运行后将环境返回到已知状态。这可能意味着删除文件夹中的文件或将数据库恢复到已知状态。这方面的一个示例是在测试订单输入应用程序中使用的方法后将库存数据库重置为初始状态。

For more information please refer : http://msdn.microsoft.com/en-us/library/ms182517%28v=vs.100%29.aspx

更多信息请参考:http: //msdn.microsoft.com/en-us/library/ms182517%28v=vs.100%29.aspx

回答by Rodolpho Brock

Full example taken from the microsoft documentation:

取自微软文档的完整示例:

using Microsoft.VisualStudio.TestTools.UnitTesting;
using SampleClassLib;
using System;
using System.Windows.Forms;

namespace TestNamespace
{
    [TestClass()]
    public sealed class DivideClassTest
    {
        [AssemblyInitialize()]
        public static void AssemblyInit(TestContext context)
        {
            MessageBox.Show("AssemblyInit " + context.TestName);
        }

        [ClassInitialize()]
        public static void ClassInit(TestContext context)
        {
            MessageBox.Show("ClassInit " + context.TestName);
        }

        [TestInitialize()]
        public void Initialize()
        {
            MessageBox.Show("TestMethodInit");
        }

        [TestCleanup()]
        public void Cleanup()
        {
            MessageBox.Show("TestMethodCleanup");
        }

        [ClassCleanup()]
        public static void ClassCleanup()
        {
            MessageBox.Show("ClassCleanup");
        }

        [AssemblyCleanup()]
        public static void AssemblyCleanup()
        {
            MessageBox.Show("AssemblyCleanup");
        }

        [TestMethod()]
        [ExpectedException(typeof(System.DivideByZeroException))]
        public void DivideMethodTest()
        {
            DivideClass.DivideMethod(0);
        }
    }
}