NUnit 测试用例创建
NUnit Test case creation
我已经创建了如下测试服。
[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。
但是第二个测试用例无法访问。它抛出以下错误。
"An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type"
如何在测试用例中传递变量名而不是值。
遗憾的是,您不能将变量传递给测试用例,除非它们是常量。
正如 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);
}
print
1st run: Expected: 1
But was: 2
at NUnit.Framework.Assert.That(Object actual, IResolveConstraint expression, String message, Object[] args)
at NUnit.Framework.Assert.AreEqual(Int32 expected, Int32 actual)
at Test.Test.DivideTest(Int32 n, Int32 d, Int32 q) in ImplicitVsExplicitTest.cs: line 22
n=10, d=5, q=1
2nd run
n=12, d=4, q=3
我已经创建了如下测试服。
[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。
但是第二个测试用例无法访问。它抛出以下错误。 "An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type"
如何在测试用例中传递变量名而不是值。
遗憾的是,您不能将变量传递给测试用例,除非它们是常量。
正如 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);
}
1st run: Expected: 1 But was: 2
at NUnit.Framework.Assert.That(Object actual, IResolveConstraint expression, String message, Object[] args) at NUnit.Framework.Assert.AreEqual(Int32 expected, Int32 actual) at Test.Test.DivideTest(Int32 n, Int32 d, Int32 q) in ImplicitVsExplicitTest.cs: line 22 n=10, d=5, q=1
2nd run n=12, d=4, q=3