【问题标题】:MbUnit's row attribute in NUnit?NUnit中MbUnit的行属性?
【发布时间】:2011-04-12 00:50:40
【问题描述】:

在阅读使用 MbUnit 作为测试框架的 Asp.Net MVC 代码示例时,我发现可以使用 Row 属性针对多种输入可能性运行单个测试,如下所示:

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

我想知道是否存在与 MbUnit 的 Row 属性等效的 NUnit,或者在 NUnit 中实现这一点的优雅方式。谢谢。

【问题讨论】:

    标签: asp.net-mvc unit-testing tdd nunit mbunit


    【解决方案1】:

    我认为你在使用 TestCase 属性

    [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

    【讨论】:

    • +1 - TestCaseAttribute 在使用多个参数时比将ValuesAttributeSequentialAttribute 一起使用时会产生更易读的测试代码。如果我想使用SequentialAttribute 指定的组合以外的组合,我只会使用ValuesAttribute
    【解决方案2】:

    NUnits Sequential attribute 正是这样做的。

    SequentialAttribute 用于 测试指定 NUnit 应该 通过选择生成测试用例 为 测试参数,无 产生额外的组合。

    注意:如果参数数据由 多个属性,顺序在 哪个 NUnit 使用的数据项不是 保证。然而,它可以 预计将保持不变 给定的运行时和操作系统。

    示例下面的测试将是 执行3次,如下:

    MyTest(1, "A")
    我的测试(2,“B”) 我的测试(3,空)

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

    根据你的例子,这将变成

    [Test, Sequential] 
    public void IsValidEmail_Invalid_Emails_Should_Return_False(
      [Values("test@test_test.com"
              , "sdfdf dsfsdf"
              , "sdfdf@.com")] string invalidEmail) 
    { 
        ... 
    } 
    

    【讨论】:

      猜你喜欢
      • 2011-04-10
      • 2011-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-13
      • 2012-01-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多