Unity: testing¶
Automated tests for Unity projects and the architecture that makes them possible.
Entries are sorted by score from 1 to 10 (criteria): 9โ10 start here or strongly recommended, 6โ8 recommended, 1โ5 further reading or conditional.
Adopted references¶
- 8 Unity Test Framework (Unity Manual): the baseline. It covers Edit Mode and Play Mode tests built on NUnit.
- 8 Unity Test Framework course (Unity Manual): a complete hands-on tutorial.
- 6 How to run automated tests for your games with the Unity Test Framework (Unity).
Key rules¶
- 8
UNITY-TEST-1Inject dependencies into the plain C# classes that hold the logic, so that Edit Mode tests can substitute fakes. Keeping that logic out of MonoBehaviours isUNITY-CODE-1in code architecture. (Unity, How to architect code as your project scales, Humble Object pattern) - 7
UNITY-TEST-2Run the tests from the command line in batch mode as part of the build, so CI runs them on every change. (Unity Test Framework, Run tests from the command line, Unity, How to architect code as your project scales) - 7
UNITY-TEST-3Write plain NUnit[Test]tests in Edit Mode for logic that doesn't need Play mode, and use[UnityTest]Play Mode tests for runtime behaviour that spans frames or needs a scene. (Unity Test Framework, Edit Mode vs. Play Mode tests) - 7
UNITY-TEST-4Put tests in their own test assemblies that reference the assemblies under test. Test assemblies can't referenceAssembly-CSharp, so code under test must live in a custom assembly. (Unity Test Framework, Edit Mode vs. Play Mode tests, Unity Manual, Assembly definitions)
How to enforce¶
Tools that check the key rules above automatically, in CI, the IDE or a commit hook. "Partly" means the tool covers only some of what the rule asks.
- 5 GameCI Unity Test Runner: a GitHub Action that runs a Unity project's Edit Mode and Play Mode tests in CI. Checks
UNITY-TEST-2.