When do I use the TestFixtureSetUp attribute instead of a default constructor?

C#Unit TestingNunit

C# Problem Overview


The NUnit documentation doesn't tell me when to use a method with a TestFixtureSetup and when to do the setup in the constructor.

public class MyTest
{
    private MyClass myClass;
    
    public MyTest()
    {
        myClass = new MyClass();
    }

    [TestFixtureSetUp]
    public void Init()
    {
        myClass = new MyClass();
    }
}

Are there any good/bad practices about the TestFixtureSetup versus default constructor or isn't there any difference?

C# Solutions


Solution 1 - C#

Why would you need to use a constructor in your test classes?

I use [SetUp] and [TearDown] marked methods for code to be executed before and after each test, and similarly [TestFixtureSetUp] and [TestFixtureTearDown] marked methods for code to be executed only once before and after all test in the fixture have been run.

I guess you could probably substitute the [TestFixtureSetUp] for a constructor (although I haven't tried), but this only seems to break from the clear convention that the marked methods provide.

Solution 2 - C#

I think this has been one of the issues that hasn't been addressed by the nUnit team. However, there is the excellent xUnit project that saw this exact issue and decided that constructors were a good thing to use on test fixture initialization.

For nunit, my best practice in this case has been to use the TestFixtureSetUp, TestFixtureTearDown, SetUp, and TearDown methods as described in the documentation.

I think it also helps me when I don't think of an nUnit test fixture as a normal class, even though you are defining it with that construct. I think of them as fixtures, and that gets me over the mental hurdle and allows me to overlook this issue.

Solution 3 - C#

One thing you can't do with [TestFixtureSetup] that you can do in the constructor is receive parameters from the [TestFixture] .

If you want to parameterise your test fixture, then you will have to use the constructor for at least some of the set-up. So far, I've only used this for integration tests, e.g. for testing a data access layer with multiple data providers:

[TestFixture("System.Data.SqlClient",
  "Server=(local)\\SQLEXPRESS;Initial Catalog=MyTestDatabase;Integrated Security=True;Pooling=False"))]
[TestFixture("System.Data.SQLite", "Data Source=MyTestDatabase.s3db")])]
internal class MyDataAccessLayerIntegrationTests
{
    MyDataAccessLayerIntegrationTests(
        string dataProvider,
        string connectionString)
    {
        ...
    }
}

Solution 4 - C#

There is difference between constructor and method marked with [TestFixtureSetUp] attribute. According to NUnit documentation:

> It is advisable that the constructor not have any side effects, since NUnit may construct the object multiple times in the course of a session.

So if you have any expensive initialization it is better to use TestFixtureSetUp.

Solution 5 - C#

I have often wondered what the need for [TestFixtureSetUp] was, given that there is a simple, well understood first class language construct that does exactly the same thing.

My preference is to use constructors, to take advantage of the readonly keyword ensuring member variables cannot be reinitialised.

Solution 6 - C#

[TestFixtureSetUp] and [TestFixtureTearDown] are for whole test class. runs only once.

[SetUp] and [TearDown] are for every test method(test). runs for every test.

Solution 7 - C#

An important difference between constructor and TestFixtureSetUp is that, in NUnit 2 at least, constructor code is actually executed on test enumeration, not just test running, so basically you want to limit ctor code to only populating readonly, i.e. parameter, values. Anything that causes side-effects or does any actual work needs to either be wrapped in a Lazy or done in the TestFixtureSetUp / OneTimeSetUp. So, you can think of the constructor as solely a place to configure the test. Whereas the TestFixtureSetUp is where the test fixture, the required initial state of the system before tests are run, is initialized.

Solution 8 - C#

I think I have a negative good answer - the reason to use a constructor instead of the attribute is when you have an inheritence between test classes.

Only one method annotated with [TestFixtureSetup] will be called (on the concrete class only), but the other fixture initializers will not. In this case I'd rather put the initialization in the constructor, which has a well-defined semantics for inheritance :)

Solution 9 - C#

The constructor and the SetUp methods are used differently:
The constructor is run only once.
However, the SetUp methods are run multiple times, before every test case is executed.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPacoView Question on Stackoverflow
Solution 1 - C#Sam WesselView Answer on Stackoverflow
Solution 2 - C#casademoraView Answer on Stackoverflow
Solution 3 - C#ErgwunView Answer on Stackoverflow
Solution 4 - C#oderibasView Answer on Stackoverflow
Solution 5 - C#David ChapmanView Answer on Stackoverflow
Solution 6 - C#Shankar SView Answer on Stackoverflow
Solution 7 - C#NovaterataView Answer on Stackoverflow
Solution 8 - C#ripper234View Answer on Stackoverflow
Solution 9 - C#nonexistent mythView Answer on Stackoverflow