NUnit的TestCaseSource [英] NUnit TestCaseSource

查看:413
本文介绍了NUnit的TestCaseSource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在与TestCaseSource属性的走了。问题一:当SOURCENAME字符串无效,测试只是被忽略,而不是失败。如果源方法被改名,这将是非常糟糕的,在SOURCENAME字符串没有更新,然后你就失去了测试提供的覆盖。有没有办法改变的NUnit的行为,因此,如果SOURCENAME是无效的测试失败?


解决方案

这是解决在NUnit的2.6.2。没有为属性的新构造函数一个键入(必须实现的IEnumerable );它推荐用于优先于其他形式的,因为它不使用字符串来指定数据源,因此更容易重构。 (从文档。)



这确实需要您的测试运行,支持最新的NUnit



一个非常简单的例子(见上面的文档链接查看更多细节):



<预类=郎-CS prettyprint-覆盖> 公共类TestDataProvider:IEnumerable的
{
公众的IEnumerator的GetEnumerator()
{
返回新列表与所述; INT> {2,4,6} .GetEnumerator();
}
}

[的TestFixture]
公共类MyTests
{
[TestCaseSource(typeof运算(TestDataProvider))]
公共无效TestOne(INT数)
{
Assert.That(编号%2,Is.EqualTo(0));
}
}


I'm having a go with the TestCaseSource attribute. One problem: when the sourceName string is invalid, the test just gets ignored instead of failing. This would be really bad if the source method gets renamed, the sourceName string doesn't get updated, and then you lose the coverage that the test provided. Is there a way to change the behaviour of NUnit, so that the test fails if the sourceName is invalid?

解决方案

This is resolved in NUnit 2.6.2. There is a new constructor for the attribute that takes a Type (which must implement IEnumerable); it "is recommended for use in preference to other forms because it doesn't use a string to specify the data source and is therefore more easily refactored." (From the documentation.)

This does require that your test runner supports the latest NUnit.

A very basic example (see the above documentation link for more details):

public class TestDataProvider : IEnumerable
{
    public IEnumerator GetEnumerator()
    {
        return new List<int>{ 2, 4, 6 }.GetEnumerator();
    }
}

[TestFixture]
public class MyTests
{
    [TestCaseSource(typeof(TestDataProvider))]
    public void TestOne(int number)
    {
        Assert.That(number % 2, Is.EqualTo(0));
    }
}

这篇关于NUnit的TestCaseSource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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