Skip to content

Commit c710072

Browse files
authored
Add test scripts to run locally (#258)
1 parent ed1e262 commit c710072

File tree

3 files changed

+149
-0
lines changed

3 files changed

+149
-0
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,21 @@ This provides a proxy server application of Azure OpenAI Service API that round-
8585
--parameters environmentName=$AZURE_ENV_NAME `
8686
--parameters location=$AZURE_LOCATION
8787
```
88+
89+
## Unit Testing & Integration Testing
90+
91+
1. Run the following command to run both unit tests and integration tests at once on your local machine.
92+
93+
```bash
94+
# zsh/bash
95+
./scripts/run-tests.sh -c Debug
96+
97+
# PowerShell
98+
./scripts/Run-Tests.ps1 -Configuration Debug
99+
```
100+
101+
> **NOTE**: While running the integration tests using the script above, you might be seeing the error like
102+
>
103+
> `System.IO.IOException: Failed to bind to address https://127.0.0.1:22000: address already in use.`
104+
>
105+
> It can sometimes happen. However, it doesn't impact on the test results.

scripts/Run-Tests.ps1

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Run unit tests and integration tests
2+
Param(
3+
[string]
4+
[Parameter(Mandatory=$false)]
5+
[ValidateSet("Debug", "Release")]
6+
$Configuration = "Debug",
7+
8+
[switch]
9+
[Parameter(Mandatory=$false)]
10+
$Help
11+
)
12+
13+
function Show-Usage {
14+
Write-Host " This runs both unit tests and integration tests.
15+
16+
Usage: $(Split-Path $MyInvocation.ScriptName -Leaf) ``
17+
[-Configuration <Configuration>] ``
18+
19+
[-Help]
20+
21+
Options:
22+
-Configuration Configuration. Possible values are 'Debug' or 'Release'. Default is 'Debug'.
23+
24+
-Help: Show this message.
25+
"
26+
27+
Exit 0
28+
}
29+
30+
# Builds apps
31+
Write-Host "Building apps..." -ForegroundColor Cyan
32+
33+
dotnet restore
34+
dotnet build -c $Configuration
35+
36+
# Runs unit tests
37+
Write-Host "Invoking unit tests..." -ForegroundColor Cyan
38+
39+
dotnet test ./test/AzureOpenAIProxy.AppHost.Tests -c $Configuration --no-build --logger "trx" --collect:"XPlat Code Coverage"
40+
dotnet test ./test/AzureOpenAIProxy.ApiApp.Tests -c $Configuration --no-build --logger "trx" --collect:"XPlat Code Coverage"
41+
42+
# Runs integration tests
43+
Write-Host "Invoking integration tests..." -ForegroundColor Cyan
44+
45+
$playwright = Get-ChildItem -File Microsoft.Playwright.dll -Path . -Recurse
46+
$installer = "$($playwright[0].Directory.FullName)/playwright.ps1"
47+
& "$installer" install
48+
49+
Start-Process -NoNewWindow "dotnet" @("run", "--project", "./src/AzureOpenAIProxy.AppHost", "--no-build")
50+
Start-Sleep -s 30
51+
52+
dotnet test ./test/AzureOpenAIProxy.PlaygroundApp.Tests -c $Configuration --logger "trx" --collect:"XPlat Code Coverage"
53+
54+
# Cleans up
55+
$process = if ($IsWindows -eq $true) {
56+
Get-Process | Where-Object { $_.ProcessName -eq "AzureOpenAIProxy.AppHost" }
57+
} else {
58+
Get-Process | Where-Object { $_.Path -like "*AzureOpenAIProxy.AppHost" }
59+
}
60+
Stop-Process -Id $process.Id

scripts/run-tests.sh

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/bin/bash
2+
# Run unit tests and integration tests
3+
4+
set -e
5+
6+
# Function to show usage
7+
usage() {
8+
echo " This runs both unit tests and integration tests.
9+
10+
Usage: $0 [-c|--config|--configuration <Configuration>] [-h|--help]
11+
12+
Options:
13+
-c|--config|--configuration Configuration. Possible values are 'Debug' or 'Release'. Default is 'Debug'.
14+
-h|--help Show this message.
15+
"
16+
exit 0
17+
}
18+
19+
# Default configuration
20+
CONFIGURATION="Debug"
21+
22+
if [[ $# -eq 0 ]]; then
23+
CONFIGURATION="Debug"
24+
fi
25+
26+
while [[ "$1" != "" ]]; do
27+
case $1 in
28+
-c | --config | --configuration)
29+
shift
30+
CONFIGURATION="$1"
31+
;;
32+
33+
-h | --help)
34+
usage
35+
exit 0
36+
;;
37+
38+
*)
39+
usage
40+
exit 0
41+
;;
42+
esac
43+
44+
shift
45+
done
46+
47+
# Builds apps
48+
echo -e "\033[36mBuilding apps...\033[0m"
49+
50+
dotnet restore
51+
dotnet build -c $CONFIGURATION
52+
53+
# Runs unit tests
54+
echo -e "\033[36mInvoking unit tests...\033[0m"
55+
56+
dotnet test ./test/AzureOpenAIProxy.AppHost.Tests -c $CONFIGURATION --no-build --logger "trx" --collect:"XPlat Code Coverage"
57+
dotnet test ./test/AzureOpenAIProxy.ApiApp.Tests -c $CONFIGURATION --no-build --logger "trx" --collect:"XPlat Code Coverage"
58+
59+
# Runs integration tests
60+
echo -e "\033[36mInvoking integration tests...\033[0m"
61+
62+
pwsh ./test/AzureOpenAIProxy.PlaygroundApp.Tests/bin/Debug/net8.0/playwright.ps1 install
63+
64+
dotnet run --project ./src/AzureOpenAIProxy.AppHost --no-build &
65+
APP_PID=$!
66+
sleep 30
67+
68+
dotnet test ./test/AzureOpenAIProxy.PlaygroundApp.Tests -c $CONFIGURATION --logger "trx" --collect:"XPlat Code Coverage"
69+
70+
# Cleans up
71+
kill $APP_PID

0 commit comments

Comments
 (0)