Assemblies for tests

The Unity Test Framework runs tests from test assemblies: assembly definitions that reference the test framework. A test assembly can only reference code that is in an assembly definition too, so your scripts can't stay in Assembly-CSharp, the assembly Unity compiles scripts into by default. This is a requirement of the Test Framework, with or without Understudy.

You need two assemblies:

  • One for the code you test, such as Game.
  • One for the tests, which references Game and Understudy.

Create an assembly for the code under test

To put your scripts in an assembly, perform the following steps:

  1. In the Project window, select the folder that holds your scripts.
  2. Go to Assets > Create > Scripting > Assembly Definition, and name it, for example Game.

Every script in the folder and its subfolders now compiles into Game. If the Console window shows errors about missing types, your scripts use code from a package or from another assembly definition, such as Unity.TextMeshPro or Unity.InputSystem. Select Game, add them to Assembly Definition References in the Inspector, then select Apply.

Inside an assembly definition's folder, a folder named Editor is no longer special. Give it an assembly definition of its own, with Platforms set to Editor only and a reference to Game. Scripts that stay outside every assembly definition, such as plugins in Assets/Plugins, compile into Assembly-CSharp or Assembly-CSharp-firstpass, which Game can't reference. When your scripts use them, give them an assembly definition too.

Choose Edit mode or Play mode

Edit mode tests run without entering Play mode, and are the fastest. Unity calls no Awake, Start or Update in them, so a test calls the methods it needs. Play mode tests run frames, for code that needs the engine to run: coroutines that wait, physics, scene loading. Spies, stubs and fakes work the same in both.

Create a test assembly

To create a test assembly that uses Understudy, perform the following steps:

  1. Go to Window > General > Test Runner.
  2. Select EditMode.
  3. In the Project window, select the folder where the tests go, such as Assets.
  4. In the Test Runner window, select Create a new Test Assembly Folder in the active path. Unity creates a Tests folder with a Tests assembly definition.
  5. Select the Tests assembly definition. In the Inspector, under Assembly Definition References, select + twice, set the two new entries to Game and Understudy, then select Apply.
The Inspector of the Tests assembly definition, with Game and Understudy in Assembly Definition References

The Test Runner window shows the button only while its tab has no tests and the selected folder holds no test assembly. Otherwise, use Assets > Create > Testing > Tests Assembly Folder, which creates a test assembly for every platform: its tests run in Play mode, under PlayMode in the Test Runner window. To run them in Edit mode instead, set its Platforms to Editor only.

Note: Only test assemblies reference Understudy. The Test Runner's test assembly definitions carry the UNITY_INCLUDE_TESTS define constraint. An assembly of your own that references Understudy, such as one holding shared doubles, needs it too, under Define Constraints. Without it, the Console shows an error naming the assembly definition.

Reach members that aren't public

A test names a member in C#, so it can only name members it can see. A private member, such as a MonoBehaviour's void Update(), is out of a test's reach.

To let a test call or select such a member, make it internal, and let the test assembly see the internal members of the code under test with InternalsVisibleTo, in any script of the Game assembly:

using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("Tests")]

Replace Tests with the name of your test assembly. Unity still calls an internal void Update() every frame, as it calls a private one.