# Unity Test Framework
Automated tests are how you keep a growing project honest. **Unity Test Framework** (`com.unity.test-framework`) runs [NUnit](https://docs.nunit.org/) tests inside the Editor: fast **Edit Mode** tests for pure logic and ScriptableObject math, and **Play Mode** tests that spin up scenes, physics, input and UI. This series is written for an experienced Unity developer who also teaches. You'll leave with a test assembly layout, a worked example against inventory and stat logic, and a command-line recipe you can drop into CI. The next tutorial in this batch, Build Automation and CI, is where that recipe lives in a full pipeline.
**Target:** Unity **6000.5+**. VR work in this vault targets **Unity 6.3 LTS (6000.3.27f2+)** ([[0.Choosing a Unity Version]]); Test Framework is also a core package on 6.3, so the APIs in these notes apply, but confirm the Test Runner window and command-line help against your installed Editor.
> [!warning] Code not compiled
> None of the C# in this series has been compiled or run in the Unity Test Runner. API names were checked against the Unity 6000.5 Test Framework manual and Scripting API on 2026-11-10.
> [!note] Verification status
> **Package (2026-11-10):** Test Framework is a **core package** on Unity 6000.5. Core packages ship with the Editor; you cannot pick a different version in Package Manager. The exact patch string your 6000.5 install embeds is therefore **unverified here** (open the package in Package Manager to read it). Package docs for the 1.7 line and the 6000.5 manual describe the same public surface used below (`[Test]`, `[UnityTest]`, async `Task` tests, `[UnitySetUp]` / `[UnityTearDown]`, `[UnityOneTimeSetUp]` / `[UnityOneTimeTearDown]`, `IPrebuildSetup`, `LogAssert`, Test Runner at **Window > General > Test Runner**).
> **Also used:** Performance testing API (`com.unity.test-framework.performance`) **3.5.0** is the version released for 6000.5; **3.7.0** is also listed as released and available. Code Coverage (`com.unity.testtools.codecoverage`) **1.3.0** is the version released for 6000.5.
> **Verified APIs:** `NUnit.Framework` attributes and assertions listed in part 2; `UnityEngine.TestTools` attributes and yield instructions listed in parts 3-4; command-line flags `-runTests`, `-testPlatform`, `-testResults`, `-testFilter`, `-testCategory`, `-assemblyNames` as documented in the Test Framework command-line topic (see Sources).
> **Not verified:** the exact 6000.5 embedded Test Framework / `com.unity.ext.nunit` patch numbers; UI Test Framework (`com.unity.ui.test-framework`, also core) APIs (mentioned only); some Test Runner context-menu wording.
## Parts
| # | Note | Covers |
|---|---|---|
| 1 | [[1. Test Assemblies and the Test Runner]] | Mental model, creating a test assembly, Edit Mode vs Play Mode, folder layout |
| 2 | [[2. Edit Mode Tests and NUnit]] | `[Test]`, assertions, `[SetUp]` / `[TearDown]`, parameterized tests, categories |
| 3 | [[3. UnityTest Async Tests and Domain Reloads]] | `[UnityTest]`, `[UnitySetUp]`, async `Task` tests, `EnterPlayMode`, `RecompileScripts` |
| 4 | [[4. Play Mode Tests and Scene Fixtures]] | Play Mode assemblies, loading scenes, `IPrebuildSetup`, running in a Player |
| 5 | [[5. Worked Example - Inventory and Stats Tests]] | Testing a bag and a modifier stack; a Play Mode spawn test |
| 6 | [[6. Performance Tests Coverage and Command Line]] | Performance testing API, Code Coverage, `-runTests` for CI |
| 7 | [[7. Test Framework Pitfalls]] | Pitfalls, flaky tests, UI Test Framework pointer |
## Design lens (Level Up!)
Rogers treats a game as something you **play-test and iterate** (Level 1, Level 9, Level 18 in [[Level Up! (Review)]]). Automated tests do not replace play-testing a metrics gym ([[5. Gray-Box Metrics Gym]]) or a class critique. They protect the rules underneath that play: damage formulas ([[5.Damage Formulas]]), inventory constraints ([[Inventory Systems]]), quest state ([[Quest Systems]]). When a student "fixes" a jump and silently breaks coyote time, a one-line Edit Mode test is the cheapest way to notice.
## Patterns used (Game Programming Patterns)
- **Command** - a test is a packaged action with a pass/fail result. The Test Runner is the invoker.
- **Type Object / Flyweight** - ScriptableObject definitions are easy to construct in Edit Mode tests; don't load unique assets from `Assets/` if a `ScriptableObject.CreateInstance` will do.
- **Subclass Sandbox** - `[SetUp]` / `[TearDown]` on a fixture base class gives every test the same world, then the test method fills in the interesting part.
- **Service Locator** - if production code reaches out to a static `GameManager.Instance`, tests become painful. Inject a service, or provide a test double. See [[Game Programming Patterns (Review)]].
## Related (existing)
- [[Architecture and Code Patterns]] and [[ScriptableObject Architecture]] - asmdefs and data-driven types that are actually testable
- [[Inventory Systems]], [[Quest Systems]], [[Stat-Driven RPG Systems]] - the systems part 5 practises against
- [[High Scores, Saves and Player Stats]] - serialize/deserialize tests
- [[Custom Inspectors and Editor Tooling]] - Edit Mode tests can drive editor utilities, carefully
- [[DOTS]] - Jobs/Burst code is especially worth testing; Entities has its own test utilities (out of scope here)
- [[Version Control]] - tests belong in the repo, not only on one machine
## Sources
- Testing your code / running tests (6000.5): https://docs.unity.com/en-us/engine/6000.5/manual/scripting/test-framework-introduction/running-tests
- Create a test assembly (6000.5): https://docs.unity.com/en-us/engine/6000.5/manual/scripting/test-framework-introduction/getting-started/workflow-create-test-assembly
- Core packages (Test Framework is core on 6000.5): https://docs.unity.com/en-us/engine/6000.5/manual/packages-list/packages-all/pack-core
- Test Framework 1.7 API (`UnityEngine.TestTools`): https://docs.unity3d.com/Packages/
[email protected]/api/UnityEngine.TestTools.html
- Test Framework 1.7 changelog: https://docs.unity3d.com/Packages/
[email protected]/changelog/CHANGELOG.html
- Performance testing API (3.5.0 released for 6000.5; 3.7.0 available): https://docs.unity.com/en-us/engine/6000.5/manual/packages-list/packages-all/pack-safe/com-unity-test-framework-performance
- Code Coverage 1.3.0: https://docs.unity.com/en-us/engine/6000.5/manual/packages-list/packages-all/pack-safe/com-unity-testtools-codecoverage