Let's Write Unit Tests in Unity!

This article is a translated version of my original post on Qiita. Original (Japanese): https://qiita.com/segur/items/3dbddb29de537e978a1e

Apology

This article is an adaptation of an internal study session slide deck for Qiita. Please excuse any layout issues.


To Unity Developers


Let's Write Unit Tests Together!!


Ever Had This Experience?

You fix one bug, and suddenly, another part of your code that was working perfectly fine has a new bug!


Fixing Bugs Creates Bugs Elsewhere

image.png

Arrows indicate class references.

Should I consider B and C before fixing A?


Reality Is Even More Complex...

Various entities are intricately referencing each other.

image.png

It's impossible to consider every single entity's needs!


Create Unit Tests to Monitor Unintended Changes

image.png

Create unit tests to ensure class operation reliability!


Let's Write Unit Tests!


How to Write Unit Tests in Unity?


Without asmdef, You Can't Introduce Unit Tests in Unity

image.png


With asmdef, You Can Introduce Unit Tests in Unity!

image.png

Let's introduce asmdef!


What is asmdef (AssemblyDefinition)?

In Unity, you can define groups of C# files as assemblies.

Assemblies in Unity are similar to:


Let's Introduce asmdef!


If the Reference Destination Isn't Supported, the Reference Source Can't Be Supported

image.png


If the Reference Destination Isn't Supported, the Reference Source Can't Be Supported

image.png


If the Reference Destination Isn't Supported, the Reference Source Can't Be Supported

image.png

Introduce asmdef starting from the endpoints!


Gradually Introduce asmdef from the Endpoints!


Reality Is Even More Complex...

image.png

Where are the endpoints?


Align the Direction of References!

image.png

Though it requires tedious work, align the direction of references!


How Do We Align Reference Directions?

Though it's challenging, it can be done using various methods!

Please google for more details.


Is It Really Worth the Effort?


It Is Worth It!


Without vs. With Unit Tests

image.png


It Significantly Reduces Programmers' Learning Costs!


Reduced Learning Costs Make Large Team Development Easier


Large Team Development Enhances Product Development Speed


This Is What We Call Loose Coupling!


The Tedious Work Is Only for Your Generation! (Don't Say You Got the Short End of the Stick)


Let's Write Unit Tests!