1

I have an Integration Test .NET 5 XUnit Project. I am using XUnit 2.4 and XUnit.Runner.VisualStudio 2.4. The Integration Tests in this project need one configuration value, the ApiUrl. I want to run the integration tests in two ways:

  1. From Visual Studio using All Test Run or ReSharper
  2. From Azure DevOps's Pipeline and pass the ApiUrl from the pipeline variables which will override the one from the project's settings

My questions:

  • How do I do my configuration in a way that my test code accesses the ApiUrl in a transparent way (it doesn't care where it is running from)?
  • How do I pass the variable ApiUrl to my Integration test project, I am using the DotNetCoreCLI@2 test task as my pipeline step.
Adam
  • 3,872
  • 6
  • 36
  • 66

1 Answers1

1

It's possible to add environment variables to your task, via env:, which you can then read in your code:

- task: DotNetCoreCLI@2
  inputs:
    command: test
    projects: "tests/**/*Tests.csproj"
  env:
    ApiUrl: $(PipelineVariable)

In your C# code:

var apiUrl = Environment.GetEnvironmentVariable("ApiUrl") ?? "http://example.com";
Métoule
  • 13,062
  • 2
  • 56
  • 84
  • Thank you, this answers part of the question. I don't want to access the variables from environment on the local machine, I want to access them via appsettings.json – Adam Aug 10 '21 at 10:49
  • Setting `ApiUrl` in the task's environment variables will force that value to take precedence over the one coming from the `appsettings.json`, because by default the configuration builder is set up via `configBuilder.AddJsonFile('appsettings.json).AddEnvironmentVariables()` – Métoule Aug 10 '21 at 12:22
  • So, why are you accessing the variable explicitly via Environment.GetEnvironmentVariable rather than IOption Or IConfigurationRoot["ApiUrl"] ? – Adam Aug 10 '21 at 12:33
  • I thought that you only needed to access that variable in XUnit, not in the application itself. Sorry about that! – Métoule Aug 10 '21 at 12:45