It's so satisfying when all the lights turn green!
Unreal comes with a great automated testing tools that can be run in the editor, in automated builds. We should use them more, let's learn how!
Initial Setup
Before we get into writing tests, we first have to enable testing on our project.
Open up the Plugins window under Edit > Plugins. Then enable the Functional Testing Editor plugin under the Testing category. Restart the editor, and under the Window menu you should now see the Test Automation item.
From the Session Frontend window, under the Automation tab we can see all of the tests that are defined for the engine and for our game.
Simply check the box next to the ones you want to run, and hit run!
Now we'll look at how to define our own simple test.
Simplest Test Example
For this example, we will be using a simple Hero class, something that might have health, do damage, take damage, get poisoned etc.
Let's TDD!
Test driven development is defined to death elsewhere. For our purposes it means we'll write tests first, then fix our Hero class to pass the tests.
This is the functionality we want to support:
Take damage from enemy weapons.
Get poisoned and take damage over time.
Die when it's health reaches zero.
Increase health when healed
Respawn with full health
Of course with this simple spec there are some other edge cases that we need to check for:
After respawning, it should no longer be poisoned
Increasing its health shouldn't put it over its max health
What happens when healing the player with a negative amount
Damaging the player with a positive/negative amount (depending on interface)
In order to use AutomationEditorCommon.h make sure to add UnrealEd to Project.Build.cs:
Running our tests, we fail! Oh no!
We can now update our Hurt function to actually subtract the damage from the player.
Running our tests again, now we pass! Yay!
We can continue this adding more tests, implementing more functionality. This is an exercise left for you, the reader! Good luck!
Further Reading
Tests and Testability by Jessica Baker is a fantastic article on the benefits of tests in gamedev, and how to design your classes to make them less tightly coupled and more easy to test.