Creating a Play Mode Test with Unity’s Test Framework in Unity 2022

GameDev Dustin
5 min readAug 30, 2023

--

In this article, we’ll use Unity’s Test Framework to run Play Mode tests.

Play Mode tests will be used for testing our gameplay related code.

Per Unity’s documentation:

“You can run Play Mode tests as a standalone in a Player or inside the Editor.

Play Mode tests allow you to exercise your game code, as the tests run as coroutines if marked with the UnityTest attribute.”

If you’re just diving into the Unity Test Framework for the first time, I recommend reading the QuickStart Guide first:

To get started, switch from “EditMode” to “PlayMode” in the Test Runner window.

Click the “Create PlayMode Test Assembly Folder” button and then the “Create Test Script in current folder” button.

Name it something appropriate to your use-case.

Referencing the Game Assembly (asmdef) File
Let’s go ahead and add the “Game” asmdef (Assembly Definition file) we created in the QuickStart Guide article to the newly created UTF-PlayTests asmdef file.

We did this for the UTF-EditorTests asmdef file in the previous article as well.

This is a prerequisite for unit testing of classes that are not part of the Editor or Play mode asmdef folder structures.

With this reference we’ll be able to test gameplay classes associated with the Game asmdef file.

We’ll cover that use-case in the next article.

Make sure to click the Apply button in the Inspector window after adding the asmdef file reference.

The PlayTest01 Class
The PlayTest01 class we generated is practically identical to the UTF-EditorTest01 class we created in the previous article.

That’s because the only difference is which Assembly File (Editor or Play mode) the class belongs to and naming of course.

The Test Framework than does all of the work in the background based on the asmdef file to run the test at the correct time based on target platform and referenced asmdef files.

Warning: Bloated Build Size Ahead!
Per Unity’s documentation, we need to be wary of project bloat when using Play Mode unit testing.

However, in Unity 2022.3.8f1, I did not see the “Disable PlayMode tests for all assemblies” option shown above.

Since Unity automatically adds a Define Constraint of “UNITY_INCLUDE_TESTS” on the asmdef files for unit testing, they are probably already excluded from project builds.

According to Unity’s documentation:
“You can use preprocessor symbols to control whether an assembly is compiled and included in builds of your game or application (including play mode in the Editor).

You can specify which symbols must be defined for an assembly to be used with the Define Constraints list in the Assembly Definition options.”

According to Unity’s documentation:

“C# directives allow you to selectively include or exclude code from compilation, based on whether certain scripting symbols are defined or not defined.”

If we navigate to Project Settings > Player > Other Settings > Script Compilation, we can see that there are no Scripting Define Symbols listed.

For the unit testing scripts to be compiled in a build, we’d probably need to add “UNITY_INCLUDE_TESTS” to the list.

To explicitly exclude unit testing scripts, we might add “!UNITY_INCLUDE_TESTS” to the Script Compilation > Scripting Define Symbols.

Please leave a comment if you have more insight into this topic!

Back to the Unit Test Code
Using the same unit test code from our EditMode unit testing, we’ll update the PlayTest01SimplePasses() method and see what happens.

Since the assertion is that the sum will equal 15, but the vars are 5 and 3 (5 + 3), we can see that the test fails as expected.

Let’s rewrite the test code with the expectation that the unit test will pass by setting the y value to 10 (5 + 10 = 15).

Running the unit test now shows as successful.

Finally, an Actual Play Mode Unit Test
Let’s add some simple logic to create a gameobject and then move it.

We’ll then assert that the gameobject’s position is no longer where it was originally instantiated.

Remember to end with “yield return null” or the test won’t stop running play mode.

When we run the unit test, we can see the gameobject being created, moving, and then the test showing as successful.

Perfect!

In the next article we’ll cover running unit tests on code that exists elsewhere in our project!

--

--