Testing/Invoke-BCTest.ps1

<#
.synopsis
    Executes tests in a container
.description
    Executes tests in a container for a specific test suite and optionally a specific codeunit and method
.parameter ResultPath
    Path where the test results should be provided in
.parameter ContainerName
    Container to be used. Can be provided in the settings.json
.parameter TestSuite
    Optional to define the test suite to be used. "DEFAULT" will be used, if not specified
.parameter TestCodeunit
    Optional codeunit to be tested. If not specified, all found test codeunits will be executed
.parameter TestMethod
    Optional method to be tested. If not specific, all found methods in the test codeunit or all test codeunits will be executed
.parameter Credential
    Optional credentials to access the container NAV/BC Version. Will be read from settings.json if not specified
.parameter DoNotPrepTestSuite
    Add this switch, if the test suite was already configured with all the tests to be executed
.example
    Invoke-BCTest -ResultPath "C:\" -ContainerName test
#>

function Invoke-BCTest
{
    Param(
        [Parameter(Mandatory=$false)]
        [string]$ResultPath = '',
        [Parameter(Mandatory=$false)]
        [string]$ContainerName,
        # Name of test suite to run
        [Parameter(Mandatory=$false)]
        [string]
        $TestSuite = 'DEFAULT',
        # Number of test codeunit to run
        [Parameter(Mandatory=$false)]
        [string]
        $TestCodeunit ='*',
        # Name of test method to run
        [Parameter(Mandatory=$false)]
        [string]
        $TestMethod = '*',
        [Parameter(Mandatory=$false)]
        [PSCredential]$Credential = (Get-CredentialFromEnvironmentJson),
        [Parameter(Mandatory=$false)]
        [switch]$DoNotPrepTestSuite
    )

    if ($ResultPath -ne '') {
        if (!(Test-Path $ResultPath)) {
            New-Item $ResultPath -ItemType Directory
        }
    }

    if ($ContainerName -eq '') {
        $ContainerName = (Get-EnvironmentKeyValue -KeyName 'name')
    }

    Write-Output -ForegroundColor Green "Run tests in container $ContainerName, suite $TestSuite, codeunit $TestCodeunit, method $TestMethod"

    if (!($DoNotPrepTestSuite.IsPresent)) {
        [version]$platform = Get-AppKeyValue -KeyName "platform"
        if ($platform.Major -gt 13) {
            try {
                $startid = ((Get-AppKeyValue -KeyName 'idRanges') | Select-Object -First 1).from
                $endid = ((Get-AppKeyValue -KeyName 'idRanges') | Select-Object -Last 1).to
                }
            catch {
                $startid = (Get-AppKeyValue -KeyName 'idRange').from
                $endid = (Get-AppKeyValue -KeyName 'idRange').to
            }
        }
        else {
            $startid = (Get-AppKeyValue -KeyName 'idRange').from
            $endid = (Get-AppKeyValue -KeyName 'idRange').to
        }

        Get-TestCodeunitsInContainer -ContainerName $ContainerName -TestSuite $TestSuite -Credential $Credential -StartId $startid -EndId $endid
    }

    Write-Output 'Executing tests in client'

    Run-TestsInNavContainer -containerName $ContainerName -credential $Credential -XUnitResultFileName "C:\ProgramData\NavContainerHelper\Extensions\$ContainerName\my\Results.xml" -detailed -testSuite $TestSuite -testCodeunit $TestCodeunit -testFunction $TestMethod -returnTrueIfAllPassed -AzureDevOps error

    if ($ResultPath -ne '') {
        Write-Output "Copying results to publishing folder ($ResultPath)"
        Copy-Item "C:\ProgramData\NavContainerHelper\Extensions\$ContainerName\my\Results.xml" $ResultPath -Force
    }
}

Export-ModuleMember -Function Invoke-BCTest