Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 589
Darthg8r Avatar asked Apr 29 '19 17:04

Darthg8r


People also ask

How do you use parameters in test cases Azure DevOps?

In an open test case, select Convert to shared parameters. After you've created a shared parameter set, open another test case and add the shared parameter set to that test case. You can search for the shared parameter set by name. The shared parameter set is displayed in the Parameter values section after you add it.

How do you pass variables between tasks in Azure DevOps?

Share variables between Tasks across the Jobs (of the same Stage) We need to use the isOutput=true flag when you desire to use the variable in another Task located in another Job. Navigate to Stage1_Job1_Task1 and add isoutput = true flag to the Logging Command which let's us to access the value outside the Job.

How do you pass Azure test cases?

From the web portal, open your project and select Test Plans > Test plans. Select Mine or All, or use Filter by title to find your test plan and select it. Select the Execute tab. Launch Test Runner from Azure Test Plans by selecting Run for desktop application from the dropdown menu.


2 Answers

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

like image 74
Shayki Abramczyk Avatar answered Sep 30 '22 16:09

Shayki Abramczyk


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

like image 36
Thinkx Avatar answered Sep 30 '22 18:09

Thinkx