2

I've got me a test method and a bunch of test cases as follows.

[TestCase(1, 2)]
[TestCase(3, 4)]
[TestCase(5, 6)]
public void Smack(int a, int b) { ... }

I've hidden the bunch of cases in a region but it doesn't feel right. I've tried with the other attributes listed at the official web page but didn't really got that to work (a bit confused on the proper way to approach it). Also, a colleague hinted that using the permutations and such can cause issues.

Is the below the best way to phrase a bunch of test cases or is there anything smoother and more pro?

#region Bunch
[TestCase(1, 2)]
[TestCase(3, 4)]
[TestCase(5, 6)]
#endregion
public void Smack(int a, int b) { ... }
Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438

1 Answers1

3

You might try using a TestCaseSource:

[TestCaseSource("SmackTestCases")]
public void Smack(int a, int b) { ... }

static object[] SmackTestCases =
{
    new object[] { 1, 2 },
    new object[] { 3, 4 },
    new object[] { 5, 6 } 
};

You could also implement this in a separate class, like this:

[TestFixture]
public class MyTests
{
    [Test]
    [TestCaseSource(typeof(SmackTestDataProvider), "TestCases")]
    public void Smack(int a, int b) { ... }
}

public class SmackTestDataProvider
{
    public static IEnumerable TestCases
    {
        get
        {
            yield return new TestCaseData( 1, 2 );
            yield return new TestCaseData( 3, 4 );
            yield return new TestCaseData( 5, 6 );
        }
    }  
}
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
  • I was trying to do something like that but I was stubborn on putting it in the same class. Perhaps that's the issue. Or I was just tired. How does it reflect on the orthogonality of the tests? In your example, we've got two separate classes and the test makes no sense if the other (with the yields) is missing. On the other hand we win on exchangeability... +1 for the clear syntax, anyway. – Konrad Viltersten Oct 01 '14 at 16:08
  • @KonradViltersten Yeah the fixture is dependent on the provider, but because you have to reference the provider type in the attribute, it won't compile if one is missing. Basically you can think of the two classes as a single unit: the fixture + provider is orthogonal to any other fixture + provider in your test library. Does that answer your question? – p.s.w.g Oct 01 '14 at 17:02
  • Yes. I'm storying the provider in a different class now but in the same file. We'll see if people around here will find it intuitive. +1 for the extra effort discussing. – Konrad Viltersten Oct 01 '14 at 21:16