NUnit使用TestCase需要更清晰的说明

3

我有一个这样的测试:

[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。

2个回答

1

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

错误信息有误导性,因为 type.ToString() 和 object.ToString() 的结果相同。

重写您的 myType.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);
    }

谢谢。我希望我能够返回对象(并检查类型),然后使用多个测试用例链接在一起,每个测试用例沿途执行其检查并将其结果返回给调用者。 - adamx97

0

我以前没有看到过像那样传递结果的情况。但是你不能只将结果作为另一个参数传递吗?

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

这可能很明显,但是期望值是:Expected: But was:,听起来非常像将“true”与true进行比较时得到的结果。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接