Unit Testing Using Unity Test Framework in Unity

Prasetio Nugroho
4 min readJan 1, 2021

Like the title of this article, i want to share about how to use Unity Test Framework in Unity for Unit Testing in your code before production.

In software development cycle, we usually know TDD (Test-Driven Development). In TDD, you write a test code for test your actual code before you release into production code. It is important to always test your code before production. Because it will reduce bugs as much as possible you do the test. But it is recommended to do automate test rather than manual test.

In unity we can do unit testing, using Unity Test Framework.

The Unity Test Framework (UTF) enables Unity users to test their code in both Edit Mode and Play Mode, and also on target platforms such as Standalone, Android, iOS, etc. — Unity Test Framework Docs

There is two test mode, edit mode or play mode. For the detail you can see in the reference that I attached in the end of this article.

Edit Mode tests (also known as Editor tests) are only run in the Unity Editor and have access to the Editor code in addition to the game code. 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. — Unity Test Framework Docs

Preparing the test in unity

First, make sure to install Test Framework in package manager.

Test framework in pacakge manager

Open Test Runner in Window > General > Test Runner. Then click the create button in Edit Mode, in this case i will test only in edit mode. It will create Test folder and .asmdef file by default.

Test runner window
Test folder and .asmdef file by default

Preparation is complete!!

How do the test in Unity?

In this case, i would like to test color input. In EditMode tab, click button create test script and I name it ColorInputTest.

ColorInputTest file

I want to test rgba value input from texture coordinate to check the color. After I got the rgba, I round the rgba value and categorize it into white or black color. Here is the code

In above the method always use Test attribute to identified the test method. And I want to add some parameter into the method using TestCase attribute. Back to test runner tab, to check if the test is ready.

Test runner tab after edit ColorInputTest file

Then, click RunAll. The test should be like this.

Test result

The test case with parameter 0,0,0,1 in ColorInputTestBlack method test is passing the test, the other are not passing the test. The test case with parameter 1,1,1,1 in ColorInputTestWhite method test is passing the test, the other are not passing the test. It means the code is ready.

Conclusion

Testing makes your game or app better. Always test your code before release into production. I can say, if you do the test as much as possible, your code minim of bugs, and better. I add some reference for you, if you want to know more about testing in unity. You can clone this sample from this Repository.

Reference

--

--

Prasetio Nugroho

Interested in new tech, gaming, programming and another knowledge or possibilites that can make me better. Cheers!!