Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run a set of nUnit tests with two different setups?

(Sorry for the unclear title, please edit it if you can come up with a better one)

I wish to run the same tests over two different data stores, I can create the data stores in the Setup() method.

So should I have a super class that contains all the tests and an abstract SetUp() method, then have a subclass for each data store?

Or is there a better way?

See "Case insensitive string compare with linq-to-sql and linq-to-objects" for what I am testing.

like image 667
Ian Ringrose Avatar asked Mar 05 '10 17:03

Ian Ringrose


People also ask

Does SetUp run before every test NUnit?

The SetUp attribute is inherited from any base class. Therefore, if a base class has defined a SetUp method, that method will be called before each test method in the derived class.

What is SetUp and teardown in NUnit?

Setup methods (both types) are called on base classes first, then on derived classes. If any setup method throws an exception, no further setups are called. Teardown methods (again, both types) are called on derived classes first, then on the base class.

What is SetUp method in NUnit?

SetUpAttribute (NUnit 2.0 / 2.5)This attribute is used inside a TestFixture to provide a common set of functions that are performed just before each test method is called. It is also used inside a SetUpFixture, where it provides the same functionality at the level of a namespace or assembly.

What is difference between NUnit and MSTest?

MsTest is a native unit testing library that comes with Visual Studio from Microsoft. NUnit is an extra Nuget package that needs to be installed on top and interact with the APIs of Visual Studio. Nunit likely doesn't have direct integration into the native APIs like MsTest does.


1 Answers

A simple solution is this.

All your test-cases are in an abstract class for example in the TestBase-class. For example:

public abstract class TestBase
{
    protected string SetupMethodWas = "";

    [Test]
    public void ExampleTest()
    {
        Console.Out.WriteLine(SetupMethodWas);    
    }

    // other test-cases
}

Then you create two sub-classes for each setup. So each sub-class will be run a individual with it-setup method and also all inherited test-methods.

[TestFixture]
class TestA : TestBase
{
    [SetUp]
    public void Setup()
    {
        SetupMethodWas = "SetupOf-A";    
    }
}
[TestFixture]
class TestB : TestBase
{
    [SetUp]
    public void Setup()
    {
        SetupMethodWas = "TestB";
    }
}

This works wonderful. However for simpler tests parameterized tests are a better solution

like image 94
Gamlor Avatar answered Nov 15 '22 11:11

Gamlor