Unity and Unit Testing

Tulenber 13 March, 2020 ⸱ Beginner ⸱ 3 min ⸱

50 Shades of Green

The 21st century is in the yard, which means testing is one of the essential parts of development not only in game development but also in any software product.

In large companies, testing handled by QA (Quality assurance) department and is an essential part of the development process. Of course, in small teams, anyone can do this (typically a game designer or a programmer), in the end, games come out with bad quality, and testing happens on players =)

The technical side of quality control is also a pretty big topic. To help developers test games on the device, Unity has the Unity Remote tool. For testing at a more global level, there are Test Flight for Apple devices and closed channels on Google Play for Android. Also, there are many smaller features, like tools for logging https traffic to check specific network requests.

This article will be about creating unit tests, a small part of a vast topic that should make a programmer’s life easier.

Unit tests

Programmers have a bunch of tools and practices. An extraordinary surge of interest in testing noted in the early 2000s when the concepts of Extreme Programming and Test-driven development formed. Unit testing is one of the fundamental tools of these practices. Finding information on this topic on the Internet is quite easy, so we will not focus on the explanation of the basics. If you need more details about fundamentals, start with Wikipedia, especially pay attention to advantage and disadvantage paragraphs, and feel free to return here for practical examples for Unity.

Unity Test Framework is the primary tool to create unit tests in Unity. It based on NUnit, and since this is the most popular testing framework for C#, familiarizing yourself with its documentation will be useful.

For this case, we will add a unit test to Hints Library, which presented in our previous post:

  1. Open Test Runner Window > General > Test Runner
    Open Test Runner

  2. Switch to PlayMode and push "Create PlayMode Test Assembly Folder" for our tests
    Create Tests folder

  3. Use "Create Test Script in current folder" to create new script
    Create Test Script

  4. For code in need of testing, create Create > Assembly Definition
    Create Assembly Definition

  5. Add link to our Assembly Definition into Tests.asmdef file, wihch located in Tests folder
    Add Assembly Definition

  6. Don’t forget to push the "Apply" button at the bottom and safe changes

  7. In the script created in step 3, we write our tests

    public class TestSuit
        // With OneTimeSetUp annotation we instantiate singleton manager
        public void Setup()
            // Instantiate hint manager
            new GameObject().AddComponent<KhtHintsManager> ();
        public IEnumerator TestKhtHintsManager()
            // Test manager existence
            yield return null;

  8. Run tests
    Run tests

  9. Rejoice with the result
    Get results


Those steps are a small example to demonstrate the creation of a unit test in Unity (Full testing code can be fount at Hints Library). For the proper testing of functionality, a large amount of time and code should be sacrificed to the testing altar. Often the test code turns out to be even larger than the business part code, but this will help save even more time for catching errors related to regression. It’s clear that unit tests are just a small part of all the necessary testing, you still need to add integration tests, as well as test the UI, but this is beyond the scope of the article about unit tests. See you next time! =)

Privacy policyCookie policyTerms of service
Tulenber 2020