What is the order of execution in NUnit?

后端 未结 6 611
滥情空心
滥情空心 2021-02-05 19:09

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 orde

6条回答
  •  半阙折子戏
    2021-02-05 19:50

    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.

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

提交回复
热议问题