Skip to content

Commit 8755f4f

Browse files
Christian Bumannpascalberger
authored andcommitted
(GH-258) Add option to define how many tests should be returned for a test run
By default will return all test runs, compared to the max 1000 which were returned before
1 parent 28f4611 commit 8755f4f

File tree

1 file changed

+85
-28
lines changed

1 file changed

+85
-28
lines changed

src/Cake.AzureDevOps/Pipelines/AzureDevOpsBuild.cs

Lines changed: 85 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -477,10 +477,27 @@ public AzureDevOpsBuildArtifact LinkArtifact(string name, string type, string lo
477477
}
478478

479479
/// <summary>
480-
/// Gets the test run ID's of a build.
480+
/// Gets the test runs of the build including test results.
481481
/// </summary>
482-
/// <returns>A list of RunIDs as int.</returns>
482+
/// <returns>A list of test runs or an empty list if no build could be found and
483+
/// <see cref="AzureDevOpsBuildSettings.ThrowExceptionIfBuildCouldNotBeFound"/> is set to <c>false</c>.</returns>
484+
/// <exception cref="AzureDevOpsBuildNotFoundException">If build could not be found and
485+
/// <see cref="AzureDevOpsBuildSettings.ThrowExceptionIfBuildCouldNotBeFound"/> is set to <c>true</c>.</exception>
483486
public IEnumerable<AzureDevOpsTestRun> GetTestRuns()
487+
{
488+
return this.GetTestRuns(null);
489+
}
490+
491+
/// <summary>
492+
/// Gets the test runs of the build including test results.
493+
/// </summary>
494+
/// <param name="maxResultsPerTestRun">Number of maximum test results to read for every test run.
495+
/// <c>0</c> for skipping reading of test results. <c>null</c> for reading all test results in batches.</param>
496+
/// <returns>A list of test runs or an empty list if no build could be found and
497+
/// <see cref="AzureDevOpsBuildSettings.ThrowExceptionIfBuildCouldNotBeFound"/> is set to <c>false</c>.</returns>
498+
/// <exception cref="AzureDevOpsBuildNotFoundException">If build could not be found and
499+
/// <see cref="AzureDevOpsBuildSettings.ThrowExceptionIfBuildCouldNotBeFound"/> is set to <c>true</c>.</exception>
500+
public IEnumerable<AzureDevOpsTestRun> GetTestRuns(int? maxResultsPerTestRun)
484501
{
485502
if (!this.ValidateBuild())
486503
{
@@ -489,24 +506,35 @@ public IEnumerable<AzureDevOpsTestRun> GetTestRuns()
489506

490507
using (var testClient = this.testClientFactory.CreateTestManagementClient(this.CollectionUrl, this.credentials))
491508
{
492-
return
509+
// Read test result details for current build.
510+
var testResultDetails =
493511
testClient
494-
.GetTestResultDetailsForBuildAsync(this.ProjectId, this.build.Id)
495-
.GetAwaiter()
496-
.GetResult()
497-
.ResultsForGroup
498-
.SelectMany(testResultGroup => testResultGroup.Results).ToList()
499-
.GroupBy(testResult => testResult.TestRun.Id)
500-
.Select(runGroup => int.Parse(runGroup.Key))
501-
.Select(runId =>
512+
.GetTestResultDetailsForBuildAsync(this.ProjectId, this.build.Id)
513+
.ConfigureAwait(false)
514+
.GetAwaiter()
515+
.GetResult();
516+
517+
// Group result by test runs.
518+
var testRuns =
519+
testResultDetails
520+
.ResultsForGroup
521+
.SelectMany(testResultGroup => testResultGroup.Results)
522+
.GroupBy(testResult => int.Parse(testResult.TestRun.Id));
523+
524+
// Read result details for every test run.
525+
return
526+
testRuns
527+
.Select(testRun =>
502528
new AzureDevOpsTestRun
503529
{
504-
RunId = runId,
530+
RunId = testRun.Key,
505531
TestResults =
506-
this.GetTestResults(testClient, runId)
532+
this.GetTestResults(testClient, testRun.Key, testRun.Count(), maxResultsPerTestRun)
533+
.ConfigureAwait(false)
507534
.GetAwaiter()
508535
.GetResult(),
509-
}).ToList();
536+
})
537+
.ToList();
510538
}
511539
}
512540

@@ -515,21 +543,50 @@ public IEnumerable<AzureDevOpsTestRun> GetTestRuns()
515543
/// </summary>
516544
/// <param name="testClient">Instance of a <see cref="TestManagementHttpClient"/> class.</param>
517545
/// <param name="runId">Id of the test run.</param>
518-
/// <returns>The test results for a build or an empty list if no build could be found and
519-
/// <see cref="AzureDevOpsBuildSettings.ThrowExceptionIfBuildCouldNotBeFound"/> is set to <c>false</c>.</returns>
520-
/// <exception cref="AzureDevOpsBuildNotFoundException">If build could not be found and
521-
/// <see cref="AzureDevOpsBuildSettings.ThrowExceptionIfBuildCouldNotBeFound"/> is set to <c>true</c>.</exception>
522-
private async Task<IEnumerable<AzureDevOpsTestResult>> GetTestResults(TestManagementHttpClient testClient, int runId)
546+
/// <param name="testCount">Number of tests in the test run.</param>
547+
/// <param name="maxResults">Number of maximum test results to read.
548+
/// <c>0</c> for skipping reading of test results. <c>null</c> for reading all test results in batches.</param>
549+
/// <returns>The test results for a build.</returns>
550+
private async Task<IEnumerable<AzureDevOpsTestResult>> GetTestResults(
551+
TestManagementHttpClient testClient,
552+
int runId,
553+
int testCount,
554+
int? maxResults)
523555
{
524-
return
525-
(await testClient.GetTestResultsAsync(this.ProjectId, runId).ConfigureAwait(false))
526-
.Select(test =>
527-
new AzureDevOpsTestResult
528-
{
529-
AutomatedTestName = test.AutomatedTestName,
530-
Outcome = test.Outcome,
531-
ErrorMessage = test.ErrorMessage,
532-
});
556+
var testResults = new List<AzureDevOpsTestResult>();
557+
558+
if (maxResults <= 0)
559+
{
560+
return testResults;
561+
}
562+
563+
const int maxPagingSize = 10000;
564+
var resultCount = 0;
565+
var remainingResultsToRead = maxResults.HasValue ? Math.Min(testCount, maxResults.Value) : testCount;
566+
do
567+
{
568+
var resultsToRead = Math.Min(remainingResultsToRead, maxPagingSize);
569+
testResults.AddRange(
570+
(await testClient
571+
.GetTestResultsAsync(
572+
this.ProjectId,
573+
runId,
574+
skip: resultCount,
575+
top: resultsToRead)
576+
.ConfigureAwait(false))
577+
.Select(test =>
578+
new AzureDevOpsTestResult
579+
{
580+
AutomatedTestName = test.AutomatedTestName,
581+
Outcome = test.Outcome,
582+
ErrorMessage = test.ErrorMessage,
583+
}));
584+
585+
resultCount += resultsToRead;
586+
remainingResultsToRead = Math.Max(remainingResultsToRead - resultsToRead, 0);
587+
} while (remainingResultsToRead > 0);
588+
589+
return testResults;
533590
}
534591

535592
/// <summary>

0 commit comments

Comments
 (0)