NUnit 3.0 TestCase const 自定义对象参数

NUnit 3.0 TestCase const custom object arguments

我已经编写了 class SomeObject 并且我想在我的 TestCase 中将此对象的 const 实例定义为 keep/reuse。我应该如何重写下面的代码来实现这种行为?

[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;
    }
}

我收到以下错误消息:

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

C# language spec, §10.3 说(强调我的):

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.


恼人的是,属性也有一定的限制这一事实使情况更加复杂 - 请参阅 C# language spec, §17.2(再次强调我的):

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.

其中§17.1.3:“属性参数类型”说1:

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:引用的文本来自旧版本的 C# 规范 - 在 C# 5.0 版本中,提到了另外四种类型:sbyte , uint, ulong, 和 ushort.


换句话说,你能做的最好的事情是:

[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;
    }
}

这样,属性的参数是编译时常量,getObject 方法可以处理获取 SomeObject 实例。

  1. 只需删除常量。它将是每个实例的私有变量
  2. 将其设为静态,它将成为 class 的单例。
  3. 将常量替换为只读。这会将其标记为不应该被弄乱的东西。

一个更好的方法来实现你想要做的是使用 TestCaseSource。你的情况:

[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" );
    }
  }
}