Unit testing a simple health script class
Let’s create something that might be used in a game and that can easily be unit tested. Classes that do not subclass from MonoBehaviour
are much easier to unit test since instance objects can be created using the new
keyword. If the class is carefully designed with private data and public methods with clearly declared dependencies as parameters, it becomes easy to write a set of tests to make us confident that objects of this class will behave as expected in terms of default values, as well as valid and invalid data.
In this recipe, we will create a health script class and a set of tests for this class. This kind of class can be reused for both the health of human players and Artificial Intelligence (AI)-controlled enemies in a game:
Figure 19.12: Passing tests for our health script class
How to do it...
To unit test a health script class, follow these steps:
- Create a new 3D Unity project.
- Create...