Search code examples
azure-pipelines-yamldotnet-cliazure-pipelines-tasks

Overriding DotNet Core test run parameters from a YAML pipeline task


I have a couple of integration tests which use connection strings. The test is run from an azure devops pipeline, with yaml definition (DotNetCoreCLI@2 task)

This connstrings used to be set in the .runsettings file, but I would rather they were secret, so I am moving the conn string to the pipeline library, and marking as secret.

Meanwhile, I have removed the secrets from the runsettings file, leaving the parameter values as empty strings there.

So now, I just have to override the test run parameters value when running dotnet test...

From reading the docs from Microsoft here, I have verified that I can accomplish this in Powershell

dotnet test path\to\mytest.dll --logger trx --settings path\to\my.runsettings --% -- TestRunParameters.Parameter(name=\"Customer1.TestConnStr\", value=\"Id=xxx;Pass=yyy\") TestRunParameters.Parameter(name=\"Customer2.TestConnStr\", value=\"Id=xxx;Pass=yyy\")

I also see that the --% is not used when running from cmd, which I think is more appropriate for my yaml pipeline (running on a windows agent). But still, I am unable to express the above in my yaml task. I've made quite a few guesses, an example is here:

  - ${{ each project in parameters.projects }}:
    - task: DotNetCoreCLI@2
      displayName: Execute ${{ project }} integration tests
      condition: succeededOrFailed()
      inputs:
        command: 'test'
        projects: '$(Pipeline.Workspace)/IntegrationTests/*/*${{ project }}.Test.dll'        
        arguments: '--settings ${{ parameters.testRunSettingsFile }} -- TestRunParameters.Parameter(name=\"Customer1.TestConnStr\", value=\"${{ parameters.testConnStr }}\" TestRunParameters.Parameter(name=\"Customer2.TestConnStr\", value=\"${{ parameters.testConnStr }}\")'
        testRunTitle: ${{ parameters.testRunTitlePrefix }}${{ project }}

In this case, the quotation marks wrapping the parameter override name and value do not appear in the generated command. Giving me an error:

The test run parameter argument is invalid. Please use the format below. Format: TestRunParameters.Parameter(name=\"<name>\", value=\"<value>\")

Substituting for properly yaml escaped single quotes as below gives me the same error:

-- TestRunParameters.Parameter(name=''Customer1.TestConnStr'', value=''${{ parameters.testConnStr }}''

So I think I am close to the solution, it may just be something with my character escaping but I just can't find how or what to escape.


Solution

  • Ended up solving this myself after some trial and error, just a matter of correctly escaping the literal backslash double quote. "\"" So I had the following line in my YAML:

    arguments: '--settings ${{ parameters.testRunSettingsFile }} -- TestRunParameters.Parameter(name="\""Customer1.TestConnStr"\"", value="\""${{ parameters.testConnStr }}"\"" TestRunParameters.Parameter(name="\""Customer2.TestConnStr"\"", value="\""${{ parameters.testConnStr }}"\"")'