70
SOFTWARE QUALITY VIA UNIT TESTING Shaun Abram April 20, 2013 Email: [email protected] Twitter: @shaunabram These slides available at shaunabram.com /dcc13 Blog: shaunabram.com LinkedIn: linkedin.com/in/sabram

Software Quality via Unit Testing

Embed Size (px)

DESCRIPTION

Software quality is critical to consistently and continually delivering new features to our users. This talk covers the importance of software quality and how to deliver it via unit testing, Test Driven Development and clean code in general. This is the deck from a talk I gave at Desert Code Camp 2013.

Citation preview

Page 1: Software Quality via Unit Testing

SOFTWARE QUALITY VIA UNIT TESTINGShaun Abram

April 20, 2013

Email: [email protected]

Twitter: @shaunabram

These slides available at shaunabram.com/dcc13

Blog: shaunabram.com

LinkedIn: linkedin.com/in/sabram

Page 2: Software Quality via Unit Testing

Software Quality via Unit Testing

The value of software design

Automated testing

Clean code

Goal:

Deliver value to our users

Page 3: Software Quality via Unit Testing

Software Quality via Unit Testing

The value of software design

Page 4: Software Quality via Unit Testing

Why should we care about ‘good’ design in software?

How do you respond?

Do we really need unit tests?

Refactoring doesn’t change what the code does, so why bother?

We need less focus on quality so we can add more features

Page 5: Software Quality via Unit Testing

Why should we care about ‘good’ design in software?

Take the moral high ground?

Page 6: Software Quality via Unit Testing

Why should we care about ‘good’ design in software?

We need to have economic reasons

Remember: Our goal is to deliver value to our users

Page 7: Software Quality via Unit Testing

What is Quality in software anyway?

Intuitive GUI

Few defects

Modular Design

Page 8: Software Quality via Unit Testing

What is Quality in software anyway?

Intuitive GUI

Few defects Visible to user

Modular Design Transparent to user

Page 9: Software Quality via Unit Testing

Fowler’s Design Stamina Hypothesis

Page 10: Software Quality via Unit Testing

Technical debtThe eventual consequences of poor design in a codebase

Page 11: Software Quality via Unit Testing

Technical debtThe eventual consequences of poor design in a codebase

Interest payments can come in the form of:

1. Bugs

2. Just understanding what the heck the current code does

3. Refactoring

4. Completing unfinished work

Page 12: Software Quality via Unit Testing

Technical debt

Pay down?

Accept?

But, don’t build bad on top of bad…

Page 13: Software Quality via Unit Testing

Design Stamina

Design

Business value to our clients

stamina to

continually and consistently deliver functionality

faster and with less bugs to our users

Page 14: Software Quality via Unit Testing

Clean code that works

• The value of software design • Automated testing

Page 15: Software Quality via Unit Testing

Unit testing

A unit test is a piece of code that executes a specific functionality (‘unit’) in the code, and • Confirms the behavior or result is as expected.• Determines if code is ‘fit for use’

Example…

Page 16: Software Quality via Unit Testing

16

Page 17: Software Quality via Unit Testing

17

Page 18: Software Quality via Unit Testing

18

Page 19: Software Quality via Unit Testing

19

Page 20: Software Quality via Unit Testing
Page 21: Software Quality via Unit Testing

21

Page 22: Software Quality via Unit Testing

22

Page 23: Software Quality via Unit Testing

23

Page 24: Software Quality via Unit Testing

24

Page 25: Software Quality via Unit Testing

What unit tests provide

Unit tests don’t necessarily help find bugs.

Instead, unit tests:• Drive design

Page 26: Software Quality via Unit Testing

What unit tests provide

Unit tests don’t necessarily help find bugs.

Instead, unit tests:• Drive design

• The tests act as the first user of the code, making you think about:• What should this code do• Border conditions (0, null, -ve, too big)

• Force you to use good design:• Short, focused methods • Dependency Injection

• Writing a class is different from using a class!

Page 27: Software Quality via Unit Testing

What unit tests provide

Unit tests don’t necessarily help find bugs.

Instead, unit tests:• Drive design• Act as safety buffers by finding regression bugs• Provide documentation

Page 28: Software Quality via Unit Testing

28

What unit tests provide

Unit tests don’t necessarily help find bugs.

Instead, unit tests:• Drive design• Act as safety buffers by finding regression bugs• Provide documentation

Can also be used on legacy codebases

Page 29: Software Quality via Unit Testing

Unit testing limitations

1. Can not prove the absence of bugs

2. Lot’s of code (x3-5)

3. Some things difficult to test

Page 30: Software Quality via Unit Testing

So should we unit test?

Not only should we unit test,

We should let unit tests drive development and design…

Test Driven Development (TDD)

Page 31: Software Quality via Unit Testing

Test Driven Development (TDD)

Page 32: Software Quality via Unit Testing

Test Driven Development (TDD)

Page 33: Software Quality via Unit Testing

Test Driven Development (TDD)

Page 34: Software Quality via Unit Testing

Test Driven Development (TDD)

Red - Green – Refactor: the TDD Mantra

No new functionality without a failing test

No refactoring without passing tests

Page 35: Software Quality via Unit Testing

Test Driven Development (TDD) Example

Write a simple StringCalculator class with a method

Integer add(String numbers)

A String of comma separated numbers should return their sum e.g. “1,2,10” should return 13.

A single number String should return that number e.g. “3” should return 3

An empty String should return 0

For brevity, our test will focus on valid inputs.

Page 36: Software Quality via Unit Testing

36

Test Driven Development (TDD) Example

Write a simple StringCalculator class with a method

Integer add(String numbers)

Code demo…

Page 37: Software Quality via Unit Testing

37

Page 38: Software Quality via Unit Testing
Page 39: Software Quality via Unit Testing
Page 40: Software Quality via Unit Testing
Page 41: Software Quality via Unit Testing
Page 42: Software Quality via Unit Testing
Page 43: Software Quality via Unit Testing

Refactor?

Page 44: Software Quality via Unit Testing
Page 45: Software Quality via Unit Testing
Page 46: Software Quality via Unit Testing
Page 47: Software Quality via Unit Testing
Page 48: Software Quality via Unit Testing

Refactor?

Page 49: Software Quality via Unit Testing
Page 50: Software Quality via Unit Testing
Page 51: Software Quality via Unit Testing
Page 52: Software Quality via Unit Testing
Page 53: Software Quality via Unit Testing
Page 54: Software Quality via Unit Testing
Page 55: Software Quality via Unit Testing
Page 56: Software Quality via Unit Testing
Page 57: Software Quality via Unit Testing

These tests act like the original developer looking over your shoulder and advising you, long after that developer has left…

Page 58: Software Quality via Unit Testing

Test Driven Development (TDD)

Red - Green – Refactor: the TDD Mantra

No new functionality without a failing test

No refactoring without passing tests

Page 59: Software Quality via Unit Testing

Clean code that works

• The value of software design • Automated testing

Page 60: Software Quality via Unit Testing

Clean code that works

• The value of software design • Automated testing • Clean code

Page 61: Software Quality via Unit Testing

• Feedback…

• XKCD

Page 62: Software Quality via Unit Testing

62

Page 63: Software Quality via Unit Testing

Code SmellsWhat are code smells?

“Certain structures in code suggest (sometimes they scream for) the possibility of refactoring.”

Martin Fowler. Refactoring: Improving the design of existing code

Page 64: Software Quality via Unit Testing

Code Smells

• Duplicated code• Long switch/if statements• Long methods

Even one line methods can be OK:if ( (account != null) && ( (account.getBalance() > 0) || (!account.overdraftLimitReached()) ) {

}

if (account.hasFundsAvailable()) {

}

Page 65: Software Quality via Unit Testing

Code Smells

• Duplicated code• Long switch/if statements• Long methods• Poor method names

int process(int id) { //bad!

int calculateAccountBalance(int accountID) { //better

Page 66: Software Quality via Unit Testing

Code Smells

• Duplicated code• Long switch/if statements• Long methods• Poor method names• In-line comments• Large classes

• Symptoms• Too many methods (>10 public?)• Too many instance variables – is every instance variable used in every

method?• Solutions

• Eliminate redundancy / duplicated code• Extract new/sub classes

Page 67: Software Quality via Unit Testing

Clear Code

Make the intent of your code clearCode should be clear, concise and easy to understand

How many times will the code you are about to write be read?

Studies show poor readability correlates strongly with defect density1

Avoid attrition & complete re-writes1 "Learning a Metric for Code Readability," IEEE Transactions on Software Engineering, 09 Nov. 2009. IEEE computer Society Digital Library. IEEE Computer Society

Page 68: Software Quality via Unit Testing

Summary• Good design gives us the stamina to continually and

consistently deliver business value

• Unit tests are an integral part of good design; TDD is even better

• Good design can also simply be cleaner code; Aggressively refactor to achieve this!

Final thought:

Every time you are in a piece of code,

just make one small improvement!

Page 69: Software Quality via Unit Testing

Effective Unit TestingLasse Koskela

Refactoring: Improving the Design of Existing CodeMartin Fowler, Kent Beck et. al.

Recommended Reading

Test Driven DevelopmentKent Beck

Growing Object-Oriented Software, Guided by TestsFreeman & Pryce

Page 70: Software Quality via Unit Testing

70

Questions?

Email: [email protected]

Twitter: @shaunabramBlog: shaunabram.com

LinkedIn: linkedin.com/in/sabram

All slides available at: shaunabram.com/dcc13