Define the same setup and teardown for every test fixture

Viewed 785

I have a project with several TestFixture and I want to define the same [SetUp] [TearDown] methods for every test. Considering I have many TestFixture I want to avoid editing all my files to add the two instructions.

Browsing the nunit documentation I thought [SetUpFixture] was the perfect solution. So I copied the example and tried to run it but it seems my TearDown method is never being runned whereas the SetUp is executing as I would expect. To assert this I simply used throw statements.

My questions are :

  1. Am I wrong with my use case ? Should I do things differently to reach my goal ?
  2. Am I making an incorrect usage of SetUpFixture attribute ?

Below is my SetUpFixture class. I am using Nunit 3.11.

using System;
using NUnit.Framework;

namespace MyTestProject
{
  [SetUpFixture]
  public class MySetUpClass
  {
    [OneTimeSetUp]
    public void RunBeforeAnyTests()
    {
      StaticClass.Init();
//    throw new InvalidOperationException("SetUp reached");
    }

    [OneTimeTearDown]
    public void RunAfterAnyTests()
    {
      StaticClass.Finalize();
//    throw new InvalidOperationException("TearDown reached");
    }
  }
}
2 Answers

I have created sample program to predict health condition based on the Blood Pressure level of the patient and sample unit test is implemented (NUnit Test). I have implemented sample unit testing with [SetUp] and [Tear Down] for each test method for better understanding.

Main class :


     public class HealthReport
    {
        public string Healthdamage(int pressure)
        {
            if (pressure == 120)
            {
                return "normal";
            }
            if (pressure > 120 && pressure < 140)
            {
                return "At risk";
            }
            if (pressure > 140)
            {
                return "Highly risk";
            }
            else
            {
                return "low BP";
            }
        }
    }

Unit testing class:

    [TestFixture]
    public class Healthtest
    {
        private HealthReport health;
        [SetUp]
        public void SetUp()
        {
            health = new HealthReport();
        }
        [TearDown]
        public void Teardown()
        {
            health = null;
        }

        [Test]
        public void Health_Damage_returnnormal()
        {
            string statusreport = health.Healthdamage(120);
            Assert.That(statusreport, Is.EqualTo("normal"));
        }
        [Test]
        public void Health_Damage_returnAtrisk()
        {
            string statusreport = health.Healthdamage(130);
            Assert.That(statusreport, Is.EqualTo("At risk"));
        }
        [Test]
        public void Health_Damage_returnHighlyrisk()
        {
            string statusreport = health.Healthdamage(180);
            Assert.That(statusreport, Is.EqualTo("Highly risk"));
        }
        [Test]
        public void Health_Damage_returnlow()
        {
            string statusreport = health.Healthdamage(110);
            Assert.That(statusreport, Is.EqualTo("low BP"));
        }
  1. Your use-case is fine, you can do this by making a base class with the [Setup] and [Teardown] defined. If you inherit they will run once per test. It is documented at https://github.com/nunit/docs/wiki/SetUp-and-TearDown.

  2. The help doesn't seem clear to me nor is your explanation of your results, but I'd recommend trying some breakpoints or logging prior to your StaticClass calls to be sure that they're not interfering with your behaviour.

Related