16
#JavaOne @spoole167 @stuartmarks TEN SIMPLE RULES FOR WRITING GREAT TESTCASES

Ten Simple Rules for Writing Great Test Cases (JavaOne 2017)

Embed Size (px)

Citation preview

#JavaOne @spoole167 @stuartmarks

TEN SIMPLE RULES FOR WRITING

GREAT TESTCASES

#JavaOne @spoole167 @stuartmarks

Steve Poole : IBM

JVM Developer

DevOps practitioner

Developer Advocate

Stuart Marks : Oracle

Principal MTS

Java / OpenJDK Core Libraries

#JavaOne @spoole167 @stuartmarks

“I get paid for code that works, not for tests”

How to maximize your effort

and protect your investment

in tests and testing now with

added

Cloud

#JavaOne @spoole167 @stuartmarks

1. Think before you act

2. Make your tests understandable

3. Keep your tests “small and simple”

4. Test one thing only

5. Fast tests only

6. Absolute repeatability

7. Independent tests only

8. Provide diagnostic data on failure

9. No hard-coding of your environment

10.No extraneous output

#JavaOne @spoole167 @stuartmarks

1. Think before you act

What are

you testing?

Why are you

testing?

Plan

htt

ps:/

/ww

w.f

lickr.

co

m/p

ho

tos/a

yu

rve

dic

me

dic

ine

s/

#JavaOne @spoole167 @stuartmarks

Unit

tests

Function

tests

System

tests

Developer QA

Easy to

understand

Long runningFast running

Complex

Domain

Knowledge

required

Dev

QA

Don’t think like this Think like this

#JavaOne @spoole167 @stuartmarks

2. Make your tests

understandable

Comments

Expected behaviour

Aid debug

Diagnostics

https://w

ww

.flic

kr.

com

/photo

s/8

3633410@

N07/

#JavaOne @spoole167 @stuartmarks

3. Keep your tests

“small and simple”

Separate test logic / setup

Much easier to debug

Use setup / teardown

https://www.flickr.com/photos/9266144@N02/

#JavaOne @spoole167 @stuartmarks

4. Test one thing only

One scenario per test

Enables fast debug

Obvious why test failed

https://www.flickr.com/photos/ryantron/

#JavaOne @spoole167 @stuartmarks

5. Fast tests only

Run unit tests often as possible

Maintain quality bar

Quick results

https://www.flickr.com/photos/mcleod/

#JavaOne @spoole167 @stuartmarks

6. Absolute repeatability

Non-deterministic

tests are a headache

Fix intermittent tests

immediately

No value, waste of resource

Must trust all tests

https://www.flickr.com/photos/fdecomite/

#JavaOne @spoole167 @stuartmarks

7. Independent tests only

Must run in any order

Run subset, faster results

No dependencies

https://w

ww

.flic

kr.

com

/photo

s/s

heila

_sund

/

#JavaOne @spoole167 @stuartmarks

8. Provide diagnostic

data on failure

Use message in asserts

Make it simple to debug

Reference input data

Record test environment info https://w

ww

.flic

kr.

com

/photo

s/d

avid

baker/

#JavaOne @spoole167 @stuartmarks

9. No hard-coding of

your environment

No ports, IP addresses,

data files, databases

Use config files, system

properties or mock objects

Portable tests

https://w

ww

.flic

kr.

com

/photo

s/p

ug50/

#JavaOne @spoole167 @stuartmarks

10. No extraneous output

A passing test is a silent test

Too much output = confusion

Use option, config file to

turn on debug, save output

https://w

ww

.flic

kr.

com

/photo

s/3

-bs/

#JavaOne @spoole167 @stuartmarks

Pirate rules

Guidelines only

Some may seen obvious.

Some may seem pedantic

Until you inherit a testsuite…

Thank you.

https://www.flickr.com/photos/gapic/