C # attribute argument must be a constant expression

Why is my array of lines below giving me an error, arent all the lines? "The attribute argument must be a constant expression, a typeof expression, or an array creation expression of the 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.. } 
+6
source share
2 answers

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

You can use TestCaseSource , it allows you to pass multiple scripting scripts into the same testing mechanism, and you can use as complex structures as you like.

  [Test] [TestCaseSource("TestCaseSourceData")] public void Test(String[] recordNumber, string testName) { //something.. } public IEnumerable<TestCaseData> TestCaseSourceData() { yield return new TestCaseData(new string[] {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10"}, "Checking10WOs"); } 

It will be found out that the first parameter is recordNumber , and the second is testName

see screenshot below.

enter image description here

Hope this saves you some time.

+5
source

The lines are all constant, but the array in which they are located is not. Try 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 list of params .

+2
source

Source: https://habr.com/ru/post/975501/


All Articles