Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
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...
Qualities of a Unit Test
Readable
Maintainable Trustworthy
Thursday, 22 August 13
[Test]
void UpdatePath_OneActionChild_AddsSelAndChildToPath() {
var actionNode = FakeAction.CreateStub();
m_selNode.AddChi...
Test Driven
Development
Thursday, 22 August 13
SCIENCE!
•15-30% higher code “quality”
•10-20% longer development
time
http://biblio.gdinwiddie.com/biblio/StudiesOfTestDr...
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 AmmoVi...
Model View
Controller
class Gun : MonoBehaviour {
public GunModel Model { get; private set; }
void Awake() {
Model = new G...
Dependency
Injection
public void GunController(GunModel model, IWorldPhysics physics) {
Physics = physics;
Model = model;
...
Recap
Thursday, 22 August 13
•Unit tests test only one thing
•Readable, maintainable, trustable
•SCIENCE says TDD is e!ective
•Write test, see fail, ma...
Upcoming SlideShare
Loading in …5
×

Unit Testing and Test Driven Development in Unity3D

9,152 views

Published on

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

Published in: Technology, Business
  • Be the first to comment

Unit Testing and Test Driven Development in Unity3D

  1. 1. Unit Testing and Test-Driven Development In Unity3D Thursday, 22 August 13
  2. 2. Definitions Thursday, 22 August 13
  3. 3. Test “Automated code that invokes a system and checks behaviour.” Thursday, 22 August 13
  4. 4. Unit Test “Checks a single assumption about the behavior of one system.” Thursday, 22 August 13
  5. 5. Integration Test “Checks multiple systems are correctly connected” Thursday, 22 August 13
  6. 6. Unit Test != Integration Test Only fails when target assumption fails Fails when one of multiple implicit assumptions fail Thursday, 22 August 13
  7. 7. Qualities of a Unit Test Readable Maintainable Trustworthy Thursday, 22 August 13
  8. 8. [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
  9. 9. Test Driven Development Thursday, 22 August 13
  10. 10. SCIENCE! •15-30% higher code “quality” •10-20% longer development time http://biblio.gdinwiddie.com/biblio/StudiesOfTestDrivenDevelopment Thursday, 22 August 13
  11. 11. TDD 1: Write a unit test 2: Watch it fail 3: Make it pass simply 4: Refactor 5: Goto 1 Thursday, 22 August 13
  12. 12. Demo Thursday, 22 August 13
  13. 13. TDD and Unity3D Thursday, 22 August 13
  14. 14. NON-TRIVAL No post- compile hook MonoBehaviours are un-unit-testable Lots of alt- tabbing Thursday, 22 August 13
  15. 15. Model View Controller MonoBehaviour View Game code Controller Struct data Model Thursday, 22 August 13
  16. 16. class InitialGunSetupView : MonoBehaviour Model View Controller class Gun : MonoBehaviour class GunController class AmmoView : MonoBehaviour struct GunModel TDD Thursday, 22 August 13
  17. 17. 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
  18. 18. Dependency Injection public void GunController(GunModel model, IWorldPhysics physics) { Physics = physics; Model = model; } Thursday, 22 August 13
  19. 19. Recap Thursday, 22 August 13
  20. 20. •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

×