25

Can we have a test set up method with arguments? I need a different set up for every test in a fixture.

Do we have something (or similar way) as the hypothetical idea :

[SetUp]
[Argument("value-1")]
[Argument("value-2")]
[Argument("value-3")]
public void InitializeTest(string value)
{
    //set env var with value
}
3
  • nunit.org/index.php?p=parameterizedTests&r=2.5.5 Commented Apr 29, 2013 at 10:25
  • 1
    It's not clear why you'd try to parameterize the setup rather than the test. Commented Apr 29, 2013 at 10:25
  • 1
    after this setup the tests do the same job. it probably makes more sense to take this bit into the test and make the test parametrized! Commented Apr 29, 2013 at 10:49

3 Answers 3

48

It can be done using the TestFixture attribute with a parameter.

If all the tests in the class depend on the same parameter this is the way to go.

The class will need a constructor with the same parameter passed to the TestFixture attribute.

See Parameterized Test Fixtures on https://github.com/nunit/docs/wiki/TestFixture-Attribute

[TestFixture("Oscar")]
[TestFixture("Paul")]
[TestFixture("Peter")]
public class NameTest
{
    private string _name;

    public NameTest(string name)
    {
        _name = name;
    }

    [Test]
    public void Test_something_that_depends_on_name()
    {
        //Todo...
    }

    [Test]
    public void Test_something_that_also_depends_on_name()
    {
        //Todo...
    }
    //...
}

This code is from the nunit documentation website:

[TestFixture("hello", "hello", "goodbye")]
[TestFixture("zip", "zip")]
[TestFixture(42, 42, 99)]
public class ParameterizedTestFixture
{
    private readonly string eq1;
    private readonly string eq2;
    private readonly string neq;

    public ParameterizedTestFixture(string eq1, string eq2, string neq)
    {
        this.eq1 = eq1;
        this.eq2 = eq2;
        this.neq = neq;
    }

    public ParameterizedTestFixture(string eq1, string eq2)
        : this(eq1, eq2, null)
    {
    }

    public ParameterizedTestFixture(int eq1, int eq2, int neq)
    {
        this.eq1 = eq1.ToString();
        this.eq2 = eq2.ToString();
        this.neq = neq.ToString();
    }

    [Test]
    public void TestEquality()
    {
        Assert.AreEqual(eq1, eq2);
        if (eq1 != null && eq2 != null)
            Assert.AreEqual(eq1.GetHashCode(), eq2.GetHashCode());
    }

    [Test]
    public void TestInequality()
    {
        Assert.AreNotEqual(eq1, neq);
        if (eq1 != null && neq != null)
            Assert.AreNotEqual(eq1.GetHashCode(), neq.GetHashCode());
    }
}
Sign up to request clarification or add additional context in comments.

Comments

24

Setup is executed once per each tests and for one tests there is only one SetUp and TearDown. You can call your Initialize method from tests explicitly and then create Data-Driven tests using TestCase attribute

public void InitializeTest(string value)
{
    //set env var with value
}

[TestCase("Value-1")]
[TestCase("Value-2")]
[TestCase("Value-3")]
public void Test(string value)
{
    InitializeTest(value);

    //Arange
    //Act
    //Assert
}

As result, you will have three tests each calling InitializeTest with different parameters

Comments

1

The setup method is used to do some preTest jobs, which would include preparing for the test like setting any values needed for the test to run, you can set these inside the setup method instead of providing the values as parameters.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.