views:

88

answers:

2

While reading an Asp.Net MVC code sample that used MbUnit as it's testing framework, I saw that it was possible to run a single test against multiple input possibilities by using a Row attribute, like so:

[Test]
[Row("test@test_test.com")]
[Row("sdfdf dsfsdf")]
[Row("[email protected]")]
public void Invalid_Emails_Should_Return_False(string invalidEmail)
{
    ...
}

Please I'd like to know if there is an NUnit equivalent of MbUnit's Row attribute , or otherwise an elegant way to achieve this in NUnit. Thanks.

+2  A: 

NUnits Sequential attribute does exactly that.

The SequentialAttribute is used on a test to specify that NUnit should generate test cases by selecting individual data items provided for the parameters of the test, without generating additional combinations.

Note: If parameter data is provided by multiple attributes, the order in which NUnit uses the data items is not guaranteed. However, it can be expected to remain constant for a given runtime and operating system.

Example The following test will be executed three times, as follows:

MyTest(1, "A")
MyTest(2, "B") MyTest(3, null)

 [Test, Sequential]
 public void MyTest(
     [Values(1,2,3)] int x,
     [Values("A","B")] string s) 
 {
     ... 
 }

Given your example, this would become

[Test, Sequential] 
public void IsValidEmail_Invalid_Emails_Should_Return_False(
  [Values("test@test_test.com"
          , "sdfdf dsfsdf"
          , "[email protected]")] string invalidEmail) 
{ 
    ... 
} 
Lieven
+6  A: 

I think you're after the TestCase attribute

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

http://www.nunit.com/index.php?p=testCase&r=2.5.7

Andy Morris
+1 - `TestCaseAttribute` leads to much more readable test code when using multiple parameters than using `ValuesAttribute` with `SequentialAttribute`. I'd only use `ValuesAttribute` if I wanted to use some combination other than the one specified by `SequentialAttribute`.
Alex Humphrey
+1 what he said...
Lieven