10

I want to pass different test parameters using NUnit Test.

I can pass integer array, no problem, but when I pass string array it does not work.

[TestCase(new[] { "ACCOUNT", "SOCIAL" })]
public void Get_Test_Result(string[] contactTypes)
{
}

Error 3 An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type ... \ContactControllerTests.cs 78 13 UnitTests

It works when I use string array as a second argument.

So what is the reason?

[TestCase(0, new[] {"ACCOUNT", "SOCIAL"})]
public void Get_Test_Result(int dummyNumber, string[] contactTypes)
{
}
3
  • Did you try specify array type explicitly? new string[] { ... }? Commented Apr 2, 2015 at 5:45
  • @abatishchev Yes but it does not work. Commented Apr 2, 2015 at 5:59
  • 1
    I see. Bummer. What version of NUnit do you use? Commented Apr 2, 2015 at 6:01

4 Answers 4

5

As k.m says, I believe the compilation error is a result of overload resolution & arrays co-variance, and the suggestion to cast to object did not work for me.

However, specifying the argument name (arg, in the case of TestCaseAttribute) fixes the mix up in overload resolution as you are specifying exactly which overload you want:

[TestCase(arg:new string[]{"somthing", "something2"})]

This compiles and works for me.

Sign up to request clarification or add additional context in comments.

Comments

3

I believe this is a case of overload resolution & arrays co-variance issue.

With [TestCase(new string[] { "" })] compiler decides the best overload for TestCase constructor is the one taking params object[] as argument. This is because compiler can assign string[] to object[] thanks to arrays co-variance and as a result this is more specific match than string[] to object assignment (other constructor).

This doesn't happen with int[] because co-variance does not apply to arrays of value types so compiler is forced to use object constructor.

Now, why it decides that new [] { "ACCOUNT", "SOCIAL" } is not an array creation expression of an attribute parameter type is beyond me.

2 Comments

So possible solution could be [TestCase((object)new string[] { "" })] to force desired overload...
@AlexeiLevenkov: that's what I would do. But I still cannot figure out why compiler has issues with string[] as attribute constructor argument even though spec says its valid.
0

Consider doing as follows

[TestCase("ACCOUNT", "SOCIAL")]
public void Test1()
{

}

Not sure if your test would be similar to mine. But I got the expected result with the following test

[TestFixture]
public class TestCaseTest
{
  [TestCase("ACCOUNT","SOCIAL")]
  public void Get_Test_Result(String a, String b)
  {
    Console.WriteLine("{0},{1}",a,b);   
  }
}

And the result

enter image description here

Additionally, if you want some reference to the TestCaseAttribute

7 Comments

Generally, answers are much more helpful if they include an explanation.
No such thing as self-explanatory code, especially when you're dealing with syntax quirks like this. Especially when you depart entirely from one of the opening assumptions and don't explain why the problem came about in the first place.
@Saifur Wrong number of arguments provided
@abatishchev: It's better since the edits, but it still doesn't explain what went wrong in the original question.
@Saifur: I did. I know what you're doing. I still don't know why the OP's approach didn't work, but that's OK, because here is your codez!
|
0

Another way by using TestCaseSource attribute:

[TestCaseSource((typeof(AccountTypes))]
public void Get_Test_Result(string[] contactTypes)
{
}

public class AccountTypes : IEnumerable
{
    public IEnumerator GetEnumerator()
    {
        yield return new string[] { "ACCOUNT", "SOCIAL" };
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.