Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# NUnit parameterized TestCaseSource value identification

Tags:

c#

nunit

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

I mean,

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

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

  • CheckMale
    • CheckMale(Person)
    • CheckMale(Person)
    • CheckMale(Person)

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.

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)

Thanks.

like image 460
ferpega Avatar asked Oct 19 '12 12:10

ferpega


People also ask

What do you mean by C?

C is a structured, procedural programming language that has been widely used both for operating systems and applications and that has had a wide following in the academic community. Many versions of UNIX-based operating systems are written in C.

Why is CA procedural language?

C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support.

Why is C language used?

The C programming language is the recommended language for creating embedded system drivers and applications. The availability of machine-level hardware APIs, as well as the presence of C compilers, dynamic memory allocation, and deterministic resource consumption, make this language the most popular.

Why is C called a mid level programming language?

C is called middle-level language because it actually binds the gap between a machine level language and high-level languages. A user can use c language to do System Programming (for writing operating systems) as well as Application Programming (for generating menu driven customer billing systems).


1 Answers

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 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: ReSharper test running results

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

like image 154
Ilya Ivanov Avatar answered Oct 01 '22 07:10

Ilya Ivanov