Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass runtime parameters to dotnet test on the command line?

I have a set of unit tests in a .NET Core project and using a runsettings file. I am trying to setup Azure DevOps to do automated testing on my deployments. As part of this process, I need to override parameters from the run settings on the command line.

I have a runsettings file with the following section:

  <TestRunParameters>
    <Parameter name="ApiUrl" value="https://myurl..." />
  </TestRunParameters>

I have a static constructor that saves the TestContext like this:

        [ClassInitialize]
        public static void TestClassInitialize(TestContext context)
        {
            TestContext = context;
        }

I am retrieving settings from the TestContext with the following method:

        protected string GetStringSetting(string settingName)
        {
            return TestContext.Properties[settingName] as string;
        }

When I run the test with the runsettings file selected, it gets the TestContext and I see the ApiUrl entry is retrieved successfully.

Now, I want to set this parameter from the command line. I am using a command like:

    dotnet test <myproject.csproj> --configuration Release -- ApiUrl=https://newurl

I get an error that says the dictionary does not contain the key "ApiUrl" which indicates that the setting was not processed. From reading documentation, I thought that maybe I need to fully specify the name of the setting with TestRunParameters.ApiUrl. This gives me an XML error.

From everything I have read, I think I am doing this right and can't figure out what is wrong. I am using version 2.1.503 of the tools.

Can someone please give me guidance on how to make this work?

like image 917
David Avatar asked Apr 06 '19 16:04

David


1 Answers

You can use the following argument syntax to do that:

dotnet test <myproject.csproj> --configuration Release -- TestRunParameters.Parameter(name=\"ApiUrl\", value=\"https://newurl\")

Source: https://github.com/Microsoft/vstest-docs/blob/master/docs/RunSettingsArguments.md

like image 52
Ε Г И І И О Avatar answered Oct 30 '22 14:10

Ε Г И І И О