tdd demystified - testcon · • what is your experience with tdd and atdd? ... test-driven...

61
TDD Demystified Slavoj Písek [email protected] 15 th October 2019

Upload: others

Post on 06-Aug-2020

10 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

TDD Demystified

Slavoj Písek

[email protected]

15th October 2019

Page 2: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

Agenda

10:00 – 10:30 Introduction

10:30 – 11:00 TDD quick intro

11:00 – 11:15 Break

11:15 – 12:45 Exercise

Page 3: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

Agenda

12:45 – 13:45 Lunch

Page 4: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

Agenda

13:45 – 14:15 Test first concept

14:15 – 15:15 TDD in real life

15:15 – 15:30 Break

15:30 – 16:30 TDD in real life continue

16:30 – 17:00 Refactoring

17:00 – 17:30 Conclusion

Page 5: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

Agenda

• Introduction

• TDD quick intro

• Break

• Exercise

Page 6: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

5 | DIEBOLD NIXDORF

• More than 15 years experience in SW development and testing

• Team Leader and Senior Developer at Diebold Nixdorf

• Author more than of two dozens of books on programming and IT

• Translator of some books about programming and photography for various publishing

houses

Slavoj Písek

Page 7: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

6 | DIEBOLD NIXDORF

You

Please introduce yourself and try to answer the following questions:

• What is your name?

• Where are you from?

• How are you?

• What is your experience with TDD and ATDD?

• What is your expectation of this workshop?

Page 8: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

Agenda

• Introduction

• TDD quick intro

• Break

• Exercise

Page 9: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

8 | DIEBOLD NIXDORF

TDD - Quick Introduction

What is TDD?

No DDT No TBC

Page 10: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

9 | DIEBOLD NIXDORF

TDD - Quick Introduction

What is TDD?

Test Driven Development

What is Test Driven Development?

Page 11: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

10 | DIEBOLD NIXDORF

Wiki says…

Test-driven development (TDD) is a software development process that relies

on the repetition of a very short development cycle…

Page 12: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

11 | DIEBOLD NIXDORF

Kent Beck’s TDD by Example

Page 13: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

12 | DIEBOLD NIXDORF

Basic principle

Refactor

Add a test

Run all tests to see

the new one failed!

Write a piece of

codeRun all tests

Page 14: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

13 | DIEBOLD NIXDORF

What an Idea!

Page 15: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

14 | DIEBOLD NIXDORF

Advantages

• Writing tests a developer thinks more about design.

• Less time spent with debugger.

• Tests serve as accurate, always up to date, low level

documentation.

• Test-first concept forces coupling reduction in the code. Less

coupling in the system the better.

Page 16: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

15 | DIEBOLD NIXDORF

Sounds good!

• Where to start?

• Theory is very simple

• Practice is not so obvious

• Let‘s start together with something simple…

Page 17: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

16 | DIEBOLD NIXDORF

Fibonacci sequence

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …

Page 18: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

17 | DIEBOLD NIXDORF

JUnit

• JUnit is a simple framework to write repeatable tests.

• It is an instance of the xUnit architecture for unit testing frameworks.

Page 19: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

18 | DIEBOLD NIXDORF

xUnit test patterns

Page 20: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

19 | DIEBOLD NIXDORF

Practical Example

Page 21: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

20 | DIEBOLD NIXDORF

Retrospective

Page 22: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

Reminder of

TDD basic rules

• Add a test

• See it fail

• Add code to make all tests pass

• Refactor

• Do it again

Page 23: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

22 | DIEBOLD NIXDORF

Reminder of TDD basic rules

Page 24: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

23 | DIEBOLD NIXDORF

People often do this

• Think up design

• Write some tests that verify the design

• Write full implementation

• Test, debug, test, debug, test, debug, …

• Add TODO to refactor later

Page 25: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

24 | DIEBOLD NIXDORF

Exercise

Page 26: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

25 | DIEBOLD NIXDORF

Rules

• Exactly one, simplest failing test

• Least code possible to pass failing test

• Add code only to test methods when duplication is spotted

• Extract non-test methods (extract method)

• New classes only as target for “move method”

• Refactor as required

Page 27: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

26 | DIEBOLD NIXDORF

Practice

Page 28: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

27 | DIEBOLD NIXDORF

Lunch

Page 29: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

28 | DIEBOLD NIXDORF

Part II

Page 30: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

Agenda

• Test first concept

• TDD in real life

• Break

• TDD in real life continue

• Refactoring

• Conclusion

Page 31: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

30 | DIEBOLD NIXDORF

Practice

Page 32: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

Agenda

• Test first concept

• TDD in real life

• Break

• TDD in real life continue

• Refactoring

• Conclusion

Page 33: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

32 | DIEBOLD NIXDORF

Unit testing

• Testing of system units

• Unit tests have to be independent

• Unit tests are automated

• There are frameworks for creating and maintaining Unit tests

Page 34: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

33 | DIEBOLD NIXDORF

What is an unit test, anyway

• Unit tests are supposed to be small.

• They have to be fast.

• They test a method or the interaction of a couple of methods.

• They are written by developers.

Page 35: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

34 | DIEBOLD NIXDORF

Test is not a unit test, when

• it talks to a database,

• it sends data over the network,

• it touches a file system,

• cannot be run concurrently with other unit tests,

• it needs some special configuration or initial steps.

Page 36: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

35 | DIEBOLD NIXDORF

TDD in real life

No man is an island, entire of itself...any man's death diminishes me, because I

am involved in mankind; and therefore never send to know for whom the bell tolls;

it tolls for thee.

-- John Donne

Page 37: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

36 | DIEBOLD NIXDORF

File Lottery Example

• Write an application that iterates through content of

given folder and returns child folders in RANDOM

order.

• When the application returns all available

subfolders, it shall start over.

• If the argument is not a folder, but a file, its name

shall be returned.

Page 38: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

37 | DIEBOLD NIXDORF

File Lottery test

In next 10 minutes write as much tests as possible

Page 39: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

38 | DIEBOLD NIXDORF

Objects double

Page 40: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

39 | DIEBOLD NIXDORF

Dependency injection

• Control Inversion

• Instead of configuring dependency, class uses configured

instance of dependency of outside.

Page 41: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

40 | DIEBOLD NIXDORF

Meszaros taxanomy

• Dummy objects are passed around but never actually used.

• Fake objects have working implementations.

• Stubs provide canned responses'.

• Mocks are pre-programmed with expectations which form a specification of the calls they are

expected to receive.

Page 42: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

41 | DIEBOLD NIXDORF

Solving File Lottery tests

• Create fake objects,

• inject them to the FileLottery class,

• configure fakes,

• perform tests.

Page 43: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

42 | DIEBOLD NIXDORF

Credits

• Previous example is based on programming kata by Zsolt Fabók

• http://zsoltfabok.com/blog/2010/07/file-lottery-kata/

Page 44: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

Agenda

• Test first concept

• TDD in real life

• Break

• TDD in real life continue

• Refactoring

• Conclusion

Page 45: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

Agenda

• Test first concept

• TDD in real life

• Break

• TDD in real life continue

• Refactoring

• Conclusion

Page 46: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

Agenda

• Test first concept

• TDD in real life

• Break

• TDD in real life continue

• Refactoring

• Conclusion

Page 47: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

46 | DIEBOLD NIXDORF

Code Refactoring

Page 48: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

47 | DIEBOLD NIXDORF

Refactoring

Code refactoring is the process of restructuring existing computer code without changing its

external behavior.

Page 49: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

48 | DIEBOLD NIXDORF

Why do you need refactoring

Page 50: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

49 | DIEBOLD NIXDORF

Refactoring is a good servant…

Page 51: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

50 | DIEBOLD NIXDORF

Refactor techniques

Techniques to add more abstraction:

• Encapsulate Field

• Generalize Type

• Replace conditional with polymorphism

Page 52: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

51 | DIEBOLD NIXDORF

Refactor techniques

Techniques for breaking code apart into more logical pieces

• Componentization

• Extract Class

• Extract Method

Page 53: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

52 | DIEBOLD NIXDORF

Refactor techniques

Techniques for improving names and location of code

• Move Method or Move Field

• Rename Method or Rename Field

• Pull Up

• Pull Down

Page 54: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

53 | DIEBOLD NIXDORF

Refactoring by Martin Fowler

Page 55: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

54 | DIEBOLD NIXDORF

Signs of smell in the code

• Lack of tests

• Name not expressing intent and not from domain

• Unnecessary if and else

• Duplication of constant

• A Method does more than one thing

• Primitive obsession

• Too long methods (> 6 lines)

• Too many parameters (> 3)

Page 56: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

55 | DIEBOLD NIXDORF

Signs of smell in the test

• Not unitary

• Setup too complex

• Unclear Act

• More than one assert

• No assert

• Too many paths

Page 57: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

56 | DIEBOLD NIXDORF

Practice

Page 58: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

Agenda

• Test first concept

• TDD in real life

• Break

• TDD in real life continue

• Refactoring

• Conclusion

Page 59: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

58 | DIEBOLD NIXDORF

Retrospective

• What did you learn today?

• What surprised you today?

• What will you do differently in the future?

Page 60: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

59 | DIEBOLD NIXDORF

Feedback

Page 61: TDD Demystified - TestCon · • What is your experience with TDD and ATDD? ... Test-driven development (TDD) ... Unit testing • Testing of system units • Unit tests have to be

60 | DIEBOLD NIXDORF

Questions