C# 如何在 NUnit 2.5 中使用 TestCase?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8869734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 04:53:26  来源:igfitidea点击:

How to use TestCase in NUnit 2.5?

c#unit-testingnunittestcase

提问by Mark Allison

I have a Currencyclass which I persist to my database using NHibernate. Currencyclass looks like this:

我有一个Currency类,我使用 NHibernate 将其保存到我的数据库中。Currency类看起来像这样:

public class Currency : Entity
{
    public virtual string Code { get; set; }
    public virtual string Name { get; set; }
    public virtual string Symbol { get; set; }        
}

I have written a unit test using [TestCase]like this:

我已经使用[TestCase]这样的方式编写了一个单元测试:

    [TestCase(6,Result = new Currency ({ Code="GBP", Name="British Pound", Symbol="£"}))]
    public Currency CanGetCurrencyById(int id)
    {
        ICurrencyRepo currencies = new RepoFactory().CreateCurrencyRepo(_session);
        Currency c = currencies.GetById<Currency>(id);

        return c;
    }

I know this is wrong but I'm not sure how to write it. Can the result be an object?

我知道这是错误的,但我不知道如何写。结果可以是object?

采纳答案by k.m

Attribute argument (for Result) must be a constant expression. You can't create objects like you do now.

属性参数 (for Result) 必须是常量表达式。您不能像现在一样创建对象。

Using TestCaseattribute is good for testing cases where you need to verify multiple simple inputs/outputs. In your scenarion, you can however do something like this (that is, if you only plan to verify whether id-code mapping is correct):

使用TestCase属性适用于需要验证多个简单输入/输出的测试用例。在你的场景中,你可以做这样的事情(也就是说,如果你只打算验证 id-code 映射是否正确):

[TestCase(6, Result = "GBP")]
[TestCase(7, Result = "USD")]
[TestCase(8, Result = "CAD")]
public string CanGetCurrencyById(int id)
{
    ICurrencyRepo currencies = new RepoFactory().CreateCurrencyRepo(_session);
    Currency c = currencies.GetById<Currency>(id);

    return c.Code;
}

Also, take a look at TestCasedocumentation- they provide quite good examples.

另外,看看TestCase文档- 他们提供了很好的例子。

Edit: By mapping testing I meant verifying whether your ORM mappings (NHibernate to database) are correct and work as you intended. You usually test that in following scenario:

编辑:通过映射测试,我的意思是验证您的 ORM 映射(NHibernate 到数据库)是否正确并按您的预期工作。您通常会在以下场景中进行测试:

  1. Create new entity instance with predefined values (eg. Currency)
  2. Start new transaction
  3. Save entity (Save+ Flush+ Evictcombination to ensure NHibernate doesn't store saved entity in cache anymore)
  4. Retrieve entity
  5. Compare retrieved values with predefined ones
  6. Rollback transaction
  1. 创建具有预定义值的新实体实例(例如。Currency
  2. 开始新的交易
  3. 保存实体(Save+ Flush+Evict组合,以确保NHibernate的不保存实体存储在缓存中了)
  4. 检索实体
  5. 将检索到的值与预定义的值进行比较
  6. 回滚事务

If such test then passes, it more or less tells you that I can save this entity with those values, and I can then retrieved it with the exactly same values. And that's all you wanted to know - mappings are correct.

如果这样的测试通过了,它或多或少会告诉你我可以用这些值保存这个实体,然后我可以用完全相同的值检索它。这就是您想知道的全部 - 映射是正确的。

With TestCaseattribute tho, verifying correctness of entire objects is quite difficult - it's meant to test simple stuff. You can use workarounds like suggested in other answer (passing arguments via TestCase) but it quickly becomes unreadable and hard to maintain (imagine entity with 6+ properties to verify).

使用TestCase属性 tho,验证整个对象的正确性非常困难 - 它旨在测试简单的东西。您可以使用其他答案中建议的变通方法(通过 传递参数TestCase),但它很快变得不可读且难以维护(想象具有 6 个以上属性的实体需要验证)。

I suggest splitting your test into one that verifies whether mapping of idto codeis correct (however I see little point in doing that, unless you alwaysplan to have certain ids mapped to certain codes) and other one verifying whether Currencyentity is properly mapped to database table.

我建议将您的测试分成一个来验证idto 的映射是否code正确(但是我认为这样做没什么意义,除非您总是计划将某些 id 映射到某些代码)和另一个验证Currency实体是否正确映射到数据库表.

回答by dasblinkenlight

In cases like this I pass constructor arguments for the expected result into the test case, and, do the check myself. Although it is not as concise, it gets the job done.

在这种情况下,我将预期结果的构造函数参数传递给测试用例,然后自己进行检查。虽然它不那么简洁,但它完成了工作。

[TestCase(6, "GBP", "British Pound", "£")]
public void CanGetCurrencyById(int id, string code, string name, string symbol)
{
    ICurrencyRepo currencies = new RepoFactory().CreateCurrencyRepo(_session);
    Currency c = currencies.GetById<Currency>(id);
    Assert.That(c, Is.EqualTo(new Currency(code, name, symbol)));
}