47
Unit-Tests Schnell und einfach selbst gemacht TobiasSchlitt <[email protected]> PHP World Kongress 2009 2009-11-24 Tobias Schlitt (PHP World Kongress 2009) Unit-Tests 2009-11-24 1 / 25

Unit-Tests

Embed Size (px)

DESCRIPTION

Slides from my unit-testing talk at the PHP World Kongress

Citation preview

Page 1: Unit-Tests

Unit-TestsSchnell und einfach selbst gemacht

TobiasSchlitt <[email protected]>

PHP World Kongress 2009

2009-11-24

Tobias Schlitt (PHP World Kongress 2009) Unit-Tests 2009-11-24 1 / 25

Page 2: Unit-Tests

About me

Tobias Schlitt <[email protected]>

PHP since 2001

Freelancing consultant

Qualified IT Specialist

Studying CS at TU Dortmund(finishing mid 2010)

OSS addicted

eZ ComponentsPHPUnitVarious other projects . . .

Tobias Schlitt (PHP World Kongress 2009) Unit-Tests 2009-11-24 2 / 25

Page 3: Unit-Tests

Overview

1 Testing

2 PHPUnit

3 Advanced scenarios (optional)

4 The end

Tobias Schlitt (PHP World Kongress 2009) Unit-Tests 2009-11-24 3 / 25

Page 4: Unit-Tests

Outline

1 TestingMethods of testingUnit tests

2 PHPUnit

3 Advanced scenarios (optional)

4 The end

Tobias Schlitt (PHP World Kongress 2009) Unit-Tests 2009-11-24 4 / 25

Page 5: Unit-Tests

A little survey . . .

Do you test?

When?

How?

Who?

Tobias Schlitt (PHP World Kongress 2009) Unit-Tests 2009-11-24 5 / 25

Page 6: Unit-Tests

A little survey . . .

Do you test?

When?

How?

Who?

Tobias Schlitt (PHP World Kongress 2009) Unit-Tests 2009-11-24 5 / 25

Page 7: Unit-Tests

A little survey . . .

Do you test?

When?

How?

Who?

Tobias Schlitt (PHP World Kongress 2009) Unit-Tests 2009-11-24 5 / 25

Page 8: Unit-Tests

A little survey . . .

Do you test?

When?

How?

Who?

Tobias Schlitt (PHP World Kongress 2009) Unit-Tests 2009-11-24 5 / 25

Page 9: Unit-Tests

Ways of testing

Automatic vs. manual

Developer vs. tester

Internal vs. external

Back end vs. front end

Code vs. appearance

Functional vs. non-functional

Dynamic vs. static

Tobias Schlitt (PHP World Kongress 2009) Unit-Tests 2009-11-24 6 / 25

Page 10: Unit-Tests

Ways of testing

Automatic vs. manual

Developer vs. tester

Internal vs. external

Back end vs. front end

Code vs. appearance

Functional vs. non-functional

Dynamic vs. static

Tobias Schlitt (PHP World Kongress 2009) Unit-Tests 2009-11-24 6 / 25

Page 11: Unit-Tests

Ways of testing

Automatic vs. manual

Developer vs. tester

Internal vs. external

Back end vs. front end

Code vs. appearance

Functional vs. non-functional

Dynamic vs. static

Tobias Schlitt (PHP World Kongress 2009) Unit-Tests 2009-11-24 6 / 25

Page 12: Unit-Tests

Ways of testing

Automatic vs. manual

Developer vs. tester

Internal vs. external

Back end vs. front end

Code vs. appearance

Functional vs. non-functional

Dynamic vs. static

Tobias Schlitt (PHP World Kongress 2009) Unit-Tests 2009-11-24 6 / 25

Page 13: Unit-Tests

Ways of testing

Automatic vs. manual

Developer vs. tester

Internal vs. external

Back end vs. front end

Code vs. appearance

Functional vs. non-functional

Dynamic vs. static

Tobias Schlitt (PHP World Kongress 2009) Unit-Tests 2009-11-24 6 / 25

Page 14: Unit-Tests

Ways of testing

Automatic vs. manual

Developer vs. tester

Internal vs. external

Back end vs. front end

Code vs. appearance

Functional vs. non-functional

Dynamic vs. static

Tobias Schlitt (PHP World Kongress 2009) Unit-Tests 2009-11-24 6 / 25

Page 15: Unit-Tests

Ways of testing

Automatic vs. manual

Developer vs. tester

Internal vs. external

Back end vs. front end

Code vs. appearance

Functional vs. non-functional

Dynamic vs. static

Tobias Schlitt (PHP World Kongress 2009) Unit-Tests 2009-11-24 6 / 25

Page 16: Unit-Tests

Ways of testing

Automatic vs. manual

Developer vs. tester

Internal vs. external

Back end vs. front end

Code vs. appearance

Functional vs. non-functional

Dynamic vs. static

Tobias Schlitt (PHP World Kongress 2009) Unit-Tests 2009-11-24 6 / 25

Page 17: Unit-Tests

Test methods

Unit tests

Integration tests

Regression tests

Acceptance tests

Tobias Schlitt (PHP World Kongress 2009) Unit-Tests 2009-11-24 7 / 25

Page 18: Unit-Tests

Unit tests

Validate functionality

Test a single unit of code

Avoid regressions

Verify interfaces

Test bugs dedicatedly

Force code modularization

Migrate safely

Test driven development (TDD)

Tobias Schlitt (PHP World Kongress 2009) Unit-Tests 2009-11-24 8 / 25

Page 19: Unit-Tests

Unit tests

Validate functionality

Test a single unit of code

Avoid regressions

Verify interfaces

Test bugs dedicatedly

Force code modularization

Migrate safely

Test driven development (TDD)

Tobias Schlitt (PHP World Kongress 2009) Unit-Tests 2009-11-24 8 / 25

Page 20: Unit-Tests

Unit tests

Validate functionality

Test a single unit of code

Avoid regressions

Verify interfaces

Test bugs dedicatedly

Force code modularization

Migrate safely

Test driven development (TDD)

Tobias Schlitt (PHP World Kongress 2009) Unit-Tests 2009-11-24 8 / 25

Page 21: Unit-Tests

Unit tests

Validate functionality

Test a single unit of code

Avoid regressions

Verify interfaces

Test bugs dedicatedly

Force code modularization

Migrate safely

Test driven development (TDD)

Tobias Schlitt (PHP World Kongress 2009) Unit-Tests 2009-11-24 8 / 25

Page 22: Unit-Tests

Unit tests

Validate functionality

Test a single unit of code

Avoid regressions

Verify interfaces

Test bugs dedicatedly

Force code modularization

Migrate safely

Test driven development (TDD)

Tobias Schlitt (PHP World Kongress 2009) Unit-Tests 2009-11-24 8 / 25

Page 23: Unit-Tests

Unit tests

Validate functionality

Test a single unit of code

Avoid regressions

Verify interfaces

Test bugs dedicatedly

Force code modularization

Migrate safely

Test driven development (TDD)

Tobias Schlitt (PHP World Kongress 2009) Unit-Tests 2009-11-24 8 / 25

Page 24: Unit-Tests

Unit tests

Validate functionality

Test a single unit of code

Avoid regressions

Verify interfaces

Test bugs dedicatedly

Force code modularization

Migrate safely

Test driven development (TDD)

Tobias Schlitt (PHP World Kongress 2009) Unit-Tests 2009-11-24 8 / 25

Page 25: Unit-Tests

Unit tests

Validate functionality

Test a single unit of code

Avoid regressions

Verify interfaces

Test bugs dedicatedly

Force code modularization

Migrate safely

Test driven development (TDD)

Tobias Schlitt (PHP World Kongress 2009) Unit-Tests 2009-11-24 8 / 25

Page 26: Unit-Tests

Outline

1 Testing

2 PHPUnitInstallationPHPUnit basicsCode examples

3 Advanced scenarios (optional)

4 The end

Tobias Schlitt (PHP World Kongress 2009) Unit-Tests 2009-11-24 9 / 25

Page 27: Unit-Tests

PHPUnit

Port of JUnit to PHP

Many enhancements

Database testsCode coverageData providers

Invented by Sebastian Bergmann

Standard for unit testing in PHP

http://www.phpunit.de/

Tobias Schlitt (PHP World Kongress 2009) Unit-Tests 2009-11-24 10 / 25

Page 28: Unit-Tests

Installation

Via PEAR

$ pear channel-discover pear.phpunit.de

$ pear install phpunit/PHPUnit

Tobias Schlitt (PHP World Kongress 2009) Unit-Tests 2009-11-24 11 / 25

Page 29: Unit-Tests

PHPUnit basics

YourClass

Tobias Schlitt (PHP World Kongress 2009) Unit-Tests 2009-11-24 12 / 25

Page 30: Unit-Tests

PHPUnit basics

YourClass YourClassTest

Tobias Schlitt (PHP World Kongress 2009) Unit-Tests 2009-11-24 12 / 25

Page 31: Unit-Tests

PHPUnit basics

YourClass YourClassTest

Tobias Schlitt (PHP World Kongress 2009) Unit-Tests 2009-11-24 12 / 25

Page 32: Unit-Tests

PHPUnit basics

YourClass YourClassTest

TestCase

Tobias Schlitt (PHP World Kongress 2009) Unit-Tests 2009-11-24 12 / 25

Page 33: Unit-Tests

PHPUnit basics

YourClass YourClassTest

TestCase

TestRunner

Tobias Schlitt (PHP World Kongress 2009) Unit-Tests 2009-11-24 12 / 25

Page 34: Unit-Tests

PHPUnit basics

YourClass YourClassTest

TestCase

TestRunner

Tobias Schlitt (PHP World Kongress 2009) Unit-Tests 2009-11-24 12 / 25

Page 35: Unit-Tests

Let’s dig into some code

Let’s dig into some code

Tobias Schlitt (PHP World Kongress 2009) Unit-Tests 2009-11-24 13 / 25

Page 36: Unit-Tests

Outline

1 Testing

2 PHPUnit

3 Advanced scenarios (optional)Custom test environmentTesting a WebDAV serverTesting graphic generationTesting parsers

4 The end

Tobias Schlitt (PHP World Kongress 2009) Unit-Tests 2009-11-24 14 / 25

Page 37: Unit-Tests

eZ Component test runner

Custom test runner

Integrated auto loadingComponent base testingRun all tests

Extended test case / suite classes

Custom assertionsDatabase testingUtility functions

Tobias Schlitt (PHP World Kongress 2009) Unit-Tests 2009-11-24 15 / 25

Page 38: Unit-Tests

eZ Webdav component

Tobias Schlitt (PHP World Kongress 2009) Unit-Tests 2009-11-24 16 / 25

Page 39: Unit-Tests

Semi-automatic regression tests

Manual client test run

Defined test receipe

Capture request / response data

Replay request in unit tests

Detect response regressions

Tobias Schlitt (PHP World Kongress 2009) Unit-Tests 2009-11-24 17 / 25

Page 40: Unit-Tests

Binary regressions

Binary differences in generated images

Test failures depending on

PHP versionGD versionOperating system

Images visually equivalent

Tobias Schlitt (PHP World Kongress 2009) Unit-Tests 2009-11-24 18 / 25

Page 41: Unit-Tests

Custom assertions

Custom assertion

Custom equality constraint

Based on ImageMagickAllows defined gap

Tobias Schlitt (PHP World Kongress 2009) Unit-Tests 2009-11-24 19 / 25

Page 42: Unit-Tests

Untestable code

Parsers are highly dependant

De-coupling hardly possible

Huge amount of mock objects

Tobias Schlitt (PHP World Kongress 2009) Unit-Tests 2009-11-24 20 / 25

Page 43: Unit-Tests

Integration tests

Test parser as a whole

Small test files

Manually create / validate

Large overall tests

Manual inspection of generated data

Tobias Schlitt (PHP World Kongress 2009) Unit-Tests 2009-11-24 21 / 25

Page 44: Unit-Tests

Outline

1 Testing

2 PHPUnit

3 Advanced scenarios (optional)

4 The end

Tobias Schlitt (PHP World Kongress 2009) Unit-Tests 2009-11-24 22 / 25

Page 45: Unit-Tests

Questions / Answers

Questions? Feedback? Critics?

Tobias Schlitt (PHP World Kongress 2009) Unit-Tests 2009-11-24 23 / 25

Page 46: Unit-Tests

The end

I hope you enjoyed the session

Slides and material

http://schlitt.info/opensourcehttp://www.slideshare.net/tobyS

Contact: Tobias Schlitt <[email protected]>

Tobias Schlitt (PHP World Kongress 2009) Unit-Tests 2009-11-24 24 / 25

Page 47: Unit-Tests

PHPUnderControl

Tobias Schlitt (PHP World Kongress 2009) Unit-Tests 2009-11-24 25 / 25