Tdd & clean code

62
UNIT TESTING, TDD & ATDD Arnon Axelrod, E4D Solutions

description

TDD, ATDD & Clean Code

Transcript of Tdd & clean code

Page 1: Tdd & clean code

UNIT TESTING, TDD & ATDD

Arnon Axelrod, E4D Solutions

Page 2: Tdd & clean code

© A

rnon A

xelro

d, E

4D

Solu

tions Ltd

.

ABOUT ME

Arnon Axelrod

TDD and Agile Quality Expert

E4D Solutions

Page 3: Tdd & clean code

© A

rnon A

xelro

d, E

4D

Solu

tions Ltd

.

ABOUT YOU

Position: Dev, Test, Product, Management, other?

TDD/Unit testing experience

Page 4: Tdd & clean code

© A

rnon A

xelro

d, E

4D

Solu

tions Ltd

.

WHY LEARN TDD TODAY?

Adoption (% of martket)

time

Today

ATDD

TDD

Agile

Object-Oriented

Page 5: Tdd & clean code

© A

rnon A

xelro

d, E

4D

Solu

tions Ltd

.

Photo by: Stuart Miles

TDD?!

Page 6: Tdd & clean code

© A

rnon A

xelro

d, E

4D

Solu

tions Ltd

.

Learning TDD is like learning to ride a bicycle

Page 7: Tdd & clean code

Quality

Testing

Unit Tests TDD ATDD

QUALITY THROUGHOUT THE PROJECT LIFECYCLE

What is quality? Why it is important?

Quality

Page 8: Tdd & clean code

© A

rnon A

xelro

d, E

4D

Solu

tions Ltd

.

QUALITY

No bugs

Stability

TestingDesign

Happy, Loyal customers

User eXperience

Clean code

Maintainability

Customer feedback

Page 9: Tdd & clean code

© A

rnon A

xelro

d, E

4D

Solu

tions Ltd

.

THE PRODUCTIVITY MISCONCEPTION

Productivity

Features

Happy customers

MaintainabilityPressure

Legend:

Positive relationshipNegative relationship

Increased

Decreased

Productivity

Features

Happy customers

Stability

Clean code

Testing

Page 10: Tdd & clean code

© A

rnon A

xelro

d, E

4D

Solu

tions Ltd

.

THE PRODUCTIVITY MISCONCEPTION

Productivity

Features

Happy customers

MaintainabilityPressure

Legend:

Positive relationshipNegative relationship

Increased

Decreased

Stability

Clean code

Testing

Page 11: Tdd & clean code

Quality

Testing

Unit Tests TDD ATDD

TESTING

Testing

Page 12: Tdd & clean code

© A

rnon A

xelro

d, E

4D

Solu

tions Ltd

.

TYPES OF TESTS

Exploratory

Planned

Manual

Automated

White box Black box

Page 13: Tdd & clean code

© A

rnon A

xelro

d, E

4D

Solu

tions Ltd

.

TYPES OF TESTS

UI

View Model

Client Logic

Server Proxy

Service Layer

Business LogicDAL

ORM

DB

End-to-End

Page 14: Tdd & clean code

© A

rnon A

xelro

d, E

4D

Solu

tions Ltd

.

TYPES OF TESTS

UI

View Model

Client Logic

Server Proxy

Service Layer

Business LogicDAL

ORM

DB

Usability, UX

Page 15: Tdd & clean code

© A

rnon A

xelro

d, E

4D

Solu

tions Ltd

.

TYPES OF TESTS

UI

View Model

Client Logic

Server Proxy

Service Layer

Business LogicDAL

ORM

DB

Integration

Page 16: Tdd & clean code

© A

rnon A

xelro

d, E

4D

Solu

tions Ltd

.

TYPES OF TESTS

UI

View Model

Client Logic

Server Proxy

Service Layer

Business LogicDAL

ORM

DB

Functional

Mock

Mock

Mock

Page 17: Tdd & clean code

© A

rnon A

xelro

d, E

4D

Solu

tions Ltd

.

TYPES OF TESTS

UI

View Model

Client Logic

Server Proxy

Service Layer

DAL

ORM

DB

Unit tests

Business Logic

Page 18: Tdd & clean code

© A

rnon A

xelro

d, E

4D

Solu

tions Ltd

.

TYPES OF TESTS

Other:

Performance

Load (scalability)

Install (deployment)

Monkey testing

Page 19: Tdd & clean code

Quality

Testing

Unit Tests TDD ATDD

UNIT TESTS BASICS

Unit Testing

Page 20: Tdd & clean code

© A

rnon A

xelro

d, E

4D

Solu

tions Ltd

.

WHAT’S A UNIT TEST?

What’s a unit?

Smallest testable functionality

any testable functionality (BDD)

Automatic

Gray box

Page 21: Tdd & clean code

© A

rnon A

xelro

d, E

4D

Solu

tions Ltd

.

BENEFITS OF UNIT TESTS

Fast

Easy to write and maintain

Code coverage

When fail, provide insight into the problem

Page 22: Tdd & clean code

© A

rnon A

xelro

d, E

4D

Solu

tions Ltd

.

UNIT TESTS SHOULD:

Be Isolated Re-runnable

No side effects Cleanup

Environment agnostic Order doesn’t matter

Verify functionality, not implementation

Be straight-forward No conditionals, loops etc

Photo by: tongdang

Page 23: Tdd & clean code

© A

rnon A

xelro

d, E

4D

Solu

tions Ltd

.

UNIT TESTS SHOULD:

Read like a story

Verify only one thing

Have meaningful names

Be fast!

Page 24: Tdd & clean code

© A

rnon A

xelro

d, E

4D

Solu

tions Ltd

.DOUBLES (MOCKS)

Order class+Items

+bool CheckAvailability (IInventory inventory)

+void Supply (IInventory inventory)

Inventory class

+int GetAvailablePieces (Item item)

+void Reduce(Item item, int count)

- DbConnectionProvider

Page 25: Tdd & clean code

© A

rnon A

xelro

d, E

4D

Solu

tions Ltd

.DOUBLES (MOCKS)

Order class+Items

+bool CheckAvailability (IInventory inventory)

+void Supply (IInventory inventory)

IInventory interface

+int GetAvailablePieces (Item item)

+void Reduce(Item item, int count)

Inventory class (impl)

+int GetAvailablePieces (Item item)

+void Reduce(Item item, int count)

- DbConnectionProvider

InventoryDouble

+int GetAvailablePieces (Item item)

+void Reduce(Item item, int count)

Page 26: Tdd & clean code

© A

rnon A

xelro

d, E

4D

Solu

tions Ltd

.

DOUBLES...

Double

Fake

DummyMockStub

Detour

Page 27: Tdd & clean code

© A

rnon A

xelro

d, E

4D

Solu

tions Ltd

.

UNIT TEST STRUCTURE

Arrange

Act

Assert

Given

When

Then

Page 28: Tdd & clean code

© A

rnon A

xelro

d, E

4D

Solu

tions Ltd

.

TEST SUITE

Suite Initialize

Test Initialize

Test Method 1

Test Cleanup

Suite Cleanup

Test Method 2 Test Method 3

Page 29: Tdd & clean code

Quality

Testing

Unit Tests TDD ATDDTDD

TDD

Page 30: Tdd & clean code

© A

rnon A

xelro

d, E

4D

Solu

tions Ltd

.

WHAT TDD IS ALL ABOUT?

Testing?!

Design!

Page 31: Tdd & clean code

© A

rnon A

xelro

d, E

4D

Solu

tions Ltd

.WHY IT IS IMPORTANT TO TEST FIRST?

Looking at the problem space

vs. the solution (implementation) space

Building decoupled code from base

Psychological effect

Testing the test first!

Page 32: Tdd & clean code

© A

rnon A

xelro

d, E

4D

Solu

tions Ltd

.

TESTING THE TEST FIRST!

What are the inputs? What are the outputs?

[TestMethod]public void TestFibonacciFunction(){ // arrange ... // Act: var result = Fibonacci(...); // Assert: Assert ...}

The test we want to test

Page 33: Tdd & clean code

© A

rnon A

xelro

d, E

4D

Solu

tions Ltd

.

TESTING THE TEST FIRST!

public bool TestFibonacciFunction(Func<…> fibonacci){ // arrange ... // Act: var result = fibonacci(...); // Assert: return ...}

The test we want to test

public void TestTheTest(){ var invalidImpl = … var goodImpl = Fibonacci; Assert.IsFalse(TestFibonacciFunction(invalidImpl)); Assert.IsTrue(TestFibonacciFunction(goodImpl));}

Testing the test

Page 34: Tdd & clean code

© A

rnon A

xelro

d, E

4D

Solu

tions Ltd

.

THE WAY TDD WORKS:

Write a failing test

Write the code to

make this test pass

Refactor

Page 35: Tdd & clean code

© A

rnon A

xelro

d, E

4D

Solu

tions Ltd

.

Write a test

New TestOld Test

s

Write production code

All Tests

Refactor

All Tests

Page 36: Tdd & clean code

© A

rnon A

xelro

d, E

4D

Solu

tions Ltd

.

TDD AND REFACTORING

Photo by: Chaiwat

Page 37: Tdd & clean code

© A

rnon A

xelro

d, E

4D

Solu

tions Ltd

.

TDD AND REFACTORING

public class SomeClass {    public SomeClass()    {        //...    }    public void Foo(int someParam)    {        // use someParam...    }}

Initial state

Page 38: Tdd & clean code

© A

rnon A

xelro

d, E

4D

Solu

tions Ltd

.

TDD AND REFACTORING

public class SomeClass { private int _someParam;    public SomeClass(int someParam)    {        //... _someParam = someParam;    }    public void Foo()    {        // use _someParam...    }}

Final (desired)

state

Page 39: Tdd & clean code

© A

rnon A

xelro

d, E

4D

Solu

tions Ltd

.

TDD AND REFACTORING

public class SomeClass {    public SomeClass()    {        //...    }    public void Foo(int someParam)    {        // use someParam...    }}

Initial state

Page 40: Tdd & clean code

© A

rnon A

xelro

d, E

4D

Solu

tions Ltd

.

TDD AND REFACTORING

public class SomeClass{    public SomeClass()    {        //...    }    private int _someParam;    public SomeClass(int someParam) : this()    {        _someParam = someParam;    }    public void Foo(int someParam)    {        //use someParam...    }}

Step1 – Add constructor

overload

Page 41: Tdd & clean code

© A

rnon A

xelro

d, E

4D

Solu

tions Ltd

.

TDD AND REFACTORING

public class SomeClass{    private int _someParam;    public SomeClass(int someParam)    {        //...        _someParam = someParam;    }    public void Foo(int someParam)    {        //use someParam...    }}

Step2 – Remove old constructor

overload

Page 42: Tdd & clean code

© A

rnon A

xelro

d, E

4D

Solu

tions Ltd

.

TDD AND REFACTORING

public class SomeClass{    private int _someParam;    public SomeClass(int someParam)    {        //...        _someParam = someParam;    }    public void Foo(int someParam)    {        //use someParam...    }    public void Foo()    {        //use _someParam...    }}

Step3 – Add new

overload of Foo

Page 43: Tdd & clean code

© A

rnon A

xelro

d, E

4D

Solu

tions Ltd

.

TDD AND REFACTORING

public class SomeClass{    private int _someParam;    public SomeClass(int someParam)    {        //...        _someParam = someParam;    }    public void Foo()    {        //use _someParam...    }}

Step4 (final) – Remove

old overload of Foo

Page 44: Tdd & clean code

© A

rnon A

xelro

d, E

4D

Solu

tions Ltd

.

TDD AND LEGACY CODE

Page 45: Tdd & clean code

TDD AND LEGACY CODE

Legacy code TDD (loosely-coupled)

Page 46: Tdd & clean code

© A

rnon A

xelro

d, E

4D

Solu

tions Ltd

.

LIMITS OF TDD

UI

Multi-threading

Integration with peripheral systems, including: Hardware (I/O) Operating System Database 3rd party applications and components

Page 47: Tdd & clean code

Quality

Testing

Unit Tests TDD ATDD

ATDD

ATDD

Page 48: Tdd & clean code

© A

rnon A

xelro

d, E

4D

Solu

tions Ltd

.

ATDD

Page 49: Tdd & clean code

© A

rnon A

xelro

d, E

4D

Solu

tions Ltd

.

ATDD

Stands for:

Accepance-Test Driven Development

Page 50: Tdd & clean code

© A

rnon A

xelro

d, E

4D

Solu

tions Ltd

.

ATDD

TDD is about writing the CODE RIGHT

ATDD is about writing the RIGHT CODE

Page 51: Tdd & clean code

© A

rnon A

xelro

d, E

4D

Solu

tions Ltd

.

ATDD

ATDD is about Communication

Mainly between Product, Dev and Test

Page 52: Tdd & clean code

© A

rnon A

xelro

d, E

4D

Solu

tions Ltd

.

ATDD IS AKA:

Agile Acceptance Testing Specification by Example Example Driven Development Executable Specifications ~= BDD (Behavio Driven Development)

Page 53: Tdd & clean code

© A

rnon A

xelro

d, E

4D

Solu

tions Ltd

.PROBLEMS WITH WATERFALL SPECIFICATION

Photo by: Michal Marcol

Page 54: Tdd & clean code

© A

rnon A

xelro

d, E

4D

Solu

tions Ltd

.PROBLEMS WITH AGILE SPECIFICATION

Page 55: Tdd & clean code

© A

rnon A

xelro

d, E

4D

Solu

tions Ltd

.

HOW ATDD WORKS?

Specification by Examples

Scenarios Acceptance tests

Page 56: Tdd & clean code

Quality

Testing

Unit Tests TDD ATDD

FITNESSE DEMO

ATDD

Page 57: Tdd & clean code

© A

rnon A

xelro

d, E

4D

Solu

tions Ltd

.BENEFITS OF ATDD FOR THE BUSINESS ANALYST

Developers will actually read the specifications that you write

You will be sure that developers and testers understand the specifications correctly

You will be sure that they do not skip parts of the specification

You can track development progress easily

You can easily identify conflicts in business rules and requirements caused by later change requests

You’ll save time on acceptance and smoke testing

Source: Gojko Adzic – Bridging the Communication Gap

Page 58: Tdd & clean code

© A

rnon A

xelro

d, E

4D

Solu

tions Ltd

.BENEFITS OF ATDD FOR THE DEVELOPER

Most functional gaps and inconsistencies in the requirements and specifications will be flushed out before the development starts

You will be sure that business analysts actually understand special cases that you want to discuss with them

You will have automated tests as targets to help you focus the development.

It will be easier to share, hand over and take over code

You’ll have a safety net for refactoring, making your code cleaner [Arnon A.]

Source: Gojko Adzic – Bridging the Communication Gap

Page 59: Tdd & clean code

© A

rnon A

xelro

d, E

4D

Solu

tions Ltd

.

BENEFITS OF ATDD FOR THE TESTER

You can influence the development process and stop developers from making the same mistakes over and over

You will have a much better understanding of the domain

You’ll delegate a lot of dull work to developers, who will collaborate with you on automating the verifications

Source: Gojko Adzic – Bridging the Communication Gap

Page 60: Tdd & clean code

© A

rnon A

xelro

d, E

4D

Solu

tions Ltd

.

BENEFITS OF ATDD FOR THE TESTER

You can build in quality from the start by raising concerns about possible problems before the development starts

You’ll be able to verify business rules with a touch of a button

You will be able to build better relationships with developers and business people and get their respect

You’ll spend less time doing dull work, and more time exploring “real” bugs and testing non-functional aspects (e.g. performance, usability, etc). [Arnon A.]

Source: Gojko Adzic – Bridging the Communication Gap

Page 61: Tdd & clean code

Quality

Testing

Unit Tests TDD ATDD

QUESTIONS?

Page 62: Tdd & clean code

Quality

Testing

Unit Tests TDD ATDD

THANK YOU!