1

I have a test-method as follows:

[TestCase(new string[] { "1", "2", "5" }, Result = true)]
bool AllocateIDsTest1(IEnumerable<string> expected)
{
    var target = ...
    var actual = target.AllocateIDs(expected);

    return actual.SequenceEqual(expected);
}

However I get a compiler-error:

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

Probably the compiler can´t distinguish between the following constructors:

TestCase(params object[] args, Named Parameters);

and

TestCase(object ob1, Named Paramaters);

because new string[] { "1", "2", "5" } can be resolved to both params object[] and object.

From this post I know that a string-array should be possible to pass as compile-constants.

How can I provide an array of strings to a TestCase?

1 Answer 1

4

I found a solution using the params-approach:

[TestCase("1", "2", "5", Result = true)]
public bool AllocateIDsTest1(params string[] expected)
{
    var target = ...
    var actual = target.AllocateIDs(expected);

    return actual.SequenceEqual(expected);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Excellent workaround. Another option is to use TestCaseSource, see the docs.
Not even a workaround. :-) It's how TestCase is supposed to be used. The compiler error is not because you passed in an array - that's expressly allowed on attributes. It's because you are asking the attribute constructor to dynamically create the array using new. C# doesn't allow that.
@Charlie So how exactly should assigning an array to an attribute work, when you say "that´s expressly allowed on attributes"? Via a static field?

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.