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

问题描述

我使用NUnit的2.6.1 TestCaseSource来测试不同的对象类的构造函数参数相同的断言。

I am using TestCaseSource on NUnit 2.6.1 to test the same Asserts with different object class constructor parameters.

我的意思是,

[Test, TestCaseSource("myConstructorsForMale")}
public void CheckMale(Person p) 
{
     Assert.That(p.IsMale);
}

static Person[] myConstructorsForMale = 
                     {
                         new Person("John"),
                         new Person(isMale=true),
                         new Person("Doe")
                     };



好吧,一切都乳宁精,但是这是我在NUnit的控制台收到的结果:

Ok, all is runing fine, but this is the result I received on the NUnit Console:


  • CheckMale

    • CheckMale(人)

    • CheckMale(人)

    • CheckMale(人)

    所以,我不知道什么是对每个迭代和执行的测试,如果其中任何一个失败,我不能得到的是失败的测试。

    So I don't know what is the test executed on every iteration and if any of them fail I cannot get what is the failing test.

    我的问题是:
    是否有任何的方法来确定什么是参数传递到测试的评论或类似的东西? (以TestCaseSource属性的方式做)

    My question is:Are there any way to identify with a comment or something similar what is the parameter being passed to the test ? (in TestCaseSource Attribute way of do)

    感谢。

    推荐答案

    如果使用的情况下,'本土'NUnitor 作为测试亚军您可以覆盖的ToString 方法,让你有好人的定义。
    为例,你的测试代码可能看起来像:

    If case of using 'native' NUnitor ReSharper as a test runner you can override ToString method so that you have good Person definitions. For example, your testing code could look like:

    public class PersonTests
    {
        [Test, TestCaseSource("myConstructorsForMale")]
        public void CheckMale(Person p)
        {
            Assert.That(p.IsMale);
        }
    
        static Person[] myConstructorsForMale = 
                     {
                         new Person("John"),
                         new Person{IsMale=true},
                         new Person("Doe")
                     };
    }
    



    Person类可能是这样的:

    Person class could be like:

    public class Person
    {
        public Person(string name)
        {
            this.Name = name;
        }
    
        public Person() { }
    
        public string Name { get; set; }
        public bool IsMale { get; set; }
    
        public override string ToString()
        {
            return string.Format("Name:{0};IsMale:{1}", Name, IsMale);
        }
    }
    

    结果窗口将显示如下:

    The result window will look like this:

    我也检查了它对本地NUnit的测试运行,你可能使用。它还显示人很好:

    I also checked it on native NUnit test runner, which you probably use. It also displays Persons nicely:

    这篇关于C#NUnit的参数TestCaseSource价值认同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 15:53