Unity

Test Driven Development (TDD) in Unity — Part 1

Welcome to the first post in a video series I call Test-Driven Development in Unity. Over the course of this series we will be implementing the Life Gauge from Zelda: Breath of the Wild.

Series Navigation

Part 1: Introduction
Part 2: Getting Started
Part 3: Social Unit Tests
Part 4: Heart Container Implementation
Part 5: Test Data Builders
Part 6: Safe Refactoring
Part 7: Basic Player Health
Part 8: An Event Driven UI

Each video will consist of an unedited live coding session narrated by myself. My goal is to demonstrate what TDD looks like in Unity, and to teach you some of the tricks and tips I’ve picked up over time.

If the format sounds familiar its because I got the idea from Erik Dietrich’s popular Chess TDD series. I personally learned a lot from it, and I highly recommend checking it out.

The Life Gauge

The smallest unit of health in Zelda: Breath of the Wild is the heart piece. Above that is the heart, which is made up of 4 heart pieces. Finally, hearts are contained in the Life Gauge, which is displayed at the upper left corner of the UI.

Animated GIF  - Find & Share on GIPHY
The Life Gauge depletes when damage is taken and replenishes when health regenerating items are consumed. Besides some animations and effects, that’s all there is to it; the requirements for the Life Gauge are pretty straightforward.

Accomplishments

  • Experimented in Unity to find out that UI Image achieves the effect that we want
  • Created a failing test in order to tease out the Health class
  • Used Red-Green-Refactor to implement a solid first pass of the Replenish method
  • Removed instantiation duplication in our health tests
  • Came up with a task for our next session (throw exceptions for negative values)

Lessons & Takeaways

  • Game Development often facilitates visualizing the end result before writing any code
  • The tenets of Test-Driven Development dictate that we should only add production code when we have a failing test
  • Always understand why your tests are failing before you proceed to make them go green
  • Running unit tests outside of the Unity test runner is not currently possible
  • Refactor duplication when possible but be weary of tests that require complex setup

Links & Resources

Thanks for watching! Please feel free to like, comment, and subscribe.