Unit and integration Testing

57
UNIT & INTEGRATION TESTING David Berliner

description

Lets break some code. In this workshop we will be going over some of the fundamental concepts of software testing and take a hands on approach to writing Unit and Integration tests. We will cover topics such as mocking, stubbing, database patching and how this can all fit into a continuous integration environment like Jenkins.

Transcript of Unit and integration Testing

Page 1: Unit and integration Testing

UNIT & INTEGRATION

TESTING

David Berl iner

Page 2: Unit and integration Testing

Why Test

Types of Tests

PHPUnit

Writing Tests

Getting Stuck In

Page 3: Unit and integration Testing
Page 4: Unit and integration Testing

WHY TEST?

Page 5: Unit and integration Testing

T E S T S R E D U C E B U G S

Page 6: Unit and integration Testing

T E S T S R E D U C E B U G S

T E S T S A R E G O O D D O C U M E N TAT I O N

Page 7: Unit and integration Testing

T E S T S R E D U C E B U G S

T E S T S A R E G O O D D O C U M E N TA T I O N

T E S T S A L L O W S A F E R E FA C T O R I N G

Page 8: Unit and integration Testing

T E S T S R E D U C E B U G S

T E S T S A R E G O O D D O C U M E N TA T I O N

T E S T S A L L O W S A F E R E FA C T O R I N G

T E S T S R E D U C E T H E C O S T O F C H A N G E

Page 9: Unit and integration Testing

T E S T S R E D U C E B U G S

T E S T S A R E G O O D D O C U M E N TA T I O N

T E S T S A L L O W S A F E R E FA C T O R I N G

T E S T S R E D U C E T H E C O S T O F C H A N G E

T E S T I N G F O R C E S Y O U T O T H I N K

Page 10: Unit and integration Testing

T E S T S R E D U C E B U G S

T E S T S A R E G O O D D O C U M E N TA T I O N

T E S T S A L L O W S A F E R E FA C T O R I N G

T E S T S R E D U C E T H E C O S T O F C H A N G E

T E S T I N G F O R C E S Y O U T O T H I N K

T E S T S R E D U C E F E A R

Page 11: Unit and integration Testing

A study conducted by Microsoft and IBM showed that writing tests can

add 15% – 35% to development time but reduce the number of bugs by

40% – 90%.

http://research.microsoft.com/en-us/groups/ese/nagappan_tdd.pdf

Page 12: Unit and integration Testing

TYPES OF TESTS

Page 13: Unit and integration Testing

Black Box

White Box

Unit

Integrat ion

Funct ional

System

Regression

Performance

Smoke

Canary

Usabi l i ty

A/B

Page 14: Unit and integration Testing

Black Box

White Box

Unit

Integration

Funct ional

System

Regression

Performance

Smoke

Canary

Usabi l i ty

A/B

Page 15: Unit and integration Testing

The goal of unit testing is:

1. to isolate each part of the program, and

U N I T T E S T S

Page 16: Unit and integration Testing

The goal of unit testing is:

1. to isolate each part of the program, and

U N I T T E S T S

2. show that the individual parts are correct.

Page 17: Unit and integration Testing

The goal of unit testing is:

1. to isolate each part of the program, and

U N I T T E S T S

2. show that the individual parts are correct.

Unit tests have a very narrow and well-defined scope.

Page 18: Unit and integration Testing

Pro’s:

1. Fast

U N I T T E S T S

Page 19: Unit and integration Testing

Pro’s:

1. Fast

U N I T T E S T S

2. Simple to understand

Page 20: Unit and integration Testing

Pro’s:

1. Fast

U N I T T E S T S

2. Simple to understand

3. Reliable

Page 21: Unit and integration Testing

Pro’s:

1. Fast

U N I T T E S T S

2. Simple to understand

3. Reliable

Con’s:

1. Large time investment

Page 22: Unit and integration Testing

Pro’s:

1. Fast

U N I T T E S T S

2. Simple to understand

3. Reliable

Con’s:

1. Large time investment

2. Requires maintenance

Page 23: Unit and integration Testing

U N I T T E S T S

A unit test should NOT:

1. Access the network

Page 24: Unit and integration Testing

U N I T T E S T S

A unit test should NOT:

1. Access the network

2. Hit a database

Page 25: Unit and integration Testing

U N I T T E S T S

A unit test should NOT:

1. Access the network

2. Hit a database

3. Use the file system

Page 26: Unit and integration Testing

U N I T T E S T S

A unit test should NOT:

1. Access the network

2. Hit a database

3. Use the file system

4. Call other non-trivial components

Page 27: Unit and integration Testing

I N T E G R AT I O N T E S T S

Test the inter-operation of multiple subsystems.

Pro’s:1. Make sure nuts and bolts fit together

Page 28: Unit and integration Testing

I N T E G R AT I O N T E S T S

Test the inter-operation of multiple subsystems.

Pro’s:1. Make sure nuts and bolts fit together

2. Test behaviour and infrastructure

Page 29: Unit and integration Testing

I N T E G R AT I O N T E S T S

Test the inter-operation of multiple subsystems.

Pro’s:1. Make sure nuts and bolts fit together

2. Test behaviour and infrastructure

3. (tested code) / test % is high

Page 30: Unit and integration Testing

I N T E G R AT I O N T E S T S

Test the inter-operation of multiple subsystems.

Pro’s:1. Make sure nuts and bolts fit together

2. Test behaviour and infrastructure

3. (tested code) / test % is high

Con’s:1. Hard to test all critical paths

Page 31: Unit and integration Testing

I N T E G R AT I O N T E S T S

Test the inter-operation of multiple subsystems.

Pro’s:1. Make sure nuts and bolts fit together

2. Test behaviour and infrastructure

3. (tested code) / test % is high

Con’s:1. Hard to test all critical paths

2. Harder to localise source of errors

Page 32: Unit and integration Testing

T E S T H I E R A R C H Y

Page 33: Unit and integration Testing
Page 34: Unit and integration Testing

PHPUnit

Page 35: Unit and integration Testing

1. Unit Testing Framework written in PHP by Sebastian Bergmann

2. De facto standard

3. Major Frameworks use it (Zend, Cake, Laravel, Symphony etc.)

Page 36: Unit and integration Testing

Installation:

https://phpunit.de/manual/current/en/installation.html

Be sure to install xDebug in order to generate code coverage.

Note: PECL no longer supported

Page 37: Unit and integration Testing

WRITING TESTS

Page 38: Unit and integration Testing

- The tests for class Foo are placed in a class FooTest

- Most of the time you will inherit from PHPUnit_Framework_TestCase PHPUnit_Extensions_Database_TestCase

- Tests are public methods named test*

- Inside the test methods, assertion methods such as assertEquals() are used.

O R G A N I S I N G T E S T S

Page 39: Unit and integration Testing

O R G A N I S I N G T E S T S

Tests should mirror the code being tested.

SRC

SomeFolder

Baz.php

Foo.php

Bar.php

TEST

SomeFolder

BazTest.php

FooTest.php

BarTest.php

Page 40: Unit and integration Testing

P H P U N I T. X M L

Page 41: Unit and integration Testing

B O O T S T R A P. P H P

Page 42: Unit and integration Testing

A S S E R T I O N S

assertArrayHasKey( )

assertClassHasAttr ibute( )

assertClassHasStat icAttr ibute( )

assertContains( )

assertContainsOnly( )

assertContainsOnlyInstancesOf( )

assertCount( )

assertEmpty( )

assertEqualXMLStructure( )

assertEquals( )

assertFalse( )

assertFi leEquals( )

assertFi leExists ( )

assertGreaterThan( )

assertGreaterThanOrEqual ( )

assert InstanceOf( )

assert InternalType( )

assertJsonFi leEqualsJsonFi le( )

assertJsonStr ingEqualsJsonFi le( )

assertJsonStr ingEqualsJsonStr ing( )

assertLessThan( )

assertLessThanOrEqual ( )

assertNul l ( )

assertObjectHasAttr ibute( )

assertRegExp()

assertStr ingMatchesFormat( )

assertStr ingMatchesFormatFi le( )

assertSame()

assertStr ingEndsWith( )

assertStr ingEqualsFi le( )

assertStr ingStartsWith( )

assertThat( )

assertTrue( )

assertXmlFi leEqualsXmlFi le( )

assertXmlStr ingEqualsXmlFi le( )

assertXmlStr ingEqualsXmlStr ing( )

Page 43: Unit and integration Testing

A S S E R T I O N S

Page 44: Unit and integration Testing

A N N O TAT I O N S @ D E P E N D S

Page 45: Unit and integration Testing

A N N O TAT I O N S @ D ATA P R O V I D E R

Page 46: Unit and integration Testing

A N N O TAT I O N S @ E X C E P T I O N S

Page 47: Unit and integration Testing

What do you do i f the code you want to test is dependent on other components that cannot be used in the test environment?

Page 48: Unit and integration Testing

M O C K S & S T U B S

Page 49: Unit and integration Testing

D ATA B A S E T E S T I N G

Four stages of a DB test

1. Set up fixture

2. Exercise System Under Test

3. Verify outcome

4. Teardown

Page 50: Unit and integration Testing

D ATA B A S E T E S T I N G ( C O N T ) Give it a connection

Page 51: Unit and integration Testing

D ATA B A S E T E S T I N G ( C O N T ) Flat XML DataSet

Page 52: Unit and integration Testing

D ATA B A S E T E S T I N G ( C O N T )

Page 53: Unit and integration Testing

D ATA B A S E T E S T I N G ( C O N T )

Page 54: Unit and integration Testing

GETTING STUCK IN

Page 55: Unit and integration Testing

Demo:

https://github.com/manatok/talk-demo-ci

Page 56: Unit and integration Testing

• db - Database Skel f i le and patches

• src - Project Code

• test/Output - Code coverage

• test/SportsBet - Projects Tests

• test/TestingCore - Ut i l i t ies

• tools - CI tools including ANT bui ld f i le

Page 57: Unit and integration Testing