Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# an attribute argument must be a constant expression

Why does my String array below give me an Error, arent they all strings??? "An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type"

[Test]
[TestCase(new string[]{"01","02","03","04","05","06","07","08","09","10"},TestName="Checking10WOs")]
public void Test(String[] recordNumber)
{
     //something..
} 
like image 503
GucciProgrammer Avatar asked Sep 18 '14 19:09

GucciProgrammer


2 Answers

This does not answer to the title of question, however it solves your specific problem.

You might want to use TestCaseSource, it allows you to pass multiple test case scenarios into the same testing mechanism, and you can use it as complex structures as you like.

    [Test]
    [TestCaseSource("TestCaseSourceData")]
    public void Test(String[] recordNumber, string testName)
    {
        //something..
    }

    public static IEnumerable<TestCaseData> TestCaseSourceData()
    {
        yield return new TestCaseData(new string[] {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10"}, "Checking10WOs");
    }

It will figure out that the first parameter is recordNumber and the second is testName

see the screenshot below.

enter image description here

Hope this saves you some time.

like image 198
Matas Vaitkevicius Avatar answered Oct 12 '22 10:10

Matas Vaitkevicius


The strings are all constant but the array they are in is not. Try this instead:

[Test]
[TestCase("01","02","03","04","05","06","07","08","09","10", TestName="Checking10WOs")]
public void Test(String recordNumber)
{
     //something..
} 

This works because TestCaseAttribute accepts its cases as a params list.

like image 28
Patrick Quirk Avatar answered Oct 12 '22 11:10

Patrick Quirk