I have multiple TestRunParameters in an nunit .runsettings file. How can I loop over them?

328 views Asked by At

I have a really simple .runsettings file for nunit that looks like this:

<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
    <TestRunParameters>
          <Parameter name="someParameterName" value="someExampleValue" />
          <Parameter name="variable1" value="someExampleValue" />
          <Parameter name="variable2" value="someExampleValue" />
    </TestRunParameters>
</RunSettings>

What I am hoping to do is loop over the parameters instead of accessing by name.

This works if I only want a single value:

var value = TestContext.Parameters["someParameterName"];

It feels like this should work to loop over them but it doesn't:

var allParams = TestContext.Parameters;

foreach (var item in allParams) {
     //I'll want to get the items name and value here. 
}

I get this error:

Compiler Error CS1579 foreach statement cannot operate on variables of type 'type1' because 'type2' does not contain a public definition for 'identifier'

What am I missing??

1

There are 1 answers

0
Charles Han On BEST ANSWER

This is because TestContext.Parameters returns TestParameters, which is not a collection.

The best you can do is:

var allParams = TestContext.Parameters;

foreach (var name in allParams.Names)
{
    //I'll want to get the items name and value here. 
    var value = allParams[name];
    Console.WriteLine($"{name} : {value}");
}

And the output will be like,

someParameterName : someExampleValue
variable1 : someExampleValue
variable2 : someExampleValue