Execute automated TestRun from C# Azure DevOps SDK

Alessandro Ghessa 0 Reputation points
2023-03-06T14:16:40.4+00:00

I'm trying to launch an automated Test from a C# code.

This is a snippet of my code:

string  organizationName = "...";
string  projectName = "...";
int     planId = ...;
string  definitionName = "...";
int     definitionId = ...;
string  releaseName = "...";
int[]   pointIds = new int[] { ... };

Uri organizationUrl = await VssConnectionHelper.GetOrganizationUrlAsync(organizationName);
VssConnection connection = new VssConnection(organizationUrl, new VssClientCredentials());

BuildHttpClient buildClnt = connection.GetClient<BuildHttpClient>();
Build build = await buildClnt.GetLatestBuildAsync(projectName, definitionName);

Microsoft.TeamFoundation.TestManagement.WebApi.ShallowReference testPlanRef = new Microsoft.TeamFoundation.TestManagement.WebApi.ShallowReference(planId.ToString());
ReleaseHttpClient releaseClnt = connection.GetClient<ReleaseHttpClient>();
ReleaseStartMetadata releaseStartMetadata = new ReleaseStartMetadata()
{
  DefinitionId = definitionId
};
Release release = await releaseClnt.CreateReleaseAsync(releaseStartMetadata, projectName);

RunCreateModel testRunModel = new RunCreateModel(
  name                    : releaseName,
  pointIds                : pointIds,
  plan                    : testPlanRef,
  buildId                 : build.Id,
  isAutomated             : true,
  releaseUri              : release.Url
);
TestManagementHttpClient testMngmnt = connection.GetClient<TestManagementHttpClient>();
TestRun testRun = await testMngmnt.CreateTestRunAsync(testRunModel, projectName);

When I run this code I receive an exception "Both release id and release environment id must be specified" but I haven't found a way to specify them correctly. If I don't specify releaseUri in RunCreateModel the TestRun is not associated to the Release; I don't know if the value I set is right.

May someone please tell me how I can do that?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,237 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Alessandro Ghessa 0 Reputation points
    2023-03-27T06:45:39.2566667+00:00

    I solved the problem of launching an automated test programmatically with the following steps:

    1. create a TestRun for the TestPlan of the automated test
    2. create a Release of the Release Pipeline linked with the TestPlan
    3. update the TestRun with the Release Id
    4. update the references of the VsTest task in the Release to the TestRun
    5. update the state of the Release Environment of the Stage containing the VsTest task to InPRogress

    I don't know if it is the correct way but it worked for me.

    0 comments No comments