Click here to Skip to main content
15,900,461 members
Articles / Programming Languages / C#
Tip/Trick

How to Use SetUpFixture in NUnit

Rate me:
Please Sign up or sign in to vote.
4.80/5 (2 votes)
19 Jun 2013CPOL2 min read 41.5K   3   2
This tip discusses how to use SetUpFixture in NUnit.

Introduction

From the NUnit website, we got the explanation for SetUpFixture as:

Reference start------------------------------------------------------------------------------

This is the attribute that marks a class that contains the one-time setup or teardown methods for all the test fixtures under a given namespace.

The SetUp method in a SetUpFixture is executed once before any of the fixtures contained in its namespace. The TearDown method is executed once after all the fixtures have completed execution.

Reference end--------------------------------------------------------------------------------------

But what is exactly the "under a given namespace" means? And how to make it work?

Background

Sometimes, we developed a library that can be reused in the application. Such as inject some logging statement when the function gets called. It works well in the function code. But during the unit test, we don't want to waste time on running those statements. So we can add the Flag in the library to bypass the code injection when the unit test is running.

Using the Code

The code for the library can be like this:

C#
namespace MyLibrary
{
    public class Program
    {
        public static bool Flag;

        public void Function()
        {
            if (Flag) return;
            DoJob();
        }

        private void DoJob()
        {
        }
    }
} 

So in unit test setup, we can set the Flag to true. The unit test will bypass it.

C#
using MyLibrary;
using NUnit.Framework;

namespace MySetup.New
{
    [TestFixture]
    public class PrgramTest
    {
        [SetUp]
        public void Init()
        {
            Program.Flag = true;
        }

        [Test]
        public void Test()
        {
            Assert.IsTrue(Program.Flag);
        }
    }
}

In the case, we have many TestFixtures, the SetUpFixture can be useful. We can create a new class and mark in with SetUpFixture. In this case, the Flag will be set for all TestFixtures.

C#
using MyLibrary;
using NUnit.Framework;

namespace MySetup
{
    [SetUpFixture]
    public class MySetup
    {
        [SetUp]
        public void Init()
        {
            Program.Flag = true;
        }
    }
}

In this case, we don't have to set the flag in each TestFixture's SetUp function.

C#
 using MyLibrary;
using NUnit.Framework;

namespace MySetup.New
{
    [TestFixture]
    public class PrgramTest
    {
        [Test]
        public void Test()
        {
            Assert.IsTrue(Program.Flag);
        }
    }
}

So how does the namespace matter? We need to pay attention to the namespace. The sample I posted before is working since the class marked by SetUpFixture and the class marked by TextFixture are under the same namespace. If I change the class marked by TextFixture to another namespace, it won't work.

C#
using MyLibrary;
using NUnit.Framework;

namespace My.New
{
    [TestFixture]
    public class PrgramTest
    {
        [Test]
        public void Test()
        {
            Assert.IsTrue(Program.Flag);
        }
    }
}

Now what we tried is that both the class marked by SetUpFixture and the class marked by TextFixture are in the same project. But how about the case that in our solution, we have many unit test projects and we want to apply this unit test Flag to all of them. Let's say we build the solution like this:

In the new test case, we put it under the same namespace with the class marked by SetUpFixture.

C#
 using MyLibrary;
using NUnit.Framework;

namespace MySetup.Test
{
    [TestFixture]
    public class NewPrgramTest
    {
        [Test]
        public void Test()
        {
            Assert.IsTrue(Program.Flag);
        }
    }
}

But this time, it does not work. The Flag is still false although they are under the same namespace.

There is a workaround for this issue. We can add the Setup file as a link file in the new project. It is working.

Points of Interest

In this tip, we explore how the SetUpFixture can be used in Unit Testing and also provide a workaround when we want to apply it to solution-wise in the Visual Studio.

History

  • 1st version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Singapore Singapore
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralDon't change the code under test when testing it Pin
John Brett24-Jun-13 0:00
John Brett24-Jun-13 0:00 
GeneralRe: Don't change the code under test when testing it Pin
chain12347-Jul-13 21:06
chain12347-Jul-13 21:06 
Thanks for your suggestion.

You are right. The sample I posted is for demonstration purpose. we should avoid changing the function code for unit test purpose.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.