If you’re an experienced software programmer with experience in unit testing or you are developing a logic heavy game or reusable game engine, make sure you unit test.
If you’re new to programming and your game does not have much logic or is still in the rapid prototype phase, don’t unit test.
After a long hiatus of nearly 8 years, I’ve finally come back to game development. During that time, I was focused on developing web applications with Ruby on Rails.
One of the side effects that I’ve noticed when programming in Unity is that I crave Test Driven Development. During my stint as a Ruby on Rails developer, I got used to writing the tests first and then writing the code that passed those tests. There’s something about TDD that scratches a personal itch of mine. I did some research and concluded that TDD is not practical for Indie Game Development. However unit testing is a a very viable option in Unity.
Unit testing might be right for you if:
- You’re an experienced software developer: If you’ve worked with unit testing before and know how to write maintainable unit tests, unit testing is right for you. Keep in mind that decoupling the logic of your game is especially important in Unity if you want to write maintainable unit tests. You have to know how to use mocking and dependency injection to decouple your unit tests from other systems in order to write true unit tests.
- You’re developing a logic or AI heavy game: If your game has a lot of logic or a complicated AI, you can save a lot of time and effort by using unit tests. Unit tests make is simple for you to setup different situations for your AI and then test out how your game logic or AI reacts to each situation. You can fine tune your AI without ever having to create a complete level in your level editor and then running the game until the point where you can view how the AI acts on screen.
- You’re developing a set of games in the series: If you’re breaking your game up into a trilogy or chapters, unit testing will definitely help you create a maintainable engine for your series.
Unit testing might NOT be right for you if:
- Unity is your first programming rodeo: If you’re new to the world of programming, Unity is an especially hard system to learn unit testing on. The Unity MonoBehaviours are untestable and you’ll see an error “Security Exception : ECall methods must be packaged into a system module” if you try to instantiate any Unity MonoBehaviours in your testing code.
You’re rapidly developing prototypes: If you’re building a prototype (you really should, you know), there’s little reason to create unit tests. Look, unit tests are great, but they require time to write and maintain. You need to trust your unit tests and if you’re constantly rewriting all your unit tests because you’re radically redesigning your game, you’re going to burn out.
- Your game depends on a graphic, audio or user interface hook: Unit testing elements like graphics or audio is wasted effort for most indie developers. Unit testing is better suited to testing logic and flow.