Successful Unit testing for large applications

31
SUCCESSFUL UNIT TESTING FOR LARGE APPLICATIONS Erik LeBel [email protected]

Transcript of Successful Unit testing for large applications

Page 1: Successful Unit testing for large applications

SUCCESSFUL UNIT

TESTING FOR LARGE

APPLICATIONS

Erik LeBel

[email protected]

Page 2: Successful Unit testing for large applications

© P

yxis

Te

chnolo

gie

s in

c.

PYXIS STUDIO

Page 3: Successful Unit testing for large applications

SETTING THE SCENE

Page 4: Successful Unit testing for large applications

© P

yxis

Te

chnolo

gie

s in

c.

You are trying to reduce frequency with

which regressions get pushed into

production

You are trying to speed up validation for

quicker release cycles

You are cool

You‟ve been told to do so by someone

higher up in your organization

IF YOU ARE USING AUTOMATED

TESTING, THEN…

Page 5: Successful Unit testing for large applications

© P

yxis

Te

chnolo

gie

s in

c.

Your project has tests, but they aren't

helping you deliver stable software

Your project has tests, but they‟re a

nightmare to maintain

Your project has tests, but they are a

haphazard mess in the making

You like tests

YOU ARE INTERESTED IN THIS

PRESENTATION BECAUSE…

Page 6: Successful Unit testing for large applications

© P

yxis

Te

chnolo

gie

s in

c.

We will be covering:

practices and concerns to help make

automated tests an enabling force

We are not discussing:

why we automate testing

Page 7: Successful Unit testing for large applications

© P

yxis

Te

chnolo

gie

s in

c.

SO, WHAT‟S THIS ABOUT DEBILITATING

TESTS?

Page 8: Successful Unit testing for large applications

© P

yxis

Te

chnolo

gie

s in

c.

Tests:

Are hard to understand

Break frequently

Are slow to execute

Are hard to fix

Let bugs through

Don‟t validate anything

useful

But we want them:

To be clear to

understand

To break legitimately

To be fast to execute

To be concise

THIS IS OFTEN BECAUSE…

Page 9: Successful Unit testing for large applications

MAKE TESTS

READABLE

Page 10: Successful Unit testing for large applications

© P

yxis

Te

chnolo

gie

s in

c.

Use OO practices to:

Make setup code easy to read

Unclutter

Use factory methods

Use fluent builders

Can build complex actions as well as

objects

CLEAN UP TEST SETUP

Page 11: Successful Unit testing for large applications

© P

yxis

Te

chnolo

gie

s in

c.

Simplify result validation

Can easily handle unordered cases

Write your own if your test framework

doesn‟t have any

COLLECTION ASSERTS

Page 12: Successful Unit testing for large applications

© P

yxis

Te

chnolo

gie

s in

c.

They are easier to read

USE LITERALS

Page 13: Successful Unit testing for large applications

© P

yxis

Te

chnolo

gie

s in

c.

Use extension methods for concise type

assertions

Build utility methods for complex

validations

BUILD INTUITIVE ASSERTIONS

Page 14: Successful Unit testing for large applications

© P

yxis

Te

chnolo

gie

s in

c.

Domain Specific Languages (DSLS)

Test API wrappers

Model wrappers

USE TEST DSLS FOR INTEGRATION

TESTS

Page 15: Successful Unit testing for large applications

© P

yxis

Te

chnolo

gie

s in

c.

Capture large expected value sets in

common file formats such as XML, JSON,

YAML

Write assertion utilities that highlight

multiple differences between expected and

actual values

(eg XmlUnit)

USE READABLE DATA FORMATS

Page 16: Successful Unit testing for large applications

© P

yxis

Te

chnolo

gie

s in

c.

You will more easily spot redundant or

useless tests

Tests should start to match business

requirements

Its should start to be clearer where there are

gaps

The factories, builders and helpers that you

develop for testing will make it easier to add

new tests scenarios

AS TESTS GET CLEANER

Page 17: Successful Unit testing for large applications

© P

yxis

Te

chnolo

gie

s in

c.

„If‟ statements in tests

Loops in tests

Exception handling

AT ALL COST, AVOID…

Page 18: Successful Unit testing for large applications

BALANCE

Page 19: Successful Unit testing for large applications

© P

yxis

Te

chnolo

gie

s in

c.

THE NUMBERS SHOW THAT…

System tests

Integration tests

Unit tests

Time to execute

Fragility

Lines of test code

Effort to fix

Overall cost of use

$$$

$

Page 20: Successful Unit testing for large applications

© P

yxis

Te

chnolo

gie

s in

c.

Module1.dll

Module1.Unit.Tests.dll

Module1.Integration.Tests.dll

Module2.dll

Module2.Unit.Tests.dll

Module2.Integration.Tests.dll

BUILD YOUR TESTS AROUND YOUR

MODULES

Page 21: Successful Unit testing for large applications

© P

yxis

Te

chnolo

gie

s in

c.

Configurations

Databases

File systems

External services

Sandboxing technologies

Flashy, shiny widgets

Use them… don‟t let it be the other way

around

TECHNOLOGIES THAT HINDER TESTING

Page 22: Successful Unit testing for large applications

© P

yxis

Te

chnolo

gie

s in

c.

Don‟t expect to stop delivering software while fixing things

Don‟t be afraid to experiment

When a pattern emerges, make sure it‟s communicated to the whole team

Don‟t get hung up on the “green field” vs “brown field” over time even the greenest fields get muddy

This is and will continue to be an organic process

GETTING THERE IN BABY STEPS

Page 23: Successful Unit testing for large applications

BUILDING TRUST

Page 24: Successful Unit testing for large applications

© P

yxis

Te

chnolo

gie

s in

c.

Commit to keeping tests green all the time.

If it‟s broken, fix it, or delete it.

DO OR DO NOT

Page 25: Successful Unit testing for large applications

© P

yxis

Te

chnolo

gie

s in

c.

Use a Continuous Integration tool:

TFS, CruiseControl.NET, Hudson/Jenkins,

roll your own

Don‟t allow the build to stay broken

Make sure your integration build and tests

are run as often as possible

Split long-running test suites to run as part

of an alternate set of build jobs

AUTOMATE TEST EXECUTION

Page 26: Successful Unit testing for large applications

© P

yxis

Te

chnolo

gie

s in

c.

Work to improve test coverage

Beware of high test coverage and low

software quality

And remember it‟s just an indicator

WATCH CODE COVERAGE

Page 27: Successful Unit testing for large applications

© P

yxis

Te

chnolo

gie

s in

c.

Monitor test and production failures

Use production issues to infer how deep

the iceberg is

When something fails, find the cause, and

improve your test and application design

INSPECT AND ADAPT

Page 28: Successful Unit testing for large applications

© P

yxis

Te

chnolo

gie

s in

c.

Test the reliability of your tests by seeding

bugs and checking the detection rate

Automate the activity with a mutation

testing framework:

See http://www.mutation-testing.net

FAULT SEEDING

Page 29: Successful Unit testing for large applications

CONCLUSION

Page 30: Successful Unit testing for large applications

© P

yxis

Te

chnolo

gie

s in

c.

Treat your tests as first order citizens

Work to improve your feedback cycle

Commit or get out, but don‟t go in halfway

TAKE AWAY

Page 31: Successful Unit testing for large applications

Titre sur mesure

POINTS FORTS

1

Thank you!

Questions?

pyxis-tech.com

Erik LeBel

[email protected]