@@ -477,10 +477,27 @@ public AzureDevOpsBuildArtifact LinkArtifact(string name, string type, string lo
477
477
}
478
478
479
479
/// <summary>
480
- /// Gets the test run ID's of a build.
480
+ /// Gets the test runs of the build including test results .
481
481
/// </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>
483
486
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 )
484
501
{
485
502
if ( ! this . ValidateBuild ( ) )
486
503
{
@@ -489,24 +506,35 @@ public IEnumerable<AzureDevOpsTestRun> GetTestRuns()
489
506
490
507
using ( var testClient = this . testClientFactory . CreateTestManagementClient ( this . CollectionUrl , this . credentials ) )
491
508
{
492
- return
509
+ // Read test result details for current build.
510
+ var testResultDetails =
493
511
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 =>
502
528
new AzureDevOpsTestRun
503
529
{
504
- RunId = runId ,
530
+ RunId = testRun . Key ,
505
531
TestResults =
506
- this . GetTestResults ( testClient , runId )
532
+ this . GetTestResults ( testClient , testRun . Key , testRun . Count ( ) , maxResultsPerTestRun )
533
+ . ConfigureAwait ( false )
507
534
. GetAwaiter ( )
508
535
. GetResult ( ) ,
509
- } ) . ToList ( ) ;
536
+ } )
537
+ . ToList ( ) ;
510
538
}
511
539
}
512
540
@@ -515,21 +543,50 @@ public IEnumerable<AzureDevOpsTestRun> GetTestRuns()
515
543
/// </summary>
516
544
/// <param name="testClient">Instance of a <see cref="TestManagementHttpClient"/> class.</param>
517
545
/// <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 )
523
555
{
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 ;
533
590
}
534
591
535
592
/// <summary>
0 commit comments