What is the order of execution in NUnit?

后端 未结 6 535
滥情空心
滥情空心 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";
    
            }
    
        }
    }
    
    0 讨论(0)
  • 2021-02-05 19:53

    In NUnit 2.5.1 the order of execution has changed once more. I agree that unittest must never interfere with each other.

    0 讨论(0)
  • 2021-02-05 19:55

    Check out the NUnit documentation.

    The menu down the right hand side under "Attributes" describes [Setup], [Test] and other attributes you can use when developing your tests.

    0 讨论(0)
  • 2021-02-05 20:00

    For each class that you have tests in, a test fixture, you can specify 4 special methods. The names of the methods are not really important, but you need to tag the methods with one of the following four attributes in order to identify them.

    Convention dictates that you call the methods the same as the attributes though, but as I said, the attributes is the important bit.

    Note that the attributes I describe here are the ones found in NUnit, but similar attributes (if not the same) are in use in most unit test frameworks.

    The attributes are:

    • TestFixtureSetUp
    • TestFixtureTearDown
    • SetUp
    • TearDown

    The first two has to do with the class as a whole. The method tagged with the TestFixtureSetUp attribute is run once, before the first test in the class.

    After all the tests in the class has been executed, the method tagged with the TestFixtureTearDown attribute is executed, once.

    You can use these two to prepare common data structures that are the same for all the tests, and aren't modified by any tests (this is important).

    The last two, SetUp and TearDown, are used to tag two methods that will be run before, and after each individual test.

    The method tagged with SetUp is called before each test, and the method tagged with TearDown is called after each test. You can use these to prepare common data structures, that though they are the same for each test, they will be changed by some or all of the tests, so it's best to prepare a new fresh copy for each test.

    Laying out the execution of these methods as pseudo-code gives us this order:

    execute TestFixtureSetUp, if present
    for each test do
        execute SetUp, if present
        execute actual test
        execute TearDown, if present
    execute TestFixtureTearDown, if present
    

    The usage of these attributes are entirely optional. You do not need to have a SetUp in order to have a TearDown or vice versa. They're just points you might want to execute code at.

    0 讨论(0)
  • 2021-02-05 20:05

    Be aware that the best practice is to keep the unit test cases independent of each other. So they can be understood, modified and run independently.

    Some consider setup and teardown a bad practice as well. See these links for the reasoning:

    • http://jamesnewkirk.typepad.com/posts/2007/09/why-you-should-.html
    • http://www.agileprogrammer.com/dotnetguy/articles/SetupTeardown.aspx
    0 讨论(0)
  • 2021-02-05 20:11

    NUnit 3.0 has made some changes regarding unit tests attributes as indicated here:

    TestFixtureSetUpAttribute and TestFixtureTearDownAttribute continue to be supported as synonyms for OneTimeSetUpAttribute and OneTimeTearDownAttribute in test fixtures, but are deprecated.

    Since SetUpAttribute and TearDownAttribute are used in two different ways, it's not possible to simply deprecate their usage in SetUpFixture. They have been disallowed in that context, which is a breaking change.

    So, execution order is the following:

    • OneTimeSetUp
    • Setup
    • Test
    • TearDown
    • Setup
    • Test
    • TearDown
    • OneTimeTearDown
    0 讨论(0)
提交回复
热议问题