Support for PowerShell Script : Re-run only the failed test cases during the TFS build process-CI:CD

Copper Contributor

Hi Team,

 

We developed a test automation micro service in our project. Currently, we are running test cases by calling the micro service : using PowerShell script during the TFS build process.

Here, I require small help to re-run only the failed test cases with the below PowerShell script. Please suggest and support us.

 

our sample code:

 

 

 

 

 

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 

$runURL = APIURL + 'run/' + Project

Write-Host "startings tests for ["api"] using url [$runURL]"

try {
    Write-Host "Invoke-WebRequest - [$runURL]"
    $response = Invoke-WebRequest -UseBasicParsing -Uri $runURL
    Write-Host "Invoke-WebRequest - [$runURL]"                
    $sessionID = $response.Content
    Write-Host "sessionID - [$sessionID.Content]"                
    $resultURL = APIURL + 'files/session/' + $sessionID.Replace("`"", "")
    Write-Host "resultURL [$resultURL] "
    $statusUrl = $response.Headers["Location"]
    Write-Host "statusUrl- [$statusUrl]"
}
catch {
    Write-host "failed to start test" -foregroundcolor Red
    exit 1;
}

$testResults = "$(System.DefaultWorkingDirectory)\TestResult-api.trx"

$i = 0 >$null 2>&1

while ($true) {
    $i++ >$null 2>&1

    $statusCode = 0

    try {
        $statusCode = (Invoke-WebRequest -UseBasicParsing -Uri $statusUrl -ErrorAction Continue).statuscode
    }
    catch {
        $statusCode = $_.Exception.Response.StatusCode.Value__
    }

    if ($statusCode -eq "204") {
        Write-host "tests completed in about" ($i * 5) "seconds" -foregroundcolor Blue
        Write-host "follow [$resultURL] to download test results" -foregroundcolor Gray

        try {
            Invoke-WebRequest -Uri $resultURL -OutFile $testResults
            if (!$testResults) {
                Write-Host "unable to read results" 
                exit 1
                break;
            }
        }
        catch {
            Write-Host "failed to download test results" 
            exit 1
            break;
        }

        $regex = 'outcome="[a-z]{0,10}" '
        $failed = select-string -Path $testResults -Pattern $regex -AllMatches | % { $_.Matches } | % { $_.Value } | Where-Object { $_ -notmatch 'Passed' }

        if ($failed -and $failed.Trim().length -ge 0) {
            Write-Error "tests failed" 
            break;
        }
        else {
            Write-Host "tests passed" -foregroundcolor Green
            break;
        }
    }
    elseif ($statusCode -eq "417") {
        Write-host "tests in progress" -foregroundcolor Blue

    }
    elseif ($statusCode -eq "404") {
        Write-Error "failed to find test result" 
        break;

    }
    elseif ($statusCode -eq "503") {
        Write-Error "test service error" 
        break;

    }
    else {
        Write-Error "unknown test error: $statusCode" 
        break;
    }

    start-sleep -s 5;

    if ($i -eq 240) {
        Write-Error "tests timed out" 
        break;
    }
}

 

 

 

 

 

0 Replies