【问题标题】:NUnit use of TestCase clarity neededNUnit 使用 TestCase 需要清晰
【发布时间】:2010-12-17 02:21:16
【问题描述】:

我有一个这样的测试:

[TestCase(12, Result= typeof(mytype))]
public mytype GetById(int id)
{
yada, yada, yada.

}

in the NUnit error window, I see this:

Test.Tester.GetById(12):
  Expected: <mytype>
  But was:  <mytype>

我的问题是,这是预期的吗?当返回值是我自己的类型而不是整数、字符串等时,有没有办法指定返回值的类型?我在网上找到的所有示例都只返回字符串或整数。我是否需要实际生成一个 mytype 实例并说它是我所期望的?

这是 NUnit 2.5.9。

【问题讨论】:

    标签: .net nunit testcase


    【解决方案1】:

    Testcase Result=... 检查结果值而不是结果类型。

    错误消息具有误导性,因为 type.ToString() 和 object.ToString() 导致相同的消息

    覆盖您的 myTpe.ToString() 方法,错误消息将变为

     Expected: <mytype>
     But was:  {your ToString() result goes here}
    

    这些测试(nunit 2.5.7)按预期工作

        [TestCase(12, Result = "0")]
        public String GetById(int id)
        {
            return "0";
        }
    
        [TestCase(12, Result = typeof(mytype))]
        public System.Type GetByIdType(int id)
        {
            return typeof(mytype);
        }
    

    【讨论】:

    • 谢谢。我希望我可以真正返回对象(并检查类型),然后使用链接在一起的多个测试用例,每个测试用例都执行其检查并将其结果返回给调用者。
    【解决方案2】:

    我以前从未见过这样传递的结果。但是你不能把结果作为另一个参数传入吗?

    [TestCase(12, 1)] 
    public mytype GetById(int id, int result) 
    { 
       Assert.AreEqual(12, 1);
    } 
    

    它可能是在说明显而易见的,但预期的:但是是: 听起来很像您将“真”与“真”进行比较时得到的结果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多