Ten things you should know when writing good unit test cases

16
© 2013 IBM Corporation Paul Thwaite IBM, Stuart Marks Oracle. BOF 4255 Ten Things You Should Know When Writing Good Unit Test Cases in Java Document number

description

A JavaOne BOF session on ten things you should know when writing good unit test cases. These ten simple rules will help guide developers to write solid unit tests which will be easy to run, debug and maintain.

Transcript of Ten things you should know when writing good unit test cases

Page 1: Ten things you should know when writing good unit test cases

© 2013 IBM Corporation

Paul Thwaite IBM, Stuart Marks Oracle.

BOF 4255

Ten Things You Should Know When Writing Good Unit Test Cases in Java

Document number

Page 2: Ten things you should know when writing good unit test cases

© 2013 IBM Corporation

Important Disclaimers

THE INFORMATION CONTAINED IN THIS PRESENTATION IS PROVIDED FOR INFORMATIONAL PURPOSES ONLY.

WHILST EFFORTS WERE MADE TO VERIFY THE COMPLETENESS AND ACCURACY OF THE INFORMATION CONTAINED IN THIS PRESENTATION, IT IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED.

ALL PERFORMANCE DATA INCLUDED IN THIS PRESENTATION HAVE BEEN GATHERED IN A CONTROLLED ENVIRONMENT. YOUR OWN TEST RESULTS MAY VARY BASED ON HARDWARE, SOFTWARE OR INFRASTRUCTURE DIFFERENCES.

ALL DATA INCLUDED IN THIS PRESENTATION ARE MEANT TO BE USED ONLY AS A GUIDE.

IN ADDITION, THE INFORMATION CONTAINED IN THIS PRESENTATION IS BASED ON IBM’S CURRENT PRODUCT PLANS AND STRATEGY, WHICH ARE SUBJECT TO CHANGE BY IBM, WITHOUT NOTICE.

IBM AND ITS AFFILIATED COMPANIES SHALL NOT BE RESPONSIBLE FOR ANY DAMAGES ARISING OUT OF THE USE OF, OR OTHERWISE RELATED TO, THIS PRESENTATION OR ANY OTHER DOCUMENTATION.

NOTHING CONTAINED IN THIS PRESENTATION IS INTENDED TO, OR SHALL HAVE THE EFFECT OF:

- CREATING ANY WARRANT OR REPRESENTATION FROM IBM, ITS AFFILIATED COMPANIES OR ITS OR THEIR SUPPLIERS AND/OR LICENSORS

2

Page 3: Ten things you should know when writing good unit test cases

© 2013 IBM Corporation

Who are we?

Paul Thwaite– QA Engineer, IBM

Stuart Marks– Principal Member of Technical Staff, Oracle

Page 4: Ten things you should know when writing good unit test cases

© 2013 IBM Corporation

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

Page 5: Ten things you should know when writing good unit test cases

© 2013 IBM Corporation

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

What are you testing?

Why are you testing?

Plan

Page 6: Ten things you should know when writing good unit test cases

© 2013 IBM Corporation

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

Comments

Expected behaviour

Aid debug

Diagnostics

Page 7: Ten things you should know when writing good unit test cases

© 2013 IBM Corporation

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

Separate test logic / setup

Much easier to debug

Use setup / teardown

Page 8: Ten things you should know when writing good unit test cases

© 2013 IBM Corporation

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

One scenario per test

Enables fast debug

Obvious why test failed

Page 9: Ten things you should know when writing good unit test cases

© 2013 IBM Corporation

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

Run unit tests often as possible

Maintain quality bar

Quick results

Page 10: Ten things you should know when writing good unit test cases

© 2013 IBM Corporation

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

Non-deterministic testsare a headache

Fix intermittent tests immediately

No value, waste of resource

Must trust all tests

Page 11: Ten things you should know when writing good unit test cases

© 2013 IBM Corporation

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

Must run in any order

Run subset, faster results

No dependencies

Page 12: Ten things you should know when writing good unit test cases

© 2013 IBM Corporation

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

Use message in asserts

Make it simple to debug

Reference input data

Record test environment info

Page 13: Ten things you should know when writing good unit test cases

© 2013 IBM Corporation

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

No ports, IP addresses,data files, databases

Use config files, systemproperties or mock objects

Portable tests

Page 14: Ten things you should know when writing good unit test cases

© 2013 IBM Corporation

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

A passing test is a silent test

Too much output = confusion

Use option, config file toturn on debug, save output

Page 15: Ten things you should know when writing good unit test cases

© 2013 IBM Corporation

Over to you

What are your experiences with unit testing ideals vs. real world?

What testing problems and pathologies have you encountered?

What are the root causes of your testing problems?

What is the cost of test maintenance?

Page 16: Ten things you should know when writing good unit test cases

© 2013 IBM Corporation16

Visit IBM booth #5112

Visit the IBM booth 5112 for QA queries–09:30 – 12:00–Tuesday 24 September

Meet other IBM developers at JavaOne

Checkout what IBM is doing at JavaOne

Tue 24 Sept, 09:30-12:00http://ibm.co/JavaOne