Core/Helpers/TestHelper.ps1

Set-StrictMode -Version Latest 
class TestHelper{
 
    #Gets the collection of the test cases based on the provided inputs
    static [TestCase[]] GetTestCases([string]$tsfileName,[string]$scenarioId,[string]$feature, [string]$moduleName, [string]$tcId, [string]$priority){
        
        #Reading all test scenarios
        $scenariospath = [CommonHelper]::GetRootPath() + "\" + $tsfileName
        $AllScenarios = Get-Content -Path $scenariospath | ConvertFrom-Json
        
        #Reading all test cases
        $testcasespath = [CommonHelper]::GetRootPath()+"\TestCasesMaster.json"
        $AllTestCases = Get-Content -Path $testcasespath | ConvertFrom-Json

        #Collecting required test scenarios and test cases
        $ReqScenario = $null  #$Scenarios.TestScenarios.TestScenario
        $ReqTestCases= @()

        if(![string]::IsNullOrEmpty($scenarioId)){
            $ReqScenario= $AllScenarios.TestScenarios.TestScenario | Where-Object { $_.TestScenarioID -eq $ScenarioID }
            $ScriptBlock = [scriptblock]::Create($ReqScenario.Query)
            $ReqTestCases += $AllTestCases | Where-Object $ScriptBlock
        }
        else{
            $ReqTestCases = $AllTestCases
        }
        if(!([string]::IsNullOrEmpty($tcId))){
            $ReqTestCases = $ReqTestCases | Where-Object {$_.TestCaseID -eq $tcId}
        }
        if(!([string]::IsNullOrEmpty($priority))){
            $ReqTestCases = $ReqTestCases | Where-Object {$_.Priority -eq $priority}
        }

        if(!([string]::IsNullOrEmpty($feature))){
            $ReqTestCases = $ReqTestCases | Where-Object {$_.Feature -eq $feature}
        }
        if(!([string]::IsNullOrEmpty($moduleName))){
            $ReqTestCases = $ReqTestCases | Where-Object {$_.ModuleName -eq $moduleName}
        }

        #Removing duplicate test cases from the collection
        $ReqTestCases = $ReqTestCases | Sort-Object -Property "TestCaseID" -Unique

        return $ReqTestCases
    }
    
    #Writes the test case result to host
    static [void] ExportTestCaseResult([TestCaseResult] $testcaseResult, [string]$AzSKEnv) {
                $Report = "" | Select-Object TestCaseID,Feature,ModuleName,AzSKEnvironment,Description,TestStatus,Message
                $Report.TestCaseID= $testcaseResult.TestCase.TestCaseID
                $Report.Feature=$testcaseResult.TestCase.Feature
                $Report.ModuleName=$testcaseResult.TestCase.ModuleName
                $Report.AzSKEnvironment=$AzSKEnv
                $Report.Description=$testcaseResult.TestCase.Description
                $Report.TestStatus=$testcaseResult.TestStatus
                $Report.Message=$testcaseResult.Message      

        Write-Host  ($Report |Format-List | Out-String)    
        }

    #Writes the test case results to csv file.
    static [void] ExportTestResultsToCSV([TestCaseResult[]] $testcaseResults, [string]$resultPath, [string]$AzSKEnv) {

                $ReportItems = @()
                $testcaseResults | ForEach-Object{
                $testcaseResult = $_
                $ReportItem = "" | Select-Object TestCaseID,Feature,Priority,Type,ModuleName,AzSKEnvironment,Description,TestStatus,Message, ManualSteps,TimeOfExecution
                $ReportItem.TestCaseID= $testcaseResult.TestCase.TestCaseID
                $ReportItem.Feature=$testcaseResult.TestCase.Feature
                $ReportItem.Priority=$testcaseResult.TestCase.Priority
                $ReportItem.Type=$testcaseResult.TestCase.Type
                $ReportItem.ModuleName=$testcaseResult.TestCase.ModuleName
                $ReportItem.AzSKEnvironment = $AzSKEnv
                $ReportItem.Description=$testcaseResult.TestCase.Description
                $ReportItem.TestStatus=$testcaseResult.TestStatus
                $ReportItem.Message=$testcaseResult.Message
                $ReportItem.TimeOfExecution=Get-date -Format g
                if(![string]::IsNullOrEmpty($testcaseResult.TestCase.ManualSteps)){
                    $ReportItem.ManualSteps =  $testcaseResult.TestCase.ManualSteps   
                }  

                $ReportItems += $ReportItem
            }
            $ReportItems |Export-Csv $resultPath -NoTypeInformation
        }

    
    #Fetches the summary of the test case execution
    static [void] GetTestCaseResultsSummary([TestCase[]]$testcases,[TestCaseResult[]]$testcaseresults){
        $passed = 0
        $failed = 0
        $error = 0
        $manual = 0
        $runtests = $testcases | Where-Object {$_.Enabled -eq $true}
        if(($testcaseresults | Measure-Object ).Count -gt 0){
             foreach ($testcaseresult in $testcaseresults) {
                 if($testcaseresult.TestStatus -eq "Passed"){
                     $passed += 1
                 }
                 if($testcaseresult.TestStatus -eq "Failed"){
                     $failed += 1
                 }
                 if($testcaseresult.TestStatus -eq "Error"){
                     $error += 1
                 }
                 if($testcaseresult.TestStatus -eq "Manual"){
                     $manual += 1
                 }
            }
        }

        $Summary = "" | Select-Object RunTestCases, PassedTestCases, FailedTestCases, ErrorTestCases, ManualTestCases
        $Summary.RunTestCases = ($runtests | Measure-Object).Count
        $Summary.PassedTestCases = $passed
        $Summary.FailedTestCases = $failed
        $Summary.ErrorTestCases =$error
        $Summary.ManualTestCases = $manual
    
        Write-Host  ($Summary |Format-List | Out-String) 
    }

    static [ParamSet[]] GetParamSets(){
        $paramSets = @()
        try{
            [string] $path = [CommonHelper]::GetRootPath() +"\ParamSets.json"
             $paramSets = Get-Content -Path $path | ConvertFrom-Json
        }
        catch{
            # Continue with default params when ParamSets.json file is not available
        }
        return $paramSets
        
    }

    }