Slide 1

Slide 1 text

The non-unitary in TDD TDD in presence of dependencies Johan Martinsson @johan_alps

Slide 2

Slide 2 text

The problem(s)

Slide 3

Slide 3 text

We don’t know how to test

Slide 4

Slide 4 text

We don’t know what to test

Slide 5

Slide 5 text

It's hard to mock

Slide 6

Slide 6 text

UNIT test don’t make sense

Slide 7

Slide 7 text

Surprises in integration • Rights not con fi gured • Heteregenous format, ex a date fi eld can have ddmm or dd/mm/yyyy • Apis lying about the information • Text recognition cuts things strangely • Performance in splitting a pdf • Rate limitation • Sending emails, is it well formed? Xavi cabrera Unsplash

Slide 8

Slide 8 text

Solutions!

Slide 9

Slide 9 text

No tests

Slide 10

Slide 10 text

Buy the tests

Slide 11

Slide 11 text

High level tests

Slide 12

Slide 12 text

Just write them!

Slide 13

Slide 13 text

Test last • When do they serve? • Who do they serve? • How do know if they’re good? Simon Berger Unsplash

Slide 14

Slide 14 text

My suggestion Start as early as possible

Slide 15

Slide 15 text

Example mapping

Slide 16

Slide 16 text

Start TDD with High level, integrated tests

Slide 17

Slide 17 text

How do we TDD this? What are the boundaries of the tests?

Slide 18

Slide 18 text

Result: Mockist style

Slide 19

Slide 19 text

Result: Classical style

Slide 20

Slide 20 text

In what order?

Slide 21

Slide 21 text

Inside Out

Slide 22

Slide 22 text

Outside In

Slide 23

Slide 23 text

Outside-In Classical TDD

Slide 24

Slide 24 text

Types of tests and qualities

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

The story of a story Find the position of a name in a pdf and if it is found, save it to the db. • Create a new lambda • Con fi gure lambda rights • Validate user rights • Extract data from request • Call external service • Parse result • If found, load existing data in db • Save data to db Etienne Girardet Unsplash

Slide 28

Slide 28 text

Example work fl ow • Write test agains deployed version • Validate function exists • Validate function does not fail • … • Write integrated test against version in IDE • Save hard-coded value to db • Deployed test validates function rights • Write another test • Call external service & parse result • More tests to vary input & expectations • Discover false assumptions • Isolate & refactor tests

Slide 29

Slide 29 text

Now Isolate

Slide 30

Slide 30 text

Architecture hexagonale

Slide 31

Slide 31 text

Dependency Adapter

Slide 32

Slide 32 text

Examples of adapters • Repository • Ex PersonRepository • File read/write wrapper • Message queue • External service wrapper • Ex Pdf extractor, email service …

Slide 33

Slide 33 text

Applicatio n test

Slide 34

Slide 34 text

Adapter Adapte r test

Slide 35

Slide 35 text

func TestExtractData(t *testing.T) { t.Skip("this costs 5 cts for each call, only activate when working on the adapter" ) client, err := newAdobeClient(t ) file, result, err := extract(t, "./Feuille_de_Presence_demembrement.pdf", client ) defer file.Close( ) assert.Nil(t, err ) assert.Len(t, result.Pages, 3 ) } Example expensive test

Slide 36

Slide 36 text

Example not isolating, yet // todo make this test faster (mocking ghostscript? ) func Test3Pages(t *testing.T) { testDir := findTestDir( ) service := createServiceWithFakeClient("analysis-result-2050-sd.json", testDir ) err, result := extractText(t, "2050-sd.pdf", testDir, service ) require.Nil(t, err ) require.NotEmpty(t, result ) }

Slide 37

Slide 37 text

Example switching back and forth //textractClient := realTextractClient( ) textractClient := plaquette.FakeTextractFromJson(baseDir + “analysis-result-2033.json" ) // … the rest of the tes t

Slide 38

Slide 38 text

Run the adapter tests for the simulators Real adapter Simulator Depend 
 ency Interface Test Flag for activation

Slide 39

Slide 39 text

Integrated tests in a CI?

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

Conclusion • Delay isolation • Delay low level tests • In a story • In a product • Separate integrated tests