Unity Test Framework CLI automation and test writing patterns. Masters batchmode execution, NUnit assertions, EditMode/PlayMode testing, and TDD workflows...
Unity Test Framework provides NUnit-based testing with CLI automation for batch execution, CI/CD pipelines, and TDD workflows.
Core Topics:
Foundation Required: csharp-plugin:csharp-code-style (naming conventions, code organization)
Optional Integrations:
unity-vcontainer: DI-based test mockingunity-unitask: Async test patternsunity-async: Coroutine testing utilitiesLearning Path: Test basics → CLI automation → Advanced patterns → CI/CD integration
using NUnit.Framework;
[TestFixture]
public class PlayerServiceTests
{
private PlayerService mPlayerService;
[SetUp]
public void SetUp()
{
mPlayerService = new PlayerService();
}
[Test]
public void Initialize_WhenCalled_SetsHealthToMax()
{
// Arrange
int expected = 100;
// Act
mPlayerService.Initialize();
// Assert
Assert.AreEqual(expected, mPlayerService.Health);
}
[TearDown]
public void TearDown()
{
mPlayerService = null;
}
}
& "C:\Program Files\Unity\Hub\Editor\{VERSION}\Editor\Unity.exe" `
-batchmode `
-nographics `
-projectPath "{PROJECT_PATH}" `
-runTests `
-testPlatform editmode `
-testResults "{TEMP}\results.xml" `
-logFile "{TEMP}\test.log" `
-quit
& "C:\Program Files\Unity\Hub\Editor\{VERSION}\Editor\Unity.exe" `
-batchmode `
-projectPath "{PROJECT_PATH}" `
-runTests `
-testPlatform playmode `
-testResults "{TEMP}\results.xml" `
-logFile "{TEMP}\test.log" `
-quit
Note: PlayMode cannot use -nographics (requires scene rendering)
Complete CLI reference and automation:
NUnit patterns and best practices:
Test result processing:
# Filter by test name (semicolon-separated)
-testFilter "LoginTest;AuthTest"
# Filter by regex pattern
-testFilter ".*Service.*"
# Filter by category
-testCategory "Unit;Integration"
# Filter by assembly
-assemblyNames "Game.Domain.Tests;Game.Service.Tests"
# Parse ProjectVersion.txt
$versionFile = Get-Content "{PROJECT_PATH}/ProjectSettings/ProjectVersion.txt"
$version = ($versionFile | Select-String "m_EditorVersion: (.+)").Matches.Groups[1].Value
# Construct Unity Editor path
$unityExe = "C:\Program Files\Unity\Hub\Editor\$version\Editor\Unity.exe"
Priority pattern for finding test assemblies:
1. {AssemblyName}.Tests.asmdef (same folder or Tests subfolder)
2. Tests/{AssemblyName}/*.asmdef
3. Assets/Tests/EditMode/*.asmdef (fallback)
Example:
Changed: Assets/Scripts/Domain/LoginService.cs
-> asmdef: Game.Domain.asmdef
-> Tests: Game.Domain.Tests.asmdef
-> Filter: -assemblyNames "Game.Domain.Tests"
| Exit Code | Meaning | Action |
|---|---|---|
| 0 | All tests passed | Display summary |
| 2 | Test failures | Display failed test details |
| 1 | Other errors | Check log file for details |
Unity Test Results (EditMode)
-----------------------------------
Passed: 15
Failed: 2
Skipped: 1
Duration: 3.45s
Failed Tests:
1. LoginServiceTests.LoginWithInvalidCredentials_ThrowsException
-> Expected: ArgumentException
-> Actual: No exception thrown
2. AuthTests.TokenExpiry_ShouldInvalidateSession
-> Assert.AreEqual failed
-> Expected: False, Actual: True
$unityExe = "C:\Program Files\Unity\Hub\Editor\{VERSION}\Editor\Unity.exe"
unityExe="/Applications/Unity/Hub/Editor/{VERSION}/Unity.app/Contents/MacOS/Unity"
# Use environment variable for flexibility
$unityExe = $env:UNITY_EDITOR_PATH
if (-not $unityExe) {
# Fallback to Unity Hub path
}
| Error | Resolution |
|---|---|
| Unity Editor path not found | Set UNITY_EDITOR_PATH environment variable |
| ProjectVersion.txt missing | Verify Unity project root location |
| Test timeout | Use -playerHeartbeatTimeout option (default: 600s) |
| License error | Verify Unity license activation |