Toto
Toto

Reputation: 7719

Run same tests with many different configuration

Is there an easy way to have a lot of unit tests to run several times with a different setup using NUnit. Say :

Currently I am inheriting a bases class that contains all the tests but it is not really scalable. What approach can work ? Is there something like TestCaseSource but at the class level ?

Current code :

public sealed class NoOptimization_Version0_Weekday_Tests : FeatureTestsBase
{
    protected override void SpecificSetup()
    {
        this.version = 0;
        this.withOptimizatin = false;
        this.dayOfWeek = DayOfWeek.Monday;
    }
}
public sealed class NoOptimization_Version1_Weekday_Tests : FeatureTestsBase
{
    protected override void SpecificSetup()
    {
        this.version = 1;
        this.withOptimizatin = false;
        this.dayOfWeek = DayOfWeek.Monday;
    }
}
public sealed class NoOptimization_Version1_Weekend_Tests : FeatureTestsBase
{
    protected override void SpecificSetup()
    {
        this.version = 1;
        this.withOptimizatin = false;
        this.dayOfWeek = DayOfWeek.Sunday;
    }
}

//...and many more specificSetups

[TestFixture]
public abstract class FeatureTestsBase
{
    protected int version;
    protected bool withOptimizatin;
    protected DayOfWeek dayOfWeek;

    public void SetUp()
    {
        SpecificSetup();
        //CommonSetup();
    }

    protected abstract void SpecificSetup();

    [Test]
    public void Test_00()
    {
        //...
    }

    //...

    [Test]
    public void Test_99()
    {
        //...
    }
}

Upvotes: 1

Views: 904

Answers (1)

Volodymyr Vedmedera
Volodymyr Vedmedera

Reputation: 114

You can provide parameters for your TestFixture. I'm not sure what you want to do with the provided values and so on, but this is a way to do this.

[TestFixture(1, false, DayOfWeek.Monday)]
[TestFixture(1, false, DayOfWeek.Sunday)]
class TestFixtureTests
{
    private readonly int version;
    private readonly bool withOptimization;
    private readonly DayOfWeek dayOfWeek;

    public TestFixtureTests(int version, bool withOptimization, DayOfWeek dayOfWeek)
    {
        this.version = version;
        this.withOptimization = withOptimization;
        this.dayOfWeek = dayOfWeek;
    }

    [Test]
    public void TestSomething()
    {
        Assert.That(dayOfWeek, Is.EqualTo(DayOfWeek.Monday).Or.EqualTo(DayOfWeek.Sunday));
    }
}

Upvotes: 3

Related Questions