简体   繁体   English

如何使用参数进行Nunit测试?

[英]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. 我想做一个[Test]来接收一个参数,并且不使用[TestCase]因为该参数可以使用多个值。 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. 该函数仅接收错误代码,如果错误代码不为0(发生问题),则断言。

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 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);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM