Plunging into .NET Development

Weblog Pieter Gheysens
Microsoft .NET Development - C# - Enterprise Library - Visual Studio 2005 Team System - Compuware DevPartner - ...
 


Monday, April 25

NUnit-bug TestFixtureSetUp / TestFixtureTearDown ??

Currently I'm using version 2.2 of NUnit for unit-testing.

The two attributes [TestFixtureSetUp] and [TestFixtureTearDown] are used inside a TestFixture to provide a single set of functions that are performed once (TestFixtureSetUp) prior to executing any of the tests in the fixture and once after (TestFixtureTearDown) all tests are completed. A TestFixture can have only one TestFixtureSetUp method and only one TestFixtureTearDown method.

Today I noticed that this is not entirely correct when selecting individual unit-tests to run. I created a small test-project to be sure ...

using System;

 

using NUnit.Framework;

 

namespace UnitTests

{

    [TestFixture]

    public class MyTests

    {

        private int x = 5;

        private int y = 2;

 

        [TestFixtureSetUp]

        public void GeneralSetUp()

        {

            Console.WriteLine("TestFixtureSetup");

        }

 

        [TestFixtureTearDown]

        public void GeneralTearDown()

        {

            Console.WriteLine("TestFixtureTearDown");

        }

 

        [SetUp]

        public void TestMethodSetUp()

        {

            Console.WriteLine("TestMethodSetup");

        }

 

        [TearDown]

        public void TestMethodTearDown()

        {

            Console.WriteLine("TestMethodTearDown");

        }

 

        [Test]

        public void TestAdd()

        {

            Console.WriteLine("Add");

            Assert.AreEqual(7, x+y);

        }

 

        [Test]

        public void TestSubtract()

        {

            Console.WriteLine("Subtract");

            Assert.AreEqual(3, x-y);

        }

    }

}





After selecting and running the two tests (TestAdd and TestSubtract) together in the NUnit-GUI, I got the result above where you can see in the output that the TestFixtureSetup and TestFixtureTearDown methods were executed twice (once for each test) instead of once.

When the entire class is selected for unit-testing, then it works fine and those methods are only executed once while the TestMethodSetUp and TestMethodTearDown methods are executed for each individual test-method.



In my opinion, all unit-tests selected by a checkbox have to be considered as a whole and therefore the "TestFixture" methods may only be called once ... or do I miss something here?

3 Comments:

  • At 4:58 AM, Anonymous Martin Platt said…

    I also notice that if you double click on a particular test, it seems that the TextFixtureSetup and TestFixtureTearDown decorated methods don't run. I think that's the case, as when I run the class, they definitely do. I think I'm going to run the setup and teardown code in [Setup] and [TearDown] as it's at least reliable....

     
  • At 2:21 AM, Anonymous Anonymous said…

    Hi,

    Yes I am seeing this problem right now. I would expect when I select some test methods off a test class, TestFixtureSetup and TestFixtureTeardown get run once.

    Cheers,

    Mehrbod

     
  • At 9:27 AM, Blogger malcolm said…

    Hi ,Anyone have overcome this problem i am also facing the similar one.If anyone find it then pls post it out. :)...

     

Post a Comment

Links to this post:

Create a Link

<< Home