如何做一个带参数的NUnit测试? [英] How to do a Nunit test with parameters?

查看:746
本文介绍了如何做一个带参数的NUnit测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做一个 [测试]接收一个参数,而不是使用 [TestCase的] ,因为这参数可以有多个值。我似乎无法找到一个方法来做到这一点。

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.

下面是我想要做的:

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

本函数只是接收到错误的代码,如果它不是0(出现问题),断言。

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

推荐答案

要传递变量中使用的数据驱动测试

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



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

这篇关于如何做一个带参数的NUnit测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆