简体   繁体   中英

Unit Test for Generic function in C#

Seems to be a popular question but I haven't found the answer yet, so

To be short, I have a generic function that I need to perform unit test on it, say

public void T[] DoSomething<T>(T input1, T input2)

Now I need to test if this function works correctly for int, ArrayList, how can I write unit tests in this case, listing all the cases for T is not an option, I'm thinking of just test int and some class instance ?

I also try to use the auto generated unit test from VS2012, something looks like :

public void DoSomethingHelper<T>() {
    T item1 = default(T);; // TODO: Initialize to an appropriate value
    T item2 = default(T); // TODO: Initialize to an appropriate value
    T[] expected = null; // TODO: Initialize to an appropriate value
    T[] actual = SomeClass.DoSomething<T>(item1, item2);
    Assert.AreEqual(expected, actual);
    Assert.Inconclusive("Verify the correctness of this test method.");
}
[TestMethod()]
public void AddTest() {
    AddTestHelper<GenericParameterHelper>();
}

which is even more confusing to me, what should i put in DoSomethingHelper to initialize variables ? an int, a string or something ?

Can anyone help ? I've heard of Pex and others, but still no one provided me a sample unit test code for this simple function.

You might want to check NUnit's Generic Test Fixtures , for testing with multiple implementations of T

First of all, think about the following: Why are you creating that function generic?

If you are writing a generic function/method, it shouldn't care about the implementation of the type it is working with. I mean, no more than what you specify in the generic class (eg. where T : IComparable<T>, new() , etc.)

So, my suggestion is that you create a dummy class that will fit the requirements of the generic type, and test with it. An example using NUnit :

class Sample {
    //code here that will meet the requirements of T
    //(eg. implement IComparable<T>, etc.)
}

[TestFixture]
class Tests {

    [Test]
    public void DoSomething() {
        var sample1 = new Sample();
        var sample2 = new Sample();
        Sample[] result = DoSomething<Sample>(sample1, sample2);

        Assert.AreEqual(2, result.Length);
        Assert.AreEqual(result[0], sample1);
        Assert.AreEqual(result[1], sample2);
    }
}

Edit: Think about it, and you'll see it works. You might think: " Well, but what if the body of DoSomething has something like... ":

if (item1 is int) {
    //do something silly here...
}

of course it will fail when testing it with int , and you won't notice it since you're testing with the Sample class, but think of it as if you were testing a function that sums two numbers, and you had something like:

if (x == 18374) {
    //do something silly here...
}

You wouldn't identify it either.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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