Testing - UO-DFM/SimCityOttawa-Documentation GitHub Wiki

Overview

To unit test the Sim City Ottawa application, we are using the Unity Test Framework. All unit tests and related code / files can be found under the Assets/Tests folder.

Framework Setup

The framework was setup using the following Video Tutorial.

Running Tests Locally

There are two types of tests, EditMode and PlayMode. Both types of tests follow the same execution steps.

  1. Open the following in the Unity navigation tabs Window > General > Test Runner
  2. Select EditMode or PlayMode in the test runner window
  3. (optional) Click Clear Results
  4. Click Run All to execute all tests

Note: To run a selection of tests, select all tests you want to run and click Run Selected

EditMode Vs PlayMode Tests

The difference between the tests is that PlayMode tests are executed in Unity's runtime environment. Editor tests are executed in the editor's environment. This means that for PlayMode tests, callback functions such as Start() and update() from MonoBehavior are executed but are not executed in EditMode tests. EditMode tests are also faster to run than PlayMode tests.

Reference

Writing Test Cases

EditMode tests can be found under the Assets/Tests/EditMode directory and PlayMode tests can be found under the Assets/Tests/PlayMode directory.

Important: Tests should be written as EditMode tests unless callbacks are required then the test should be a PlayMode test. See EditMode Vs PlayMode Tests for more details.

To write tests, create a new file under the proper test folder you want the test to run under Ex: Assets/Tests/EditMode/GameColorTests.cs. Ensure the following are imported within the file.

using System.Collections;
using System.Collections.Generic;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.TestTools;

From there you can write your tests, an example of a test file can be seen below

using System.Collections;
using System.Collections.Generic;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.TestTools;

public class GameColorTests
{
    
    [Test]
    public void getColorHexBodyColorTest()
    {
        string hex = GameColors.getColorHexFromList("bodyColor", 0);
        Assert.AreEqual("#301E10", hex);
    }

    [Test]
    public void getColorHexColorInvalidKeyTest() {
        string hex = GameColors.getColorHexFromList("HelloWorld!", 0);
        Assert.AreEqual("#FF00DD", hex);
    }

    [Test]
    public void getColorHexColorNegativeIndexTest() {
        string hex = GameColors.getColorHexFromList("bodyColor", -10);
        Assert.AreEqual("#FF00DD", hex);
    }

    [Test]
    public void getColorHexColorOutOfBoundsIndexTest() {
        string hex = GameColors.getColorHexFromList("bodyColor", 100);
        Assert.AreEqual("#FF00DD", hex);
    }
}