unit testing and test driven development in unity3d

20
Unit Testing and Test-Driven Development In Unity3D Thursday, 22 August 13

Upload: andrew-fray

Post on 14-Jan-2015

2.771 views

Category:

Technology


7 download

DESCRIPTION

A short presentation I gave at the West Midlands Unity3D Users Group, about how to do effective TDD in Unity.

TRANSCRIPT

Page 1: Unit Testing and Test Driven Development in Unity3D

Unit Testing and

Test-Driven Development

In Unity3D

Thursday, 22 August 13

Page 2: Unit Testing and Test Driven Development in Unity3D

Definitions

Thursday, 22 August 13

Page 3: Unit Testing and Test Driven Development in Unity3D

Test“Automated code that

invokes a system and checks behaviour.”

Thursday, 22 August 13

Page 4: Unit Testing and Test Driven Development in Unity3D

Unit Test“Checks a single

assumption about the behavior of one system.”

Thursday, 22 August 13

Page 5: Unit Testing and Test Driven Development in Unity3D

Integration Test

“Checks multiple systems are correctly connected”

Thursday, 22 August 13

Page 6: Unit Testing and Test Driven Development in Unity3D

Unit Test != Integration Test

Only fails when target assumption fails

Fails when one of multiple implicit assumptions fail

Thursday, 22 August 13

Page 7: Unit Testing and Test Driven Development in Unity3D

Qualities of a Unit Test

Readable

Maintainable Trustworthy

Thursday, 22 August 13

Page 8: Unit Testing and Test Driven Development in Unity3D

[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

Page 9: Unit Testing and Test Driven Development in Unity3D

Test Driven Development

Thursday, 22 August 13

Page 10: Unit Testing and Test Driven Development in Unity3D

SCIENCE!•15-30% higher code “quality”•10-20% longer development time

http://biblio.gdinwiddie.com/biblio/StudiesOfTestDrivenDevelopmentThursday, 22 August 13

Page 11: Unit Testing and Test Driven Development in Unity3D

TDD1: Write a unit test2: Watch it fail3: Make it pass simply4: Refactor5: Goto 1

Thursday, 22 August 13

Page 12: Unit Testing and Test Driven Development in Unity3D

Demo

Thursday, 22 August 13

Page 13: Unit Testing and Test Driven Development in Unity3D

TDD and Unity3D

Thursday, 22 August 13

Page 14: Unit Testing and Test Driven Development in Unity3D

NON-TRIVALNo post-

compile hook

MonoBehaviours are un-unit-testable

Maintainable

Lots of alt-tabbing

Thursday, 22 August 13

Page 15: Unit Testing and Test Driven Development in Unity3D

Model View Controller

Maintainable

MonoBehaviour View

Game code Controller

Struct data Model

Thursday, 22 August 13

Page 16: Unit Testing and Test Driven Development in Unity3D

class InitialGunSetupView : MonoBehaviour

Model View Controller

Maintainable

class Gun : MonoBehaviour

class GunController

class AmmoView : MonoBehaviour

struct GunModelTDD

Thursday, 22 August 13

Page 17: Unit Testing and Test Driven Development in Unity3D

Model View Controller

Maintainable

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

Page 18: Unit Testing and Test Driven Development in Unity3D

Dependency Injection

public void GunController(GunModel model, IWorldPhysics physics) { Physics = physics; Model = model;}

Thursday, 22 August 13

Page 19: Unit Testing and Test Driven Development in Unity3D

Recap

Thursday, 22 August 13

Page 20: Unit Testing and Test Driven Development in Unity3D

•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, [email protected] http://andrewfray.wordpress.com

Thursday, 22 August 13