I am getting the error "One or more errors occurred. (TestPointId, testCaseId, testCaseRevision, testCaseTitle must be specified for planned test results.)", when trying to add a test result to a programmatically created manual test run in Azure Dev Ops. I wasn't getting this error when AzureDevOps was hosted on on-prem servers but since the deployment to Azure Cloud, I have been facing this issue. Some more information about my environment
OS: Windows 10
VS Version: VS 2022 (64 Bit) - 17.8.3
Framework : .NET Standard 2.0
Language : C# v10.0
NuGet:
Microsoft.TeamFoundationServer.Client - 19.231.0-preview
Microsoft.VisualStudio.Services.Client - 19.231.0-preview
Below is my code
Models:
public class TestResultOutcomeUpdateModel
{
public int TestPlanId { get; set; }
public int TestSuiteId { get; set; }
public int TestCaseId { get; set; }
public int PointId { get; set; }
public int TestRunId { get; set; }
public Outcome Outcome { get; set; }
public string ErrorMessage { get; set; } = string.Empty;
public DateTime StartTime { get; set; }
public DateTime EndTime { get; set; }
public RunState TestState { get; set; }
public double DurationInMs { get => (EndTime - StartTime).TotalMilliseconds; }
public bool? ResetToActive { get; set; } = default!;
}
Main Code:
public class TestMain
{
#region Public Methods
public void Test()
{
const string ACCESS_TOKEN = "azuredevopspat";
const string URL_KEY = "azuredevopsurl";
string Url = ConfigurationManager.AppSettings[URL_KEY];
string Token = ConfigurationManager.AppSettings[ACCESS_TOKEN];
VssConnection _VssConnection ??= new VssConnection(new Uri(Url), new VssBasicCredential(string.Empty, Token));
TestManagementHttpClient _TestManagementHttpClient = _VssConnection?.GetClient<TestManagementHttpClient>();
IdentityRef _IdentityRef = new IdentityRef() { DisplayName = _VssConnection.AuthorizedIdentity.DisplayName };
const string _ProjectName = "TEST1";
int planId = 1;
int suiteId = 2;
int testCaseId = 3;
TestRunState runState = TestRunState.NeedsInvestigation;
string testRunName = "Build#1.11_Test";
// initializers with default values
int testRunId = default!;
bool updated = false;
try
{
// Create a Test Run
RunCreateModel rcm = new(testRunName, string.Empty, null, new ShallowReference() { Id = planId.ToString() }, null, 0, string.Empty, null, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, DateTime.Now.ToString("O"), string.Empty, null, null, null, null, _IdentityRef);
testRunId = _TestManagementHttpClient.CreateTestRunAsync(rcm, _ProjectName).Result.Id;
// Add Test Result to test run
IList<TestResultOutcomeUpdateModel> testResultOutcomeUpdateModels = new List<TestResultOutcomeUpdateModel>() { new TestResultOutcomeUpdateModel()
{
TestPlanId= planId,
TestSuiteId= suiteId,
TestCaseId = testCaseId,
TestRunId = testRunId,
ResetToActive = null,
Outcome = Outcome.Failed,
ErrorMessage = "Exception",
StartTime= DateTime.Now,
EndTime= DateTime.Now + TimeSpan.FromSeconds(1),
TestState = RunState.Completed
} });
IList<TestCaseResult> testCaseResults = new List<TestCaseResult>();
// Iterate each ResultOutcomeUpdateModel
testResultOutcomeUpdateModels.ForEach(trocum =>
{
// Get Point Id
// Get test points associated with a testplan id, suite id, and test case id
List<TestPoint> points = _TestManagementHttpClient.GetPointsAsync(_ProjectName, trocum.TestPlanId, trocum.TestSuiteId, null, null, trocum.TestCaseId.ToString()).Result;
TestPoint? pnt = points?.First();
// populate the model value
trocum.PointId = pnt?.Id ?? 0;
// Attach the test result to test run
ShallowReference testPoint = new()
{
Id = pnt?.Id.ToString(),
Url = pnt?.Url
};
// with the latest hosting to cloud, the testpoint is returning null for test plan and suite, so basically making sure that we have null check
TestCaseResult trcm = new()
{
TestPlan = pnt?.TestPlan ?? new ShallowReference() { Id = trocum.TestPlanId.ToString() },
TestSuite = pnt?.Suite ?? new ShallowReference() { Id = trocum.TestSuiteId.ToString() },
TestRun = new ShallowReference(trocum.TestRunId.ToString()),
TestPoint = testPoint,
TestCase = new ShallowReference(pnt?.TestCase) { Id = pnt?.TestCase.Id, Name = pnt?.TestCase?.Name, Url = pnt?.TestCase.Url },
StartedDate = trocum.StartTime,
TestCaseReferenceId = int.Parse(pnt?.TestCase?.Id ?? "0"),
TestCaseRevision = pnt?.Revision ?? 1,
TestCaseTitle = pnt?.TestCase?.Name,
RunBy = _IdentityRef
};
// Add TestResults to Test Runs
// Note : this won't update the outcome of the testpoint and won't get reflected in TestCase View
// ERROR: throwing the following error Here -> "One or more errors occurred. (TestPointId, testCaseId, testCaseRevision, testCaseTitle must be specified for planned test results.)"
TestCaseResult[] addedTrcm = _TestManagementHttpClient.AddTestResultsToTestRunAsync(new TestCaseResult[1] { trcm }, _ProjectName, trocum.TestRunId).Result.ToArray();
// Update test result once created to get reflected in Test cases
addedTrcm[0].Outcome = trocum.Outcome.ToString();
addedTrcm[0].CompletedDate = trocum.EndTime;
addedTrcm[0].DurationInMs = trocum.DurationInMs;
addedTrcm[0].State = trocum.TestState.ToString();
addedTrcm[0].ErrorMessage = trocum.ErrorMessage;
// Update test results and add it to the list for update status comparison
testCaseResults.AddRange(_TestManagementHttpClient.UpdateTestResultsAsync(addedTrcm, _ProjectName, trocum.TestRunId).Result);
});
_ = testCaseResults.Count == testResultOutcomeUpdateModels.Count;
}
finally
{
// Update the test run state
RunUpdateModel rum = new(testRunName, (DateTime.Now + TimeSpan.FromSeconds(2)).ToString("o"), string.Empty, runState.ToString());
_ = _TestManagementHttpClient.UpdateTestRunAsync(rum, _ProjectName, testRunId).Result;
}
}
#endregion Public Methods
}
I don't understand why its complaining about "a planned test results".
I have also seeing others facing similar issue in the past - https://github.com/microsoft/azure-devops-node-api/issues/318.
Any kind of help will be appreciated.
I have also tried upgrading and downgrading NuGet packages but that didn't help either.