Testing/Run-BCTests.ps1

function Run-BCTests
{
    Param(
        [Parameter(Mandatory=$false)]
        [string]$ResultPath = '',
        [Parameter(Mandatory=$false)]
        [string]$ContainerName = (Get-ContainerFromLaunchJson),
        [Parameter(Mandatory=$false)]
        [System.Management.Automation.PSCredential]$Credential,
        [Parameter(Mandatory=$false)]
        [switch]$DoNotPrepTestSuite
    )

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

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

    if (($ContainerName -ne '') -and ($null -eq $Credential)) {
        $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"
    }
    
    if (!($DoNotPrepTestSuite.IsPresent)) {
        $BuildHelperApp = Get-AppFromLastSuccessfulBuild -ProjectName 'TFS Tools' -RepositoryName BuildHelper
        Publish-NavContainerApp -containerName $ContainerName -appFile $BuildHelperApp.FullName -install -sync

        if ((Get-NavContainerCountry $ContainerName) -eq 'dk') { 
            $CompanyName = (Get-CompanyInNavContainer $ContainerName).Item(1).CompanyName
        }
        else {
            if ($null -eq (Get-CompanyInNavContainer -containerName $ContainerName).Item(0).CompanyName) {
                $CompanyName = (Get-CompanyInNavContainer $ContainerName).CompanyName
            }
            else {
                $CompanyName = (Get-CompanyInNavContainer $ContainerName).Item(0).CompanyName
            }
        }

        $AutomatedTestMgt = New-WebServiceProxy -Uri ("http://{0}:7047/NAV/WS/{1}/Codeunit/AutomatedTestMgt" -f (Get-NavContainerIpAddress -containerName $ContainerName), $CompanyName) -Credential $Credential
        $AutomatedTestMgt.GetTests()
    }

    Write-Host 'Executing tests in client'

    Run-TestsInNavContainer -containerName $ContainerName -credential $Credential -XUnitResultFileName "C:\ProgramData\NavContainerHelper\Extensions\$ContainerName\my\Results.xml" -detailed

    if ($ResultPath -ne '') {
        Copy-Item "C:\ProgramData\NavContainerHelper\Extensions\$ContainerName\my\Results.xml" $ResultPath -Force
    }
}

Export-ModuleMember -Function Run-BCTests