Functions/Invoke-NotebookTests.ps1

Function Invoke-NotebookTests {

    param(
        $NotebookTest
    )
    $DatabricksJobRun = $null
    $notebookTestResults = $null
    $noteBookOutput = $null
    $testCaseTemplate = $null
    $ClusterId = Get-CachedDatabricksClusterId -ClusterName $NotebookTest.testClusterName
    $runId = Add-DatabricksNotebookJob -JobName $NotebookTest.jobName -ClusterId $ClusterId -NotebookPath $NotebookTest.notebookPath -NotebookParametersJson $NotebookTest.notebookparamsjson -RunImmediate
    Write-Host "Run ID - "$runId
    Write-Host "Running tests. This is the info for the job running the tests..."
    Get-DatabricksRun -RunId $runId
    $DatabricksJobRunState = $null
    $wait = 0
    do {
        Start-Sleep -Seconds 6
        $DatabricksJobRunState = Get-DatabricksRun -RunId $runId -StateOnly       
        $wait ++
    } until (($null -ne $DatabricksJobRunState) -or ($wait -eq 100))

    Write-Host "Test Completed. Check here for the status of the job."
    Get-DatabricksRun -RunId $runId

    $DatabricksJobRun = Get-DatabricksJobRun -RunId $runId -includeNoteBookOutput
    $DatabricksJobRun | ConvertTo-Json | Out-File .\notebookoutput.json
    Write-Verbose $DatabricksJobRun
    $noteBookOutput = $DatabricksJobRun.notebook_output.result
    Write-Verbose $noteBookOutput
    $notebookTestResults = ConvertFrom-Json($noteBookOutput)

    $xmlHeader = '<test-results name="##notebookPath##" total="##total##" date="##getdate##" time="##gettime##">
<environment nunit-version="2.6.0.12035" clr-version="2.0.50727.4963" os-version="Microsoft Windows NT 6.1.7600.0" platform="Win32NT" cwd="C:\Program Files\NUnit 2.6\bin" machine-name="dummymachine" user="dummyuser" user-domain="dummy"/>
<culture-info current-culture="en-US" current-uiculture="en-US"/>'

    $xmlFooter = '</results>
</test-suite>
</test-results>'


    $xmlHeader = $xmlHeader.Replace("##notebookPath##", $NotebookTest.notebookPath).Replace('##total##', $notebookTestResults.Count).Replace('##getdate##', (Get-Date -Format yyyy-MM-dd)).Replace('##gettime##', (Get-Date -Format hh:mm:ss))

    if ($notebookTestResults.result -match "failure") {
        $testSuiteResult = "failure"
        $testSuiteSuccess = "False"
    }
    else {
        $testSuiteResult = "success"
        $testSuiteSuccess = "True"
    }
    $testSuite = '<test-suite type="TestFixture" name="' + $NotebookTest.notebookPath + '" executed="True" result="' + $testSuiteResult + '" success="' + $testSuiteSuccess + '" time="0.000" asserts="0">
<results>'

    foreach ($notebookTestResult in $notebookTestResults) {
        if ($notebookTestResult.result -eq "failure") {
            $testCaseTemplate += '<test-case name="' + $notebookTestResult.test + '" description="" executed="True" result="' + $notebookTestResult.result + '" success="' + $notebookTestResult.issuccess + '" time="0.000" asserts="1">
        <failure>
        </failure>
        </test-case>'

        }
        elseif ($notebookTestResult.result -eq "success") {
            $testCaseTemplate += '<test-case name="' + $notebookTestResult.test + '" description="" executed="True" result="' + $notebookTestResult.result + '" success="' + $notebookTestResult.issuccess + '" time="0.000" asserts="1"/>
    '

        }
    }
    $testResultsFile = "$($jobName)_$($runId).xml"
    $nunitXml = $xmlHeader + $testSuite + $testCaseTemplate + $xmlFooter | Out-File .\$testResultsFile -Force
}