1
votes

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");
    }
  }
}
1

1 Answers

0
votes
  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.