Ajar
Ajar

Reputation: 162

NUnit Test case creation

I have Created testsuits like following.

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

I have pass the variables m = 10, n = 2 and o = 5.

But the second test case is not accessible. it throws the following error:

"An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type"

How to pass the variables name instead of values in test case?

Upvotes: 1

Views: 282

Answers (2)

Saifur
Saifur

Reputation: 16201

As nickmkk mentioned the variable has to be constant.

You do not need to pass the second attribute if you do not need to. In case you do, you pass same types in the test and in attribute. It will read the parameter sequentially.

[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

Upvotes: 0

Nicholas J. Markkula
Nicholas J. Markkula

Reputation: 1572

Unfortunately, you cannot pass variables to a test case unless they are constants.

Upvotes: 1

Related Questions