Pergunta

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("sdfdf@.com")]
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.

Foi útil?

Solução

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

Outras dicas

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"
          , "sdfdf@.com")] string invalidEmail) 
{ 
    ... 
} 
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top