Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ package-dev/alias-references.txt
# ignore integration test files
Samples/IntegrationTest*
unity.log
unity-test.log

# Ignore package release
test-package-release/
Expand Down
26 changes: 20 additions & 6 deletions Agents.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,19 @@ dotnet msbuild /t:DownloadNativeSDKs src/Sentry.Unity
# Build the Unity SDK
dotnet build

# Run all tests
./test.sh
# Run all tests (builds SDK first)
pwsh scripts/run-tests.ps1

# Run specific test targets
dotnet msbuild /t:UnityEditModeTest /p:Configuration=Release test/Sentry.Unity.Editor.Tests
dotnet msbuild /t:UnityPlayModeTest /p:Configuration=Release
# Run specific test types
pwsh scripts/run-tests.ps1 -PlayMode
pwsh scripts/run-tests.ps1 -EditMode

# Run filtered tests
pwsh scripts/run-tests.ps1 -Filter "TestClassName"
pwsh scripts/run-tests.ps1 -PlayMode -Filter "Throttler"

# Skip build for faster iteration
pwsh scripts/run-tests.ps1 -SkipBuild -Filter "MyTest"

# Integration testing (local)
./test/Scripts.Integration.Test/integration-test.ps1 -Platform "macOS" -UnityVersion "2021.3.45f2"
Expand Down Expand Up @@ -482,7 +489,14 @@ Key options:
### Running All Tests

```bash
./test.sh
# Run all tests (builds SDK first)
pwsh scripts/run-tests.ps1

# Run with filtering
pwsh scripts/run-tests.ps1 -Filter "TestClassName"

# Skip build for faster iteration
pwsh scripts/run-tests.ps1 -SkipBuild
```

### Integration Test Scripts
Expand Down
102 changes: 2 additions & 100 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ Related: https://forum.unity.com/threads/6572-debugger-agent-unable-to-listen-on
<!-- Unity exits with a non-zero exit code when running tests. We ignore it and manually check the test results instead. -->
<Delete Files="$(UnityTestPlayModeResultFilePath)" />
<Exec EnvironmentVariables="IgnoreExitCode=true" Command="pwsh &quot;$(RepoRoot)scripts/unity.ps1&quot; $(UnityExec) -batchmode -nographics -runTests -testPlatform PlayMode -projectPath $(UnitySampleProjectPath) -testResults $(UnityTestPlayModeResultFilePath)" />
<UnityTestResults Path="$(UnityTestPlayModeResultFilePath)" />
<Exec Command="pwsh &quot;$(RepoRoot)scripts/report-test-results.ps1&quot; &quot;$(UnityTestPlayModeResultFilePath)&quot;" />
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This now reuses the script that parses the test result for local development.

</Target>

<!-- Run EditMode tests with dotnet msbuild /t:UnityEditModeTest test/Sentry.Unity.Editor.Tests -->
Expand All @@ -411,7 +411,7 @@ Related: https://forum.unity.com/threads/6572-debugger-agent-unable-to-listen-on
<!-- IgnoreStandardErrorWarningFormat="true" because of the intentional compilation error printed in GenerateOptions_NewSentryOptionsGarbageAppended_FailsToCompile(). -->
<Delete Files="$(UnityTestEditModeResultFilePath)" />
<Exec EnvironmentVariables="IgnoreExitCode=true" IgnoreStandardErrorWarningFormat="true" Command="pwsh &quot;$(RepoRoot)scripts/unity.ps1&quot; $(UnityExec) -batchmode -nographics -runTests -testPlatform EditMode -projectPath $(UnitySampleProjectPath) -testResults $(UnityTestEditModeResultFilePath)"/>
<UnityTestResults Path="$(UnityTestEditModeResultFilePath)" />
<Exec Command="pwsh &quot;$(RepoRoot)scripts/report-test-results.ps1&quot; &quot;$(UnityTestEditModeResultFilePath)&quot;" />
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This now reuses the script that parses the test result for local development.

</Target>

<!-- Locate the TestRunner.dlls by filling the wildcard with the template version number. 3d is the default template -->
Expand Down Expand Up @@ -465,104 +465,6 @@ File.WriteAllLines(PackageManifestFile, lines);
</Task>
</UsingTask>

<!-- Parse test results -->
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This now happens in report-test-results.ps1

<UsingTask TaskName="UnityTestResults" TaskFactory="RoslynCodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
<ParameterGroup>
<Path ParameterType="System.String" Required="true" />
</ParameterGroup>

<Task>
<Using Namespace="System" />
<Using Namespace="System.IO" />
<Using Namespace="System.Linq" />
<Using Namespace="System.Xml.Linq" />
<Code Type="Fragment" Language="cs">
<![CDATA[

if (!File.Exists(Path))
{
Log.LogError("Test results file not found at " + Path);
return false;
}

var document = XDocument.Load(Path);
var testRun = document.Descendants("test-run").First();

var total = testRun.Attribute("total").Value;
if (total == "0")
{
Log.LogError("Unity test results is empty.");
return false;
}

var result = testRun.Attribute("result").Value;

Log.LogCriticalMessage("UnityTestResults", null, null, null, 0, 0, 0, 0, "{0} in {1}s", result.Replace("(Child)", ""), testRun.Attribute("duration").Value);
Log.LogCriticalMessage("UnityTestResults", null, null, null, 0, 0, 0, 0, " Passed: {0,3}", testRun.Attribute("passed").Value);
Log.LogCriticalMessage("UnityTestResults", null, null, null, 0, 0, 0, 0, " Failed: {0,3}", testRun.Attribute("failed").Value);
Log.LogCriticalMessage("UnityTestResults", null, null, null, 0, 0, 0, 0, " Skipped: {0,3}", testRun.Attribute("skipped").Value);
Log.LogCriticalMessage("UnityTestResults", null, null, null, 0, 0, 0, 0, " Inconclusive: {0,3}", testRun.Attribute("inconclusive").Value);

if (result == "Passed")
{
return true;
}

PrintFailedTests(testRun);

var failed = testRun.Attribute("failed").Value;
var errorMessage = $"Test run completed with {failed} failing test{(int.Parse(failed) > 1 ? "s" : "")}.";
Log.LogError(errorMessage);

Success = false;

void PrintFailedTests(XElement element)
{
foreach (var descendant in element.Descendants())
{
if (descendant.Name != "test-case"
|| descendant.Attribute("result")?.Value != "Failed")
{
continue;
}

if (descendant.Descendants().Any(d => d.Name == "test-case"))
{
PrintFailedTests(descendant);
}
else
{
var sb = new StringBuilder()
.Append("Test ")
.Append(descendant.Attribute("id")?.Value)
.Append(": ")
.AppendLine(descendant.Attribute("name")?.Value);

var failure = descendant.Descendants("failure")
.Descendants("message")
.FirstOrDefault()
?.Value;

var stack = descendant.Descendants("failure")
.Descendants("stack-trace")
.FirstOrDefault()
?.Value;

sb.AppendLine(failure)
.Append("Test StackTrace: ")
.AppendLine(stack);

// MSBuild is breaking each line as if it was an error per line and not a single error.
// So Log.LogError got replaced by Console.WriteLine for now.
Console.WriteLine(sb.ToString());
}
}
}
]]>
</Code>
</Task>
</UsingTask>

<!-- Checks if the environment variable 'SENTRY_AUTH_TOKEN' has been set and creates the SentryCliOptions.asset for the sample project
This is meant for developers - so they don't have to configure the CLI options after each clean checkout (or git clean).
Gets automatically run after 'DownloadNativeSDKs'
Expand Down
91 changes: 91 additions & 0 deletions scripts/report-test-results.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<#
.SYNOPSIS
Reports Unity test results from NUnit XML file.

.DESCRIPTION
Parses NUnit XML test results and prints a summary. Exits with code 1 if tests failed.
Designed to be called from MSBuild targets.

.PARAMETER Path
Path to the NUnit XML test results file.

.EXAMPLE
pwsh scripts/report-test-results.ps1 artifacts/test/playmode/results.xml
#>

param(
[Parameter(Mandatory = $true, Position = 0)]
[string] $Path
)

Set-StrictMode -Version latest
$ErrorActionPreference = "Stop"

. $PSScriptRoot/test-utils.ps1

if (-not (Test-Path $Path)) {
Write-Host "Test results file not found at $Path" -ForegroundColor Red
exit 1
}

$results = Parse-TestResults $Path

if ($null -eq $results) {
Write-Host "Failed to parse test results" -ForegroundColor Red
exit 1
}

if ($results.Total -eq 0) {
Write-Host "Unity test results is empty." -ForegroundColor Red
exit 1
}

# Print summary (matching format from original C# implementation)
$status = if ($results.Success) { "Passed" } else { "Failed" }
Write-Host "$status in $($results.Duration)s"
Write-Host (" Passed: {0,3}" -f $results.Passed)
Write-Host (" Failed: {0,3}" -f $results.Failed)
Write-Host (" Skipped: {0,3}" -f $results.Skipped)
Write-Host (" Inconclusive: {0,3}" -f $results.Inconclusive)

# Print failed test details
if ($results.Failed -gt 0) {
Write-Host ""

# Re-parse to get stack traces (not included in Parse-TestResults)
[xml]$xml = Get-Content $Path
$failedNodes = $xml.SelectNodes("//test-case[@result='Failed']")

foreach ($node in $failedNodes) {
# Skip parent test-cases that contain child test-cases
if ($node.SelectNodes(".//test-case").Count -gt 0) {
continue
}

$name = $node.GetAttribute("name")
$id = $node.GetAttribute("id")
Write-Host "Test $id`: $name"

$message = $node.SelectSingleNode("failure/message")
if ($message) {
Write-Host $message.InnerText
}

$stackTrace = $node.SelectSingleNode("failure/stack-trace")
if ($stackTrace) {
Write-Host "Test StackTrace:"
Write-Host $stackTrace.InnerText
}

Write-Host ""
}

$testWord = if ($results.Failed -gt 1) { "tests" } else { "test" }
Write-Host "Test run completed with $($results.Failed) failing $testWord." -ForegroundColor Red
}

# Exit based on overall success (handles edge cases where result != "Passed" but failed count is 0)
if (-not $results.Success) {
exit 1
}
exit 0
Loading
Loading