What are Test Fixtures

Course Home | Code Example(s)

If we have some tests that have similar needs we can group them into a TestFixture. Or in simple terms, put them in the same class.

With the tests being in the same class, it means we can create methods in that class that all the tests can consume. NUnit has provided some helpful annotations to them with this. We haven’t covered annotations yet, but all text with put above methods and class that is inside [] are annotations. In this case, those annotations are [SetUp] and [TearDown].

[SetUp]

If we mark a method with the [SetUp] annotation, NUnit will call this method automatically for us before each Test is executed. This can be very useful in helping us set the state of the application ready for the Test. It could include configuring the application, creating test data, or configuring an object for the test to utilise such as a browser driver in Selenium. The clue is in the name, what do you need to do to be able to execute this test.

[TearDown]

That’s right, you guessed it. NUnit will automatically run this method at the end of every test. Here we could reset our application, delete/reset some data in our application or perhaps do some additional reporting, we have endless options.

using System;
using NUnit.Framework;

namespace CSharpBasics.nunitlessons
{
    [TestFixture]
    public class B_CreatingATestFixture
    {
        //This method will be called before each test
        [SetUp]
        public void SetUp()
        {
          //"TestContext.Progress.WriteLine" this is the same as Console.WriteLine however the test will do the outputting in real test time, instead of .NET
          TestContext.Progress.WriteLine("I'm doing something to setup the system ready for the test");
        }

        //This method will be called after each test
        [TearDown]
        public void Teardown()
        {
            TestContext.Progress.WriteLine("I'm doing something to tidy up after the test");
        }

        [Test]
        public void TestOne()
        {
            TestContext.Progress.WriteLine("I'm a test doing some stuff");
        }
    }
}
    

[OneTimeSetUp] and [OneTimeTearDown]

You can see where this is going right. [OneTimeSetUp] will run just once before any of the tests in the class. So if you run the whole class, the method annotated with [OneTimeSetUp] will get executed just once before the first test, and then never again. The same with [OneTimeTearDown], the method annotated with this will run once after all the tests are completed. Common usage for these annotations is opening and closing a database connection before all the tests. Or perhaps if doing some UI work, getting the application on a specific page ready for all the tests. Or could even be deploying a new instance of the application.

With regard to myself, I tend not to use the [OneTimeSetUp] and [OneTimeTearDown]. I tend to stick to before and after test, I like to keep my tests completely independent of each other.

Next Lesson ➔

Resources

https://github.com/nunit/docs/wiki/SetUp-and-TearDown-Changes
https://github.com/nunit/docs/wiki/OneTimeTearDown-Attribute
https://github.com/nunit/docs/wiki/SetUp-Attribute
https://github.com/nunit/docs/wiki/TearDown-Attribute
https://github.com/nunit/docs/wiki/OneTimeSetUp-Attribute
https://github.com/nunit/docs/wiki/TestFixture-Attribute
https://github.com/nunit/docs/wiki/SetUpFixture-Attribute

Richard Bradshaw's photo
Author

Richard Bradshaw

@FriendlyTester

Software Tester, speaker and trainer at Friendly Testing. BossBoss at Ministry of Testing. Whiteboard Testing creator. Striving to improve the testing craft.

Comments