6 steps to write Test Scripts and run them in Unity for your games and XR applications.
Introduction
You know the concept of Unit Testing but you don’t know how to write a test and run it in Unity? You are in the right place because I will concentrate on the steps of writing a test case and running it in Unity rather than the concept of Unit Testing itself because there are already enough articles and content present online that will tell you about Unit Testing.
Unit Testing in Unity can be done in 2 modes—
- Edit Mode Unit Testing
- Play Mode Unit Testing
I say modes because the concept of Unit Testing will always remain the same across any IDE or any framework i.e “test a unit of the code you write”. Considering the fact that XR Development is taking off and Unity is a great platform to build XR applications, I feel knowing how to write Unit Tests in Unity can be a good contribution towards developing production ready XR applications. It is a good first step into writing production level code(let’s leave the code smells, refactoring, design patterns, cognitive complexity, etc aside😅).
If you don’t understand the term “Unit Testing” then I would recommend you to learn about it. Just understand the concept. Implementation can be done anywhere in any language. This link should be useful but there is a lot more to it and I will share better resources at the end of this article. For now, read from that link and return to this article.
Steps
So without further ado, let’s understand the implementation in Unity. This article is not for those who are learning the basics of Unity and game development. It’s for those who are interested in writing highly maintainable and large code base and want to understand how to run tests in Unity.
Step 1
Open a new Unity 3D project(or continue with any existing project you are working on. You can always delete the scripts that we write in this article if you wish to).
Step 2
Step 3
Now go to the Projects panel. If you are using Default Layout(you can enable this by going to right hand top corner, clicking on Layout and then Default. This organizes the panels in the “default Unity way”) then this panel will be right below the whole scene panel and the Hierarchy Panel.
Step 4
Open Editor by double clicking on it. Right click anywhere inside the “Assets>Editor” panel.
Go to Create > Testing > C# Test Script. Name it whatever you want. If you want to follow the convention then go ahead name it as “_____Test” (I named it GeneralTest).
Step 5
Open GeneralTest in VisualStudio by double clicking on it and now you can modify the script by writing your own Tests. Once you are done writing the Tests go to step 6.
Trending AR VR Articles:
2. Infographic: The Future of Virtual Reality
I will just write one teeny tiny Test to test that my player’s (imagine that I have a Player class) health never falls below zero, for the purpose of this article.
This is how my “GeneralTest.cs” looks like👇🏼
using System.Collections;
using System.Collections.Generic;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.TestTools;namespace Tests
{
public class GeneralTest
{
[Test]
public void PlayerHealthShouldNotFallBelowZero()
{
var player = new Player();
var playerHealth = player.health;
Assert.GreaterOrEqual(playerHealth,0); }
}
}
Step 6
Done writing your test cases? Great. Let’s move to running your tests now. Keep Visual Studio open and hop back to Unity. Go to the “Test Runner” panel(the one you docked in Step 2). Select “Edit mode” inside the “Test Runner” panel. Expand “Assembly-C-Sharp-Editor.dll” by clicking on the small drop down arrow on the left of it. Expand “Tests”.
You will be able to see —
- Your Test Scripts(in my case only GeneralTest because that’s the only script I have written) and
- The Tests that you have written in those Test Scripts(in my case it is “PlayerHealthShouldNotFallBelowZero” ).
Alright, now you can click on the “Run all” button to test all the Tests and all the Test Scripts in one go or you may select one test and click on the “Run Selected” button to run only one specific test. That’s all. You can see which test cases passed and failed depending on the Tests you write.
Conclusion
Congratulations. You now know how to write Test Scripts in Unity and run them. You can create as many test scripts as you want following the above 6 steps.
There is obviously much more about Unit Testing that I have not covered in this article because all I wanted is to show you is how to implement a Test in Unity in Editor Mode. I have not covered the anatomy of Unit Tests i.e Arrange, Act and Asset which can be covered in another article to keep this article short and simple. Also, there is a difference between testing Unity MonoBehaviours and general C# classes which will be covered in a different article.
Resource to understand Unit Testing in Unity with an Example (this is good for starters)