MbUnit's row attribute in NUnit?
Asked Answered
P

2

7

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.

Pentachlorophenol answered 14/9, 2010 at 7:30 Comment(0)
B
11

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

Borroff answered 14/9, 2010 at 10:55 Comment(1)
+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.Szeged
R
2

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) 
{ 
    ... 
} 
Raffinose answered 14/9, 2010 at 7:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.