Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MbUnit's row attribute in NUnit?

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.

like image 476
Mayowa Ayinde Avatar asked Sep 14 '10 07:09

Mayowa Ayinde


2 Answers

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

like image 200
Andy Morris Avatar answered Sep 17 '22 11:09

Andy Morris


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) 
{ 
    ... 
} 
like image 32
Lieven Keersmaekers Avatar answered Sep 19 '22 11:09

Lieven Keersmaekers