Unit and integration Testing

  • View
    1.613

  • Download
    0

  • Category

    Software

Preview:

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.

Citation preview

UNIT & INTEGRATION

TESTING

David Berl iner

Why Test

Types of Tests

PHPUnit

Writing Tests

Getting Stuck In

WHY TEST?

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

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

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 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 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 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

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

TYPES OF TESTS

Black Box

White Box

Unit

Integrat ion

Funct ional

System

Regression

Performance

Smoke

Canary

Usabi l i ty

A/B

Black Box

White Box

Unit

Integration

Funct ional

System

Regression

Performance

Smoke

Canary

Usabi l i ty

A/B

The goal of unit testing is:

1. to isolate each part of the program, and

U N I T T E S T S

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.

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.

Pro’s:

1. Fast

U N I T T E S T S

Pro’s:

1. Fast

U N I T T E S T S

2. Simple to understand

Pro’s:

1. Fast

U N I T T E S T S

2. Simple to understand

3. Reliable

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

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

U N I T T E S T S

A unit test should NOT:

1. Access the network

U N I T T E S T S

A unit test should NOT:

1. Access the network

2. Hit a database

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

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

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

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

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

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

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

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

PHPUnit

1. Unit Testing Framework written in PHP by Sebastian Bergmann

2. De facto standard

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

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

WRITING TESTS

- 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

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

P H P U N I T. X M L

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

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( )

A S S E R T I O N S

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

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

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

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?

M O C K S & S T U B S

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

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

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

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

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

GETTING STUCK IN

Demo:

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

• 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

Recommended