Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Core MSTest Parameters

I have a .NET Core MSTest library, and I would like to pass a parameter to it on the command line. Something like this:

dotnet test -myparam=myvalue

How would I do something like this, and how would I retrieve the value of "myparam" in code.

Note: I would be ok with somehow specifying a configuration file for the test library to use, rather than just passing a parameter.

like image 856
Liam Smith Avatar asked Feb 06 '26 08:02

Liam Smith


1 Answers

You can use the .runsettings file to send custom parameters. For example, I could have the following run settings file:

<RunSettings>
  <TestRunParameters>  
    <Parameter name="foo" value="bar" />  
  </TestRunParameters>  
</RunSettings>

You can then access it through the TestContext:

public TestContext TestContext { get; set; }

[TestMethod]
public void TestMethod1()
{
    Assert.AreEqual("bar", TestContext.Properties["foo"]);
}

Finally, you specify the runsettings file on the command line using the --settings argument:

dotnet test --settings test.runsettings

Note: There is support for passing some runsettings parameters in via the command line, but it looks like the support is rather basic at this time and it appears that you can't pass TestRunParameters in this way.

like image 97
John Koerner Avatar answered Feb 08 '26 21:02

John Koerner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!