0

For a method whose signature looks like this:

public T Add<T>(T first, T second) where T : struct, IEquatable<T>, IComparable<T>

which can work with all of the integral types, do I need to write tests for each of those, e.g.

public void Test_Add_Int()
{
    Assert.That(Add<int>(10, 20), Is.EqualTo(30));
}

public void Test_Add_UInt()
{
    Assert.That(Add<uint>(10, 20), Is.EqualTo(30));
}

public void Test_Add_Long()
{
    Assert.That(Add<long>(10, 20), Is.EqualTo(30));
}

...

or is it sufficient to only have Test_Add_Int?

  • 2
    Write enough tests to be confident that your code are working. If for this you need to write tests for every type - do it. If you feel confident with only tests for integer - that's good too ;) – Fabio May 24 '19 at 06:33
  • 1
    Possible duplicate of [Where is the line between unit testing application logic and distrusting language constructs?](https://softwareengineering.stackexchange.com/questions/322909/where-is-the-line-between-unit-testing-application-logic-and-distrusting-languag) – gnat May 24 '19 at 07:25
  • 1
    In addition to testing it with one or more integral types, you should also test it with your own `struct NonIntegralType : IEquatable, IComparable` to ensure that it handles a non-integral type that matches the generic constraints correctly. – David Arno May 24 '19 at 09:40
  • @DavidArno thank you, very important point! –  May 24 '19 at 10:23
  • There are infinitely many types acceptable to that `Add` – Caleth May 24 '19 at 15:09
  • @ThomasFlinkow Can we see the implementation of the method or is it a trivial example signature? –  May 25 '19 at 19:19

1 Answers1

2

Do I need to unit test a generic method with all accepted types?

It depends. The types accepted by a generic method are just as much an input parameter as the values that you pass as arguments and the same considerations for testing apply.

This means that if it is really important that your function works correctly for certain types, you should test with those types. If you believe that int and uint are similar enough that if the function works for one, you are confident that it will also work for the other, then you can choose to test with only one of the types. This consideration can also be made over ranges of the value inputs to the tested function.

For example, for your Add<> method, I would consider int, long and uint similar enough within the range of values that doesn't overflow in any of the types that I wouldn't duplicate the tests for multiple types, but I would add type-specific tests around the boundaries of int and uint.

Bart van Ingen Schenau
  • 71,712
  • 20
  • 110
  • 179