Unit-Tests

Preview:

DESCRIPTION

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

Citation preview

Unit-TestsSchnell und einfach selbst gemacht

TobiasSchlitt <toby@php.net>

PHP World Kongress 2009

2009-11-24

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

About me

Tobias Schlitt <toby@php.net>

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

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

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

A little survey . . .

Do you test?

When?

How?

Who?

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

A little survey . . .

Do you test?

When?

How?

Who?

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

A little survey . . .

Do you test?

When?

How?

Who?

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

A little survey . . .

Do you test?

When?

How?

Who?

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

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

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

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

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

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

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

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

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

Test methods

Unit tests

Integration tests

Regression tests

Acceptance tests

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

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

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

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

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

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

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

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

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

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

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

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

PHPUnit basics

YourClass

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

PHPUnit basics

YourClass YourClassTest

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

PHPUnit basics

YourClass YourClassTest

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

PHPUnit basics

YourClass YourClassTest

TestCase

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

PHPUnit basics

YourClass YourClassTest

TestCase

TestRunner

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

PHPUnit basics

YourClass YourClassTest

TestCase

TestRunner

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

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

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

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

eZ Webdav component

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

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

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

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

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

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

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

Questions / Answers

Questions? Feedback? Critics?

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

The end

I hope you enjoyed the session

Slides and material

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

Contact: Tobias Schlitt <toby@php.net>

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

PHPUnderControl

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

Recommended