Test driven development

27
Test Driven Development An Introduction by Lukasz Kujawa

Transcript of Test driven development

Page 1: Test driven development

Test Driven Development

An Introductionby

Lukasz Kujawa

Page 2: Test driven development

About Me

- Lukasz Kujawa

- Lead Developer at Gloople

- http://systemsarchitect.net/

- @lukaszkujawa

Page 3: Test driven development

What is TDD?

"Test-driven development (TDD) is a software development process that relies on the repetition of a very short development cycle: first the developer writes an (initially failing) automated test case that defines a desired improvement or new function, then produces the minimum amount of code to pass that test, and finally refactors the new code to acceptable standards"- Wikipedia

Page 4: Test driven development

What is TDD in English?

Create test before code

Page 5: Test driven development

TDD Cycle

Page 6: Test driven development

RED GREEN

REFACTOR

Page 7: Test driven development

The Three Laws of TDD

- You might not write production code until you have written a failing unit test

- You might not write more of a unit test than is sufficient to fail, and not compiling is failing

- You may not write more production code then is sufficient to pass the currently failing test

Page 8: Test driven development

TDD Iterations

Test Production code

Write minimal Unit Test Create a Class

Call method of the class Create the Method

Assert output of the method

Add logic to return expected output

Page 9: Test driven development

Unit Testing

"In computer programming, unit testing is a method by which individual units of source code, sets of one or more computer program modules together with associated control data, usage procedures, and operating procedures, are tested to determine if they are fit for use."- Wikipedia

Page 10: Test driven development

Unit Testing in English

Testing smallest testable parts of application

Page 11: Test driven development

Unit Test is not a Test

- Very narrow and well defined scope- No complex dependencies- Test only one application layer- Environment independent- Fast

Page 12: Test driven development

TTD - Too Damn Difficult?

Page 13: Test driven development

Benefits for Developer

- Confidence

- Code quality

- Time

- Refactoring

- Reopening issues

- Self documenting code

Page 14: Test driven development

Benefits for Business

Page 15: Test driven development

Benefits for Business

- Shorten development cycles

- Delivering more

- Stable products

Page 16: Test driven development

Research 1

"Art of Unit Testing" by Roy Osherove

Stage Team without tests Team with tests

Implementation (coding)

7 days 14 days

Integration 7 days 2 days

Testing / Bug Fixing 12 days 8 days

Overall release time 26 days 24 days

Bugs in production 71 11

Page 17: Test driven development

Research 2

Quality improvement through test driven development: results and experiences of four industrial teams (2008) by Nachiappan Nagappan, E. Michael Maximilien, Thirumalesh Bhat and Laurie Williams

http://biblio.gdinwiddie.com/biblio/StudiesOfTestDrivenDevelopment

Page 18: Test driven development

Research 2Metric description

IBM Drivers Microsoft: Windows

Microsoft: MSN

Microsoft: VS

Defect density of comparable team in organizationbut not using TDD

W X Y Z

Defect density of team using TDD

0.61W 0.38X 0.24Y 0.09Z

Increase in time taken to code the feature becauseof TDD (%)

15-20% 25-35% 15% 20-25%

Page 19: Test driven development

How to test?<?php

MyFramework::bootstrap();

$obj = new Class_Under_Test();

if( $obj->something('Foo') != 47 ) {throw new Exception('Incorrect output for Foo');

}

if( $obj->someting('Bar') !== false ) {throw new Exception('Incorrect output for Bar');

}

Page 20: Test driven development

Unit Test Frameworks

- PHPUnit

- SimpleTest

Page 21: Test driven development

Using Framework

- API

- No maintenance

- Integration with IDE

- Code coverage report

- Mocks

Page 22: Test driven development
Page 23: Test driven development

<?php

class MyClassTest extends PHPUnit_Framework_TestCase { public static function setUpBeforeClass() {

MyFramework::initAutoloader(); }

protected function setUp() {$this->myClass = new MyClass();

}

public function testSomething() { $this->assertEquals(1, $this->myClass->something()); }

public function testSomethingElse() { $this->assertTrue( $this->myClass->somethingElse() ); $this->assertFalse $this->myClass->somethingElse( 47 ) ); }

}

Page 24: Test driven development

PHPUnit - Eclipse integration

Page 25: Test driven development

Unit Testing - small print

- Can't test directly private, protected and static

methods

- Tests only functionality

- Database

- Requires rigorous discipline

Page 26: Test driven development

SHOULD I TRY IT?

YESREFACTOR

Page 27: Test driven development

Q&A