Darthg8r
Darthg8r

Reputation: 12675

Pass arguments to tests in Azure Devops

I've got an api project that I'd like to run some integration tests on in the Azure release pipeline.

  1. Build project.
  2. Create release.
  3. Deploy release to slot.
  4. Run NUnit integration tests against slot. This entails http requests to the slot.
  5. If tests pass, swap production slot with the tested slot.

I'm stuck on step 4. It's easy to pass arguments to a test fixture in Visual Studio.

[TestFixture(arguments: "https://urltomyslot.azurewebsites.net")]
public class CachedClientApiTokenManagerTests
{
    public CachedClientApiTokenManagerTests(string authority)
    {
        _authority = authority;
    }

    private readonly string _authority;

    // Runs tests using the url
}

What I don't know how to do is passing arguments from Azure Devops based on environment. I'm using the NUnit3TestAdapter package and it runs fine, but the args is the sticking point. If we're doing this in our lab environment, the url passed is different from the staging or production urls.

How do we configure this in Azure DevOps with args?

Upvotes: 6

Views: 5469

Answers (2)

Thinkx
Thinkx

Reputation: 129

You can configure runsettings file and override test parameters based on the environment.

Runsettings file :

 <TestRunParameters>
    <Parameter name="ApplicationUrl" value="https://qa.environment.url" /> 
 </TestRunParameters>

You can access the test run parameters like :

string applicationUrl = TestContext.Properties["ApplicationUrl"];

How to override parameters in VsTest task in pipeline:

enter image description here

Upvotes: 4

Shayki Abramczyk
Shayki Abramczyk

Reputation: 41545

You can define the environment in the variables:

enter image description here

Then read the variables in the C# code with this way:

string environment = Environment.GetEnvironmentVariable("environment", EnvironmentVariableTarget.Process);

Now depend to the value of environment create the URL and run the tests.

For example, I created a small Console Application:

class Program
{
    static void Main(string[] args)
    {
        string environment = Environment.GetEnvironmentVariable("environment", EnvironmentVariableTarget.Process);
        if (environment == "prod")
        {
            Console.WriteLine("We are in production :)");
        }
    }
}

I configured the variable:

enter image description here

And I run the .exe file, in the output I can see the We are in production :) printed:

enter image description here

Upvotes: 11

Related Questions