Home > asp.net, Unit Testing > TestCaseSource in Nunit 2.5

TestCaseSource in Nunit 2.5

Recently, one my team mates discovered the TestCaseSource attribute in Nunit 2.5. I have been using the TestCase attribute for a little while. It enables you to tes multiple inputs with one test:

[TestCase(12,3,4)]
[TestCase(12,2,6)]
[TestCase(12,4,3)]
public void DivideTest(int n, int d, int q)
{
  Assert.AreEqual( q, n / d );
}

This is useful for various reasons, mainly that you don’t have to write a separate test for each input. TestCaseSource is a different way of doing this by allowing the user to build up an IEnumerable of test cases and just add that to the test.


[Test, TestCaseSource("DivideCases")]
public void DivideTest(int n, int d, int q)
{
  Assert.AreEqual( q, n / d );
}

static object[] DivideCases =
{
  new object[] { 12, 3, 4 },
  new object[] { 12, 2, 6 },
  new object[] { 12, 4, 3 }
};

This is helpful as you won’t have a load of testcase attributes at the top of each test, and you could reuse this data. Lovely!

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: