1 of 25

Workshop:�Test Driven Development

Lorenzo Spinelli

Tiziano Pessa

2 of 25

What are we going to do?

Brief intro: TDD - What, How & Why?

Hands on session - let’s code together

3 of 25

TDD_

What

How

Why?

4 of 25

image credit: Adobe stock

Production failures

5 of 25

Study of 198 production failures

(Paper)

https://www.usenix.org/system/files/conference/osdi14/osdi14-paper-yuan.pdf

6 of 25

58%

Easily avoided with adequate test coverage

7 of 25

We need a powerful error detection protocol

8 of 25

it’s about ensuring that when we push code into production

we do so with the confidence that it won’t break anything.

It's not about admitting that we’re not good developers;

9 of 25

Testing

10 of 25

Testing (verb)

Evaluating a system, product, or software to assess its functionality, performance, reliability, and quality

by executing it under specific conditions to identify defects and ensure it meets requirements

11 of 25

The "No time for testing" death spiral

Stress

Less test

Automated tests

12 of 25

The Test Pyramid

https://martinfowler.com/articles/practical-test-pyramid.html

BDD

TDD

13 of 25

What is TDD?

Writing tests before writing the code

Writing code only to pass the tests

TDD is about expressing intent

14 of 25

The TDD process

1. Write a Test

2. Run the Test

Repeat

Red phase

Green phase

Gray phase

3. Write the Code

4. Run the Test Again

5. Refactor

15 of 25

Two simple rules

  1. Write new code only if an automated test has failed
  2. Eliminate duplication

16 of 25

TDD phases

Write a little test that doesn't work

Red phase

Green phase

Gray phase

Make the test work quickly

Eliminate duplication

17 of 25

Key benefits of TDD

  • High test coverage
  • Meaningful tests
  • Early problem detection
  • Less debugging
  • Improved design
  • Easier to maintain

18 of 25

The attributes of good code

  • Modular
  • Loosely coupled
  • High cohesion
  • Separation of concerns
  • Information hiding

19 of 25

The attributes of good code

The attributes of testable code

  • Modular
  • Loosely coupled
  • High cohesion
  • Separation of concerns
  • Information hiding

20 of 25

TDD: the talent amplifier

Testing is not a magic fix!

It won't instantly make you the best programmer in the world.

Bad programmer

Less bad programmer

Good programmer

Better programmer

Great programmer

Greater programmer

21 of 25

Any questions so far?

22 of 25

TDD_

Let’s code!

23 of 25

Goal

Supplier

Quantity

Price

Total

CodeCorp

1000

25 USD

25000 USD

BugBusters

400

150 CHF

60000 CHF

Total

65000 USD

From

To

Rate

CHF

USD

1.5

24 of 25

https://github.com/firenzedev/tdd-workshop

25 of 25

We want to drive the development with automated tests

  1. Quickly add a test.
  2. Run all tests and see the new one fail.
  3. Make a little change.
  4. Run all tests and see them all succeed.
  5. Refactor to remove duplication.