unit testing and test driven development in unity3d
DESCRIPTION
A short presentation I gave at the West Midlands Unity3D Users Group, about how to do effective TDD in Unity.TRANSCRIPT
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/StudiesOfTestDrivenDevelopmentThursday, 22 August 13
TDD1: Write a unit test2: Watch it fail3: Make it pass simply4: Refactor5: Goto 1
Thursday, 22 August 13
Demo
Thursday, 22 August 13
TDD and Unity3D
Thursday, 22 August 13
NON-TRIVALNo post-
compile hook
MonoBehaviours are un-unit-testable
Maintainable
Lots of alt-tabbing
Thursday, 22 August 13
Model View Controller
Maintainable
MonoBehaviour View
Game code Controller
Struct data Model
Thursday, 22 August 13
class InitialGunSetupView : MonoBehaviour
Model View Controller
Maintainable
class Gun : MonoBehaviour
class GunController
class AmmoView : MonoBehaviour
struct GunModelTDD
Thursday, 22 August 13
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
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, [email protected] http://andrewfray.wordpress.com
Thursday, 22 August 13