August 25, 2019
Unit Testing
How to Use It
Youtube – Unit Testing in Unity – Livestream
By: Unity3d College
Unit testing is used to check that small chunks of your code can pass designed tests. These tests can be made in a way to show why a certain method or snippet of code is designed the way it is, and help inform other programmers on the project of why that is and also if they break some intended uses of the designed code. They just generally ensure that your code is doing what you expect it to, and remains doing what you expect.
The tests generally follow a “triple A” format: Arrange, Act, Assert.
- Arrange: Set up the conditions for the test
- Act: Perform actions that you want to test
- Assert: This is the check used to test
In this lecture, they have setup a very nice system for holding all of their testing needs in Unity. Their is a specific Testing namespace with all the testing classes that hold public test methods. These are accessible through a specific window in Unity and can be run in the Editor, as opposed to constantly needing to run the tests in the play mode of Unity. This uses a setup with assembly definitions and other editor interactions I am unfamiliar with, so I will need to look into that more to setup a system like this.
The test system they have setup does not work particularly well with MonoBehaviours. This is because a lot of tests involve creating new objects to arrange the test, and Unity does not allow MonoBehaviours to be created with the new keyword. You must create a separate gameobject and attached the MonoBehaviours with an addComponent type approach.
This can be worked around with something called NSubstitute. You can grab this online, it is a .dll file that you place in your Pluging > Editor folder to help use with all your testing. It allows for use of the NSubstitute namespace to use this Substitue.For method which somehow works around the MonoBehaviours issues for testing purposes. This can also be used to test interfaces, which also cannot just be made as new objects.
Later they do some play mode tests, which test that adding force to a rigidbody is moving the body properly. This type of testing could actually lead to something very useful with my thesis research as I may want to be monitoring the movement of physical bodies to see if they are behaving properly.
Others to Checkout for Unity Programming Content
There were a few final notes on some people to find on Youtube for more on programming in general and with Unity:
- UncleBob Bob Martin (Design Patterns, C# focused, not Unity specific)
- Mob Mentality Show – Good for groups of programmers