NUnit 测试用例创建

问题描述 投票:0回答:2

我创建了如下测试服。

[TestCase(12,4,3)]
[TestCase(m,n,o)]
public void DivideTest(int n, int d, int q)
{
    Assert.AreEqual( q, n / d );
}

我已传递变量 m = 10、n = 2 和 o = 5。

但是第二个测试用例无法访问。它抛出以下错误:

“属性参数必须是常量表达式,typeof 属性参数的表达式或数组创建表达式 类型”

如何在测试用例中传递变量名称而不是值?

c# nunit
2个回答
1
投票

不幸的是,你不能将变量传递给测试用例,除非它们是常量。


0
投票

正如 nickmkk 提到的,变量必须是常量。

如果不需要,可以不传第二个属性。如果您这样做,您将在测试和attribute中通过相同的类型。它将依次读取参数。

[TestCase(12, 4, 3)]
[TestCase(10, 5, 1)]
public void DivideTest(int n, int d, int q)
{
  Console.WriteLine("n={0}, d={1}, q={2}", n, d, q);
  Assert.AreEqual(q, n / d);
}

打印

第一次运行:预期:1 但是:2

at NUnit.Framework.Assert.That(实际对象,IResolveConstraint 表达式,字符串消息,Object[] args) 在 NUnit.Framework.Assert.AreEqual(Int32 预期,Int32 实际) 在 ImplicitVsExplicitTest.cs 中的 Test.Test.DivideTest(Int32 n, Int32 d, Int32 q) 处:第 22 行 n=10,d=5,q=1

第二次运行 n=12,d=4,q=3

© www.soinside.com 2019 - 2024. All rights reserved.