diff --git a/README.md b/README.md index eaebf904..237f114c 100644 --- a/README.md +++ b/README.md @@ -85,3 +85,21 @@ This provides a proxy server application of Azure OpenAI Service API that round- --parameters environmentName=$AZURE_ENV_NAME ` --parameters location=$AZURE_LOCATION ``` + +## Unit Testing & Integration Testing + +1. Run the following command to run both unit tests and integration tests at once on your local machine. + + ```bash + # zsh/bash + ./scripts/run-tests.sh -c Debug + + # PowerShell + ./scripts/Run-Tests.ps1 -Configuration Debug + ``` + + > **NOTE**: While running the integration tests using the script above, you might be seeing the error like + > + > `System.IO.IOException: Failed to bind to address https://127.0.0.1:22000: address already in use.` + > + > It can sometimes happen. However, it doesn't impact on the test results. diff --git a/scripts/Run-Tests.ps1 b/scripts/Run-Tests.ps1 new file mode 100644 index 00000000..69f36529 --- /dev/null +++ b/scripts/Run-Tests.ps1 @@ -0,0 +1,60 @@ +# Run unit tests and integration tests +Param( + [string] + [Parameter(Mandatory=$false)] + [ValidateSet("Debug", "Release")] + $Configuration = "Debug", + + [switch] + [Parameter(Mandatory=$false)] + $Help +) + +function Show-Usage { + Write-Host " This runs both unit tests and integration tests. + + Usage: $(Split-Path $MyInvocation.ScriptName -Leaf) `` + [-Configuration ] `` + + [-Help] + + Options: + -Configuration Configuration. Possible values are 'Debug' or 'Release'. Default is 'Debug'. + + -Help: Show this message. +" + + Exit 0 +} + +# Builds apps +Write-Host "Building apps..." -ForegroundColor Cyan + +dotnet restore +dotnet build -c $Configuration + +# Runs unit tests +Write-Host "Invoking unit tests..." -ForegroundColor Cyan + +dotnet test ./test/AzureOpenAIProxy.AppHost.Tests -c $Configuration --no-build --logger "trx" --collect:"XPlat Code Coverage" +dotnet test ./test/AzureOpenAIProxy.ApiApp.Tests -c $Configuration --no-build --logger "trx" --collect:"XPlat Code Coverage" + +# Runs integration tests +Write-Host "Invoking integration tests..." -ForegroundColor Cyan + +$playwright = Get-ChildItem -File Microsoft.Playwright.dll -Path . -Recurse +$installer = "$($playwright[0].Directory.FullName)/playwright.ps1" +& "$installer" install + +Start-Process -NoNewWindow "dotnet" @("run", "--project", "./src/AzureOpenAIProxy.AppHost", "--no-build") +Start-Sleep -s 30 + +dotnet test ./test/AzureOpenAIProxy.PlaygroundApp.Tests -c $Configuration --logger "trx" --collect:"XPlat Code Coverage" + +# Cleans up +$process = if ($IsWindows -eq $true) { + Get-Process | Where-Object { $_.ProcessName -eq "AzureOpenAIProxy.AppHost" } +} else { + Get-Process | Where-Object { $_.Path -like "*AzureOpenAIProxy.AppHost" } +} +Stop-Process -Id $process.Id diff --git a/scripts/run-tests.sh b/scripts/run-tests.sh new file mode 100755 index 00000000..a431504e --- /dev/null +++ b/scripts/run-tests.sh @@ -0,0 +1,71 @@ +#!/bin/bash +# Run unit tests and integration tests + +set -e + +# Function to show usage +usage() { + echo " This runs both unit tests and integration tests. + + Usage: $0 [-c|--config|--configuration ] [-h|--help] + + Options: + -c|--config|--configuration Configuration. Possible values are 'Debug' or 'Release'. Default is 'Debug'. + -h|--help Show this message. +" + exit 0 +} + +# Default configuration +CONFIGURATION="Debug" + +if [[ $# -eq 0 ]]; then + CONFIGURATION="Debug" +fi + +while [[ "$1" != "" ]]; do + case $1 in + -c | --config | --configuration) + shift + CONFIGURATION="$1" + ;; + + -h | --help) + usage + exit 0 + ;; + + *) + usage + exit 0 + ;; + esac + + shift +done + +# Builds apps +echo -e "\033[36mBuilding apps...\033[0m" + +dotnet restore +dotnet build -c $CONFIGURATION + +# Runs unit tests +echo -e "\033[36mInvoking unit tests...\033[0m" + +dotnet test ./test/AzureOpenAIProxy.AppHost.Tests -c $CONFIGURATION --no-build --logger "trx" --collect:"XPlat Code Coverage" +dotnet test ./test/AzureOpenAIProxy.ApiApp.Tests -c $CONFIGURATION --no-build --logger "trx" --collect:"XPlat Code Coverage" + +# Runs integration tests +echo -e "\033[36mInvoking integration tests...\033[0m" + +pwsh ./test/AzureOpenAIProxy.PlaygroundApp.Tests/bin/Debug/net8.0/playwright.ps1 install + +dotnet run --project ./src/AzureOpenAIProxy.AppHost --no-build & +APP_PID=$! +sleep 30 + +dotnet test ./test/AzureOpenAIProxy.PlaygroundApp.Tests -c $CONFIGURATION --logger "trx" --collect:"XPlat Code Coverage" + +# Cleans up +kill $APP_PID