Search code examples
nunitautomated-testsfixturesmbunit

nunit/mbunit parameterized SetUp


I'd like to provide parameterized data for the setup of a test. Something like this:

[TestCase("1", "2")
[TestCase("a", "b")
public class TestFixture
{
    [SetUp]
    public void SetUp(string x, string y)
    {
        Console.WriteLine("Setup with {0}, {1}", x, y);
    }

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

But I can only manage to get parameters passed to the testcase itself.

I prefer solutions for either nunit or mbunit but open for alternatives (yet to settle on which test framework to use). This is ultimately for a set of Selenium system-tests where the setup creates a browser session.

[edit] Using NUnit I can get this working:

[TestFixture("1", "2")]
[TestFixture("a", "b")]
public class Tests
{
    private string _x;
    private string _y;

    public Tests(string x, string y)
    {
        _x = x;
        _y = y;
    }

    [SetUp]
    public void SetUp()
    {
        Console.WriteLine("Setup with {0}, {1}", _x, _y);
    }

    [Test]
    public void Test()
    {

    }
}

[/edit]

That seems to suit my needs, but I'll leave the question open for a few days to see if alternatives are suggested.


Solution

  • Using NUnit I can get this working:

    [TestFixture("1", "2")]
    [TestFixture("a", "b")]
    public class Tests
    {
        private string _x;
        private string _y;
    
        public Tests(string x, string y)
        {
            _x = x;
            _y = y;
        }
    
        [SetUp]
        public void SetUp()
        {
            Console.WriteLine("Setup with {0}, {1}", _x, _y);
        }
    
        [Test]
        public void Test()
        {
    
        }
    }