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

发布于 2024-08-14 00:48:20 字数 319 浏览 2 评论 0原文

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

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

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

这是正确的吗?如果不是,那么正确的方法是什么?

I'm using Visual Studio 2010 Beta 2. I've got a single [TestClass], which has a [TestInitialize], [TestCleanup] and a few [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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

ペ泪落弦音 2024-08-21 00:48:20

TestInitializeTestCleanup 在它们定义的同一类中的每个测试之前和之后运行,这是为了确保没有测试与任何其他测试耦合在同一个班级进行测试,并始终在干净的状态下开始每次测试。


如果您想在特定类中的所有测试之前和之后运行方法,请使用ClassInitializeClassCleanup属性修饰相关方法。

如果要在特定测试项目(程序集)中的所有测试之前和之后运行方法,请使用 AssemblyInitializeAssemblyCleanup 属性修饰相关方法。

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

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

// 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() { }

TestInitialize and TestCleanup are ran before and after every test in the same class they are defined, this to ensure that no test is coupled to any other test in the same one class and to always start each test in a clean state.


If you want to run methods before and after all tests in a particular class, decorate relevant methods with the ClassInitialize and ClassCleanup attributes.

If you want to run methods before and after all tests in a particular test project (assembly), decorate relevant methods with the AssemblyInitialize and AssemblyCleanup attributes.

Relevant information from the auto generated test-file in 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() { }
惜醉颜 2024-08-21 00:48:20

完整示例取自微软文档:

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);
        }
    }
}

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);
        }
    }
}
一笔一画续写前缘 2024-08-21 00:48:20

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

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.

走野 2024-08-21 00:48:20

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

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

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

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.

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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文