Linkedin Article Thumbnail 2 3 scaled

Mastering Unit Testing in Unity: Boost Your Code Reliability

Unit tests in Unity are automated tools designed to validate the behavior of individual components (units or methods) within your project’s codebase. They ensure that specific parts of your code function correctly under various conditions, allowing you to catch bugs early in development.

What Are Unit Tests?

Imagine a method called MoveOneUnitForward(), which moves a player forward by a defined distance. By writing a unit test for this method, you can confirm that it behaves correctly. Additionally, running this test during future updates ensures new changes don’t accidentally break existing functionality.

Types of Tests in Unity

Unity supports two main types of tests, each suited to different scenarios:

1. Edit Mode Tests

These are designed to run within the Unity Editor. They can access Editor-specific features and are ideal for testing isolated logic, such as utility methods or ScriptableObjects. However, they don’t work with scenes, GameObjects, or MonoBehaviours.

2. Play Mode Tests

These tests are used to evaluate your game’s runtime behavior. They can run in the Unity Editor or as standalone players. Marked with the [UnityTest] attribute, they can even execute asynchronous operations. Play Mode tests are suitable for scenarios requiring active scenes, such as validating GameObjectsMonoBehaviours, or gameplay mechanics.

Example Project Overview

This project, built using Unity 6, includes a class called Direction with variables for Front, Back, Left, and Right. An Edit Mode test will be created to verify direction checks in this class. It also has a Unit class that handles player movement through methods like MoveFront, MoveBack, MoveLeft, and MoveRight. Play Mode tests will be created to ensure these movement methods work correctly

Project link (Go to branch “Unit-testing”) : Link

How to Setup a Edit mode test:

  1. In the Unity “Project” window create a new folder called “Tests” to the project under the “Assets” folder.

AD 4nXenCHZfrU XSAVcKHcQzUDR MyogzqKDGliPjXYZprKuFTRxSCuRatUNdgsnKAtLq0h3dhnOGeQvON8Orl2tDWbzJVOXDJxMr8XpVrwkbkBM0grj9jnuLVuOCdVTW3knn4fJalz?key=

2.Now go to “Window > General > Test Runner”. It will open the “Test Runner” window. You can place it somewhere you like.

AD 4nXfu6mX4dCgpoZ0ySCeqdWs8KFb0f8EVKmbI5kxoofODDtQLQ0Y5v mNMhEbD7HibfDoizQtggamU7ZfySlc3S7SCgxFoVuFVKdaGdpEE0Dw6NecE3a1P1dEqTfezEUehrx8p8VZdw?key=

3. Select on the “Tests” folder previously you created. And then click on the “Edit Mode” button in the Test Runner window. Then click on the “Create a new Test Assembly Folder in the active path.” Rename it to “Edit mode” and name it “Edit Mode” for clarity. This will create a new folder in that name and an “Assembly Definition” in that folder.

AD 4nXczEjuCddezwq6JeVRQE7XXHZjqn511acq9hwcGfycE4N0COZnl5HROQ8aKiECxYDjzC NmPX2 lQmeHGW8LUoLC1aY26wTPKJfrJdeSh 7IokZ daR3g245mDqcBQVCEn8gI0inA?key=

4. Now go into the newly created “Edit Mode” folder from the “Project” window. Click on the “Create a new Test Script in the active path.” in the Test Runner window. (in the example renamed  as “DirectionTest”). 

AD 4nXdtytfLh94CHcAO9Y5TS IYvd BaRXGGmdKg4GhhfDf5oDat7 LBQpwUWnDQfTJqw55EP 8FN9kZxPS1Dct6oTv1 5KjQj97 SW8P5Fdj54KzBY7au9WjOmEqHHpZ8NC318h MBg?key=

5. Add  an assembly to the “Scripts” folder (Assuming that all of the scripts are in a folder called ”Scripts” under the “Assets” folder.). Note: We need all of the project Scripts associated with an assembly so the test can see them.

AD 4nXcRmJaHD9fVbOiLHXckEzqFg41Ovrd9jj1xxWccGJSNFEc3Hxl2a9PDn6FYuRHgqLnvWNm Hehv m6XY0RdbbQApyBI368ufjyFJzEfalyUekI3bstpw NM7PQWIZrvFpH cXk0A?key=

6. Now Add that assembly Script(s) as a reference to the edit mode assembly. By selecting the “Edit Mode” assembly definition inside the folder “ Edit Mode” and clicking on the “+” button under the section “Assembly Definition References”  in the inspector. And then “Apply” on the bottom of the inspector.

AD 4nXdNJc3l94XJ4RKBx fwA6qY9EcyFbJGIFU 0tyRZREv2D5EI8fatGeGBORP7o6FLTzb2Onj zyK0oHWH3oIEsq0FOl a82PejYj9kB7i5cNh oKi2dPLwdmeaUKkayVFtwViWfocQ?key=

7. now open the Test file (DirectionTest) you can remove the existing unnecessary code like in the code below. (leave the first function with the [Test]  tag)

 

image

8. Rename the method (DirectionTestSimplePasses) for the desired name and add the Test code. An example code is given below.

 

image 1

 

image 2

9. Now go back to unity and select “Run All” in the Test Runner window. Then the Edi mode tests will run.

AD 4nXcUD3Td1OyTwMDTi BtO1LAfUphdQThGAdaD1FM85K4iGH9uU3 kCeftZTEaFfS8WRnTjglBo GfVBeSZHn2Ek4fIGYXosxzfviGxONsMrovH3w drMH60MlhfR pGTbenL8BlUw?key=

How to Setup a Play Mode test:

  1. Click on the Tests folder (again) and switch to the “Play Mode” then click on “Create a new Test Assembly Folder in the active path.” Rename the folder to “Play Mode”

AD 4nXeXKMnVolhKGbfa0JGiFJl4pYtNaZq6V0E8F6EonKBjGRFgLRZJCVCFfEX75RXIzxbKArY7kJX4Ue0Gx4C Pa4hAgvjyZ6 x6QGSyAAUNzaGDRy9PQTrvS5BqXGp2g0JkUDWhOMCg?key=

2. Click on “Create a new Test Assembly Folder in the active path.” and create a test script.

AD 4nXdA2XH8Z4eAjla51YOf mraqoDJ6JaG60GpFeNemlH4VlvJKn4yzjHFh8HC 9LJThQieOO2OMkILwTh1lEqNfNqREK6ALibeotZBPd1xHRiLCbXWQInNmANLHFcD322q3ZTrI6t8w?key=

3. Add a reference for the “Scripts” to the Play mode assembly. Anc click on “Apply”

AD 4nXchFKryDRo1Ckpi3UfUfYAzVITiMpKDmq 1rNHc n6DInPBcW8hwc5hLFh0ra1I4 vjwhbboc0apnJw Vj4F9nBR yYjCjjA4ETL31cU5Zq4YQ1pQBO0tVon8I lcsbTiio L1cxA?key=

4. Clean the code

 

image 3

5. Add the Test code. Example code is given below.

 

image 4

 

image 5

6. Then Run.

AD 4nXcC2pycrG1RP37cohL BN2V7shVBD34o4ZJKew3Ct ZOzQrxVJOVQqvvlTEKcYZkznnHx0HAxSjIID JdHH3kT1jKUm5 WqRWFPW4MrsIn4HvvmlFJy3UyPO1fMfSz0QSJyjqPdKQ?key=

 

For More Details of Envision Studio Update Post click here.

Read Next