Unit Testing
and
Test-Driven
Development
In Unity3D
Thursday, 22 August 13
Definitions
Thursday, 22 August 13
Test
“Automated code that
invokes a system and checks
behaviour.”
Thursday, 22 August 13
Unit Test
“Checks a single
assumption about the
behavior of one system.”
Thursday, 22 August 13
Integration
Test
“Checks multiple systems
are correctly connected”
Thursday, 22 August 13
Unit Test != Integration Test
Only fails when target
assumption fails
Fails when one of multiple
implicit assumptions fail
Thursday, 22 August 13
Qualities of a Unit Test
Readable
Maintainable Trustworthy
Thursday, 22 August 13
[Test]
void UpdatePath_OneActionChild_AddsSelAndChildToPath() {
var actionNode = FakeAction.CreateStub();
m_selNode.AddChild(actionNode);
m_selNode.UpdatePath(m_path, NodeResult.Fails, null);
var desiredPath = new ITreeNode[] {
m_selNode, actionNode };
Assert.IsEqualSequence(m_path, desiredPath);
}
Thursday, 22 August 13
Test Driven
Development
Thursday, 22 August 13
SCIENCE!
•15-30% higher code “quality”
•10-20% longer development
time
http://biblio.gdinwiddie.com/biblio/StudiesOfTestDrivenDevelopment
Thursday, 22 August 13
TDD
1: Write a unit test
2: Watch it fail
3: Make it pass simply
4: Refactor
5: Goto 1
Thursday, 22 August 13
Demo
Thursday, 22 August 13
TDD and Unity3D
Thursday, 22 August 13
NON-TRIVAL
No post-
compile hook
MonoBehaviours are
un-unit-testable
Lots of alt-
tabbing
Thursday, 22 August 13
Model View
Controller
MonoBehaviour View
Game code Controller
Struct data Model
Thursday, 22 August 13
class InitialGunSetupView : MonoBehaviour
Model View
Controller
class Gun : MonoBehaviour
class GunController
class AmmoView : MonoBehaviour
struct GunModel
TDD
Thursday, 22 August 13
Model View
Controller
class Gun : MonoBehaviour {
public GunModel Model { get; private set; }
void Awake() {
Model = new GunModel();
m_controller = new GunController(Model);
class AmmoView : MonoBehaviour {
void Awake() {
m_model = GetComponent<Gun>().GunModel;
Thursday, 22 August 13
Dependency
Injection
public void GunController(GunModel model, IWorldPhysics physics) {
Physics = physics;
Model = model;
}
Thursday, 22 August 13
Recap
Thursday, 22 August 13
•Unit tests test only one thing
•Readable, maintainable, trustable
•SCIENCE says TDD is e!ective
•Write test, see fail, make pass, refactor
•Model-View-Controller and Dependency Injection
make TDD possible in Unity
•Kent Beck, “Test Driven Development”
•Roy Osherove, “Art of Unit Testing”
•UnTest http://www.github.com/tenpn/untest
•Moq https://www.nuget.org/packages/moq
•StrangeIOC http://thirdmotion.github.io/
strangeioc/
Andrew Fray, @tenpn, andrew.fray@gmail.com
http://andrewfray.wordpress.com
Thursday, 22 August 13

Unit Testing and Test Driven Development in Unity3D