Testing/Run-NAVTests.ps1

function Run-NAVTests
{
    Param(
        [Parameter(Mandatory=$false)]
        [string]$ServerInstance = '',
        [Parameter(Mandatory=$false)]
        [string]$ResultPath = 'C:\Temp\Results.xml',
        [ValidateSet('All','Failures')]
        [string]$OutputTests = 'All',
        [Parameter(Mandatory=$false)]
        [string]$ContainerName = '',
        [Parameter(Mandatory=$false)]
        [System.Management.Automation.PSCredential]$Credential
    )

    if ((Get-IsALRepo) -and ($ContainerName -eq '')) {
        $ContainerName = (Split-Path (Get-Location) -Leaf)
    }

    if ($ContainerName -eq '') {
        if ($ServerInstance -eq '') {
            $ServerInstance = Select-ServiceTier
        }

        if ($ServerInstance -eq '') {
            return
        }
    }

    $TempPath = Create-TempDirectory

    if (Test-Path $ResultPath)
    {
        Remove-Item $ResultPath
    }

    if (($ContainerName -ne '') -and ($Credential -eq $null)) {
        $Password = ConvertTo-SecureString (Get-EnvironmentKeyValue -KeyName 'password') -AsPlainText -Force
        $Credential = New-Object System.Management.Automation.PSCredential((Get-EnvironmentKeyValue -KeyName 'user'), $Password)
    }

    if ($ContainerName -ne '') {
        Write-Host -ForegroundColor Green "Running tests for $ContainerName"
    }
    else {
        Write-Host -ForegroundColor Green "Running tests for $ServerInstance"
    }

    $AutoTestCodeunitContent = Get-Content (Join-Path $PSScriptRoot 'COD90101.TXT') -Raw
    $AutoTestCodeunitContent = $AutoTestCodeunitContent.Replace('$ResultsPath',$ResultPath)

    $AutoTestCodeunitFile = Join-Path (Create-TempDirectory) 'COD90101.TXT'
    Add-Content $AutoTestCodeunitFile $AutoTestCodeunitContent

    if ($ContainerName -ne '') {
        Import-ObjectsToNavContainer -containerName $ContainerName -objectsFile $AutoTestCodeunitFile -sqlCredential $Credential
        Compile-ObjectsInNavContainer -containerName $ContainerName -filter 'Compiled=No' -sqlCredential $Credential
    }
    else {
        Import-NAVApplicationObject2 -ServerInstance $ServerInstance -Path $AutoTestCodeunitFile -LogPath $TempPath
        Compile-NAVApplicationObject2 -ServerInstance $ServerInstance -Filter 'ID=90101' -LogPath $TempPath
    }

    Remove-Item (Split-Path $AutoTestCodeunitFile -Parent) -Recurse -Force  

    $ClientProcess = Open-NavWindowsClient -ServerInstance $ServerInstance -ObjectType Codeunit -ObjectID 90101 -WindowStyle Minimized -ContainerName $ContainerName -Credential $Credential

    Write-Host 'Executing tests in client'

    while (!(Test-Path $ResultPath)) {
        Write-Host '.' -NoNewline
        Sleep -Seconds 3
    }

    if ($ContainerName -eq '') {
        Delete-NAVApplicationObject2 -ServerInstance $ServerInstance -Filter 'ID=90101;Type=Codeunit' -LogPath $TempPath -Confirm:$false
    }

    [xml]$Results = Get-Content $ResultPath

    $ResultObjects = @()

    if ($OutputTests -eq 'Failures')
    {
        $XPath = "/TestResults/TestResult[Result='Failed']"
    }
    else
    {
        $XPath = '/TestResults/TestResult'
    }

    foreach ($TestResult in $Results.SelectNodes($XPath))
    {
        $ResultObject = New-Object System.Object
        $ResultObject | Add-Member -MemberType NoteProperty -Name Codeunit -Value $TestResult.CUName
        $ResultObject | Add-Member -MemberType NoteProperty -Name Function -Value $TestResult.FName
        $ResultObject | Add-Member -MemberType NoteProperty -Name Result -Value $TestResult.Result
        $ResultObject | Add-Member -MemberType NoteProperty -Name ErrorMessage -Value $TestResult.ErrorMessage
        $ResultObject | Add-Member -MemberType NoteProperty -Name CallStack -Value ($TestResult.CallStack).Replace('\',"`n")
        $ResultObjects += $ResultObject
    }

    $ResultObjects

    Remove-Item $TempPath -Force -Recurse

    Stop-Process -Id $ClientProcess.ID
}

Export-ModuleMember -Function Run-NAVTests