NUnit中的执行顺序是什么? [英] What is the order of execution in NUnit?

查看:51
本文介绍了NUnit中的执行顺序是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究测试驱动开发,发现它很酷.

I have been doing some research on test driven development and find it pretty cool.

我遇到的一件事是,当您编写测试时,设置和测试方法([Setup] 和 [Test])的执行顺序是一致的.

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.

示例:

当我运行测试时,它首先执行 [Setup],然后运行 ​​[Test],当它进入下一个测试时,它再次运行 [Setup],然后进入 [Test].

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].

如果有帮助,我正在使用 NUnit.

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.

我的问题是在测试时我可以使用哪些其他类型,例如 [Setup] 和 [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.

推荐答案

使用 NUnit(不确定其他)您有以下执行顺序:

Using NUnit (not sure about others) you have the following order of executions:

测试夹具设置

设置

测试

拆解

设置

测试

拆解

TestFixtureTearDown

TestFixtureTearDown

每次运行测试时,它始终会按该顺序执行.

Every time you run your tests it will always execute in that order.

如果你看一下下面的代码,你可以看到我所说的内容的精确复制品.您甚至可以复制并粘贴此代码,它应该可以工作(使用 NUnit,不确定它是否适用于其他人).

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.

using NUnit.Framework;

namespace Tester
{
    [TestFixture]
    public class Tester
    {
        public string RandomVariable = string.Empty;

        [TestFixtureSetUp]
        public void TestFixtureSetup()
        {
            //This gets executed first before anything else
            RandomVariable = "This was set in TestFixtureSetup";
        }

        [SetUp]
        public void Setup()
        {
            //This gets called before every test
            RandomVariable = "This was set in Setup";
        }

        [Test]
        public void MyTest1()
        {
            //This is your test...
            RandomVariable = "This was set in Test 1";
        }

        [Test]
        public void MyTest2()
        {
            //This is your test...
            RandomVariable = "This was set in Test 2";
        }

        [TearDown]
        public void TestTearDown()
        {
            //This gets executed after your test gets executed. 
            //Used to dispose of objects and such if needed
            RandomVariable = "This was set in TearDown";
        }

        [TestFixtureTearDown]
        public void TestFixtureTearDown()
        {
            //Executes Last after all tests have run.
            RandomVariable = "This was set in TestFixtureTearDown";

        }

    }
}

这篇关于NUnit中的执行顺序是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆