introduction to tdd · things we need to practice tdd why? test infrastructure. we want to write a...

17
Introduction to TDD Dario Campagna

Upload: others

Post on 15-Mar-2021

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to TDD · Things we need to practice TDD Why? Test Infrastructure. We want to write a program that convert integer numbers (in decimal notation) into their Roman numeral

Introduction to TDD

Dario Campagna

Page 2: Introduction to TDD · Things we need to practice TDD Why? Test Infrastructure. We want to write a program that convert integer numbers (in decimal notation) into their Roman numeral

Let’s start with Development

Page 3: Introduction to TDD · Things we need to practice TDD Why? Test Infrastructure. We want to write a program that convert integer numbers (in decimal notation) into their Roman numeral

Software development practice

Clean code that works

Test first

Small steps, fast feedback

Now add Test Driven

Page 4: Introduction to TDD · Things we need to practice TDD Why? Test Infrastructure. We want to write a program that convert integer numbers (in decimal notation) into their Roman numeral

TerrariaClone class from the GitHub repository TerrariaClone.

▪ > 6500 lines of code

▪ > 1300 lines of code for init() method

▪ Deeply nested if and for statements

▪ Many other code smells

Example of Rotten Code

Page 5: Introduction to TDD · Things we need to practice TDD Why? Test Infrastructure. We want to write a program that convert integer numbers (in decimal notation) into their Roman numeral

Rigid

Fragile

Inseparable

Opaque

Rotting code is

Page 6: Introduction to TDD · Things we need to practice TDD Why? Test Infrastructure. We want to write a program that convert integer numbers (in decimal notation) into their Roman numeral

We have no time to clean it

We need to go “faster”

We are afraid of breaking it

Fear prevents us to clean it

Why does it rot?

Page 7: Introduction to TDD · Things we need to practice TDD Why? Test Infrastructure. We want to write a program that convert integer numbers (in decimal notation) into their Roman numeral

The “Faster is Slower” Dynamic

Goal: conform to

original scheduleactual

variance to

original

schedulepressure to

try actions to

conform to

original

schedule

QF

% use of “secret

toolbox”—

hacking to

generate bad

code quickly

exhortations,

bribes, and threats

to developers to

meet schedule

duration and

effort to add

new featuresO

% of clean code

with good design# of defects

O

Oduration between creating

and fixing a defect

duration and correctness

in fixing a defect

short termonly

QF

www.craiglarman.com

www.odd-e.com

Copyright © 2009

C.Larman & B. Vodde

All rights reserved.

Page 8: Introduction to TDD · Things we need to practice TDD Why? Test Infrastructure. We want to write a program that convert integer numbers (in decimal notation) into their Roman numeral

Easy to understand

Easy to evolve

Easy to maintain

Sustains delivery pace

Clean code

Page 9: Introduction to TDD · Things we need to practice TDD Why? Test Infrastructure. We want to write a program that convert integer numbers (in decimal notation) into their Roman numeral

We want to begin learning concretely as quickly as possible

We want to communicate more clearly

We want to search out helpful, concrete feedback

Fear makes you tentative

Fear makes you want to communicate less

Fear makes you shy away from feedback

Knowledge Gap Causes Fear

Page 10: Introduction to TDD · Things we need to practice TDD Why? Test Infrastructure. We want to write a program that convert integer numbers (in decimal notation) into their Roman numeral

Test Driven Development Cycle

Think

Red

Green

Refactor

Find out which test to write next

The test should fail for the right reason

Make the test pass quickly!

How can we make the code better?

Page 11: Introduction to TDD · Things we need to practice TDD Why? Test Infrastructure. We want to write a program that convert integer numbers (in decimal notation) into their Roman numeral

To run the tests, as fast as possible

To build the test suite

To check test status

A way to write tests

Automated build

Test framework

Assertion library

Arrange/Act/Assert

Things we need to practice TDD Why?

Test Infrastructure

Page 12: Introduction to TDD · Things we need to practice TDD Why? Test Infrastructure. We want to write a program that convert integer numbers (in decimal notation) into their Roman numeral

We want to write a program that convert integer numbers (in decimal notation) into their Roman numeral equivalents.

1. What test should we write? 2. Write the test and see it fail. 3. Make the test pass, quickly! 4. Eliminate duplication and improve expressiveness. 5. Back to 1.

Let’s try it!

Page 13: Introduction to TDD · Things we need to practice TDD Why? Test Infrastructure. We want to write a program that convert integer numbers (in decimal notation) into their Roman numeral

Two people working together, at the same computer, solving the same problem.

▪ XP practice for software development ▪ Two roles: driver and navigator ▪ The pair swaps fluidly between the roles ▪ Improve code quality, spread knowledge, training on the job, …

Let’s try Pair Programming too

Driver Navigator

Page 14: Introduction to TDD · Things we need to practice TDD Why? Test Infrastructure. We want to write a program that convert integer numbers (in decimal notation) into their Roman numeral

Should you always practice Test Driven Development?

Page 15: Introduction to TDD · Things we need to practice TDD Why? Test Infrastructure. We want to write a program that convert integer numbers (in decimal notation) into their Roman numeral

Databases

Network communications

File system

Other shared fixtures

Configurations

Isolated and composable

Fast and automated

Behavioral and structure-insensitive

Specific and deterministic

Inspiring and predictive

Should be… Beware of

Tests in TDD

Page 16: Introduction to TDD · Things we need to practice TDD Why? Test Infrastructure. We want to write a program that convert integer numbers (in decimal notation) into their Roman numeral

Test Pyramid

https://martinfowler.com/bliki/TestPyramid.html Copyright © 2012 Martin Fowler

Page 17: Introduction to TDD · Things we need to practice TDD Why? Test Infrastructure. We want to write a program that convert integer numbers (in decimal notation) into their Roman numeral

Test Driven Development by ExampleKent Beck

Extreme Programming ExplainedKent Beck

References

Clean CodeRobert C. Martin