Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NUnit - Fails with TestFixtureSetUp method not allowed on a SetUpFixture

Tags:

c#

nunit

I am trying to re-organise some Integration Tests we have so that they use a common class for creating a Database and the Data required in the Database to Test against in other classes in the same assembly using [SetUpFixture] NUnit attribute.

I have :

namespace Tests;

public class TestBaseClass : SolutionBaseClass
{
    public void Setup()
    {
        base.CreateDatabase();
        base.CreateData();
    }

    public void Teardown()
    {
        base.DestroyDatabase();
    }
}

[SetUpFixture]
public class Setup : TestBaseClass
{
    [SetUp]
    public void Setup()
    {
        base.Setup();
    }

    [TearDown]
    public void Teardown()
    {
        base.Teardown();
    }
}

then individual test fixture classes:

namespace Tests.Services;

[TestFixture]
public class LibraryTest : TestBaseClass
{
    [TestFixtureSetUp]
    public void SetupTests()
    {
        // I know am calling the same Setup twice once from SetUpFixture and TestFixture, 
        // I have handled it so that only one Database/Data gets set up once (for safety mostly!)
        base.SetUp();

        // Other class initialisations.
    }
}

Any ideas what I am doing wrong, I figure it is a problem with the inheritance model being used, as you can tell I am inheriting this from someone else!!

Thanks.

like image 637
Chris Usher Avatar asked Aug 20 '14 09:08

Chris Usher


2 Answers

In NUnit 3 one should use OneTimeSetUpAttribute and OneTimeTearDownAttribute on the static methods of the [SetUpFixture] class. Source: http://bartwullems.blogspot.nl/2015/12/upgrading-to-nunit-30-onetimesetup.html

In NUnit 2.0

[SetUpFixture]
class TestHost
{
    [SetUp]
    public static void AssemblyInitalize()
    {
      //Global initialization logic here
    }
}

In NUnit 3.0

[SetUpFixture]
class TestHost
{
    [OneTimeSetUp]
    public static void AssemblyInitalize()
    {
      //Global initialization logic here
    }
}
like image 157
Denis Gladkiy Avatar answered Jan 01 '23 17:01

Denis Gladkiy


In NUnit 3.0 TestFixtureSetUp and TestFixtureTearDown has been renamed to OneTimeSetUp and OneTimeTearDown.

Here is the documentation link for above changes:

  • SetUp and TearDown Changes
like image 42
Ihtsham Minhas Avatar answered Jan 01 '23 19:01

Ihtsham Minhas