Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NUnit 3.0 TestCase const custom object arguments

Tags:

c#

nunit

I've written the class SomeObject and I want to define a const instance of this object to keep/reuse in my TestCases. How should I rewrite the code below to achieve this behavior?

[TestFixture]
public class SomeObjectTests
{
    private const SomeObject item0 = new SomeObject(0.0); // doesn't work

    [TestCase(item0, ExpectedResult = 0.0)]
    public double TestSomeObjectValue(SomeObject so)
    {
        return so.Value;
    }

    [TestCase(item0, ExpectedResult = "0.0")]
    public string TestSomeObjectValueString(SomeObject so)
    {
        return so.ValueString;
    }
}

I get the following error message:

A const field of a reference type other than string can only be initialized with null.

like image 729
budi Avatar asked Dec 03 '15 20:12

budi


2 Answers

A better way to achieve what you are trying to do is to use TestCaseSource. In your case:

[TestFixture]
public class SomeObjectTests
{
    [TestCaseSource(typeof(TestSomeObject),"TestCasesValue")]
    public double TestSomeObjectValue(SomeObject so)
    {
        return so.Value;
    }

    [TestCaseSource(typeof(TestSomeObject),"TestCasesValueString")]
    public string TestSomeObjectValueString(SomeObject so)
    {
        return so.ValueString;
    }
}

public class TestSomeObject
{
  public static IEnumerable TestCasesValue
  {
    get
    {
        yield return new TestCaseData( new SomeObject(0.0) ).Returns( 0.0 );
        yield return new TestCaseData( new SomeObject(1.0) ).Returns( 1.0 );
    }
  }

  public static IEnumerable TestCasesValueString
  {
    get
    {
        yield return new TestCaseData( new SomeObject(0.0) ).Returns( "0.0" );
        yield return new TestCaseData( new SomeObject(1.0) ).Returns( "1.0" );
    }
  }
}
like image 97
Daniel Rose Avatar answered Dec 07 '22 08:12

Daniel Rose


The C# language spec, §10.3 says (emphasis mine):

When a symbolic name for a constant value is desired, but when the type of that value is not permitted in a constant declaration, or when the value cannot be computed at compile-time by a constant-expression, a readonly field (Section 10.4.2) may be used instead.


Annoyingly, this is compounded by the fact that attributes have certain restrictions too - see the C# language spec, §17.2 (again, emphasis mine):

An expression E is an attribute-argument-expression if all of the following statements are true:

  • The type of E is an attribute parameter type (Section 17.1.3).

  • At compile-time, the value of E can be resolved to one of the following:

    • A constant value.

    • A System.Type object.

    • A one-dimensional array of attribute-argument-expressions.

Where §17.1.3: "Attribute parameter types" says1:

The types of positional and named parameters for an attribute class are limited to the attribute parameter types, which are:

  • One of the following types: bool, byte, char, double, float, int, long, short, string.
  • The type object.
  • The type System.Type.
  • An enum type, provided it has public accessibility and the types in which it is nested (if any) also have public accessibility (§17.2).
  • Single-dimensional arrays of the above types.

1: the quoted text is from an older version of the C# specification - in the C# 5.0 version, four additional types are mentioned: sbyte, uint, ulong, and ushort.


In other words, the best you can do is something like:

[TestFixture]
public class SomeObjectTests {

    private static readonly SomeObject item0 = new SomeObject(0.0);

    private static SomeObject getObject(string key) {
        if ( key == "item0" )
            return item0;

        throw new ArgumentException("Unknown key");
    }

    [TestCase("item0", ExpectedResult = 0.0)]
    public double TestSomeObjectValue(string key) {
        SomeObject so = getObject(key);
        return so.Value;
    }

    [TestCase("item0", ExpectedResult = "0.0")]
    public string TestSomeObjectValueString(string key) {
        SomeObject so = getObject(key);
        return so.ValueString;
    }
}

This way, the arguments to the attributes are compile-time constant, and the getObject method can handle getting the SomeObject instance.

like image 31
Wai Ha Lee Avatar answered Dec 07 '22 08:12

Wai Ha Lee