How to use TestCase in NUnit 2.5?

21,672

Solution 1

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

Using TestCase attribute 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(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 TestCase documentation - they provide quite good examples.

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:

  1. Create new entity instance with predefined values (eg. Currency)
  2. Start new transaction
  3. Save entity (Save + Flush + Evict combination to ensure NHibernate doesn't store saved entity in cache anymore)
  4. Retrieve entity
  5. Compare retrieved values with predefined ones
  6. Rollback transaction

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 TestCase attribute 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).

I suggest splitting your test into one that verifies whether mapping of id to code is correct (however I see little point in doing that, unless you always plan to have certain ids mapped to certain codes) and other one verifying whether Currency entity is properly mapped to database table.

Solution 2

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)));
}
Share:
21,672
Mark Allison
Author by

Mark Allison

Updated on July 16, 2022

Comments

  • Mark Allison
    Mark Allison almost 2 years

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

    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(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?

  • Mark Allison
    Mark Allison over 12 years
    Thanks, can you explain how I can test multiple objects? What is mapping testing?
  • k.m
    k.m over 12 years
    @Mark: by mapping test you verify that ORM mappings are correct (eg. that if you save entity with DateTime property set, it is mapped and saved without any information loss). See my edit.