Cher
Cher

Reputation: 2947

How to do a Nunit test with parameters?

I want to do a [Test] which receives a parameter, and not using [TestCase] as this parameter can take multiple values. I can't seem to find a way to do this.

Here is what I'd like to do:

    [Test]
    static public void NUnitWriter(int errorCode)
    {
        Assert.AreEqual (0, errorCode);
    }

This function just receives an error code and if it's not 0 (a problem occurred), assert.

Upvotes: 1

Views: 394

Answers (1)

lloyd
lloyd

Reputation: 1811

To pass in variables use Data driven tests

[DataSource(@"Provider=Microsoft.SqlServerCe.Client.4.0; Data Source=C:\Data\MathsData.sdf;", "Numbers")]
[Test]
static public void NUnitWriter()
{
    int x = 0
    int errorCode = Convert.ToInt32(TestContext.DataRow["ErrorCode"]);
    Assert.AreEqual (x, errorCode);
}  

Passing in from Xml

    [DataSource("Table:CSharpDataDrivenTests.xml#FirstTable")]
    [Test]
    static public void NUnitWriter()
    {
        int x = 0
        int errorCode = Convert.ToInt32(TestContext.DataRow["ErrorCode"]);
        Assert.AreEqual (x, errorCode);
    }

Upvotes: 5

Related Questions