90
@giorgionatili #mobiletea 1 The Little Shop of TDD Horrors a story from Giorgio Natili (the mobile & tdd dude)

Giorgio Natili - The Little Shop of TDD Horrors

Embed Size (px)

Citation preview

Page 1: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 1

The Little Shop of TDD Horrors

a story from Giorgio Natili (the mobile & tdd dude)

Page 2: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 2

let licensing = CC + WTFPL

Page 3: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 3

A few words about me…

Page 4: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 4

Lead Mobile Engineer (McGraw Hill Education)

@giorgionatili

Page 5: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 5

Mobile Tea

Page 6: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 6

www.meetup.com/pro/mobiletea

Page 7: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 7

!Setup and Teardown

Tests clarity and readability

Mocking data

Contract tests

Behaviors driven development

End to end testing

The goals of testing

What we’ll talk about

Page 8: Giorgio Natili - The Little Shop of TDD Horrors

“We are going to cycle in good and bad practices highlighting potential solutions…”

How we’ll talk about this

Page 9: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 9

Everything Started with a Red Sign…

Page 10: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 10

And from a bunch of notes

Page 11: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 11

Avoid the “Red” in ProcessTest-Driven Development is not the one true programming methodology, testing is a tool for helping you!!!

Page 12: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 12

What’s TDD

Page 13: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 13

Test Driven Development

Page 14: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 14

Development life cycleIt’s all about writing enough code to satisfy specific conditions and a continuous refactoring of the code

Page 15: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 15

Analysis toolDecouple requirements in independent, negotiable, valuable, estimable, small testable pieces of code

Page 16: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 16

With TDD you are not lost anymore!

Page 17: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 17

Set up and tear down

Page 18: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 18

Easy setup and teardownUnderstandable setups keep the test clean and easily comprehensible for everybody

Page 19: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 19

beforeEach(function() {

provider.init(["course:read:*", "group:read:*", "settings:read:*", "settings:update:*", "activity:read:*", "log:*:*", "node:create:*", "settings:create:*", "system:update:*", "system:read:*", "system:delete:*", "system:create:*", "unit:create:*", "unit:read:*", "node:update:*", "node:read:*", "node:delete:*", "splash:read:*", "path:read:*", "master:update:*", "snapshot:read:*", "lti:grade:*", "master:read:*", "snapshot:update:*", "history:read:*", "lti:appLaunch:*", "lti:contentItemReturn:*", "appAction:annotationBundle.js:*", "appAction:apps:*", "appAction:annotation.js:*", "openId:read:*", "app:read:*", "activityOutcome:activitySubmission:*", "search:read:*", "userOrgProfile:read:*", "lti:contentItemSelectionReturn:*", "history:create:*", "eula:read:*", "lti:editActivity:*", "userAppCategoryPref:update:*", "platformArtifacts:read:*", "userAppCategoryPref:read:*", "userAppCategoryPref:delete:*", "userAppCategoryPref:create:*", "appCategory:read:*", "appActivity:update:3,4,16,48,49", "appActivity:findAddible:16,48,49", "nextbook:read:*", "appActivity:read:*", "lti:addActivity:*", "activityOutcome:create:*", "activityOutcome:read:*", "activityOutcome:update:*" ]);

});

Overcomplicated setup is a symptom of poor (test(s)) organization!

Page 20: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 20

Eventually decouple setups

Just in case you really need complicated setups decouple them from tests and include them in the setup…

Page 21: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 21

Essentials Tests

Page 22: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 22

Test the fundamentals

If you are not 100% test covered you are not a bad person! Code coverage is just about the quantity, not the quality!

Page 23: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 23

describe( 'AppCtrl', function() { describe( 'isCurrentUrl', function() { var AppCtrl, $location, $scope;

beforeEach( module( 'ngBoilerplate' ) );

beforeEach( inject( function( $controller, _$location_, $rootScope ) { $location = _$location_; $scope = $rootScope.$new(); AppCtrl = $controller( 'AppCtrl', { $location: $location, $scope: $scope }); }));

it( 'should pass a dummy test', inject( function() { expect( AppCtrl ).toBeTruthy(); })); }); });

Don’t take advantage from this!

Page 24: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 24

Test, design and code quality

Page 25: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 25

Big up front design is not TDD

It’s not based on cycles and it’s not how you would design a component…

Page 26: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 26

Big projects should still follow best practices…

Page 27: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 27

Tests are code, treat them well

Write small, readable and decoupled tests. Tests are part of your code base, don’t write garbage code!

Page 28: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 28

Treat your tests as a welcome guests

Page 29: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 29

Refactor on green

Don’t refactor code or tests until the tests are not green…

Page 30: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 30

Iterative Testing

Writing all the tests first is a mistake because you cannot have a complete understanding of a feature from a user story

Page 31: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 31

Existing Code

Don’t write tests unless you have to modify the original code and change the behavior

Page 32: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 32

Readability

Page 33: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 33

Readable

Never write tests that nobody wants or is able to read, you’ll stop to read them too

Page 34: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 34

@implementation CardTests

- (void)testFlag { [self login]; NSManagedObjectContext *context = [CoreDataManager sharedManager].operationContext; Section *testSection = [Section sectionForUpsertWithSectionId:@(1) forUserId:self.testUserId inContext:context];

// Completed activities should not show a due soon indicator even when due soon. Activity *activityNotStarted = [Activity activityForUpsertWithActivityId:@(2) forSection:testSection inContext:context]; activityNotStarted.progressStatus = ActivityProgressStatusNotYetStarted; activityNotStarted.due = [NSDate distantFuture]; Card *cardForActivityNotStarted = [Card cardForUpsertWithActivityDue:activityNotStarted]; XCTAssertEqualObjects(SCLocStr(@"ACTIVITY_FLAG_NOT_STARTED"), cardForActivityNotStarted.flag);

// Repeated for 6 different scenarios…

@end

Boring and not useful

Page 35: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 35

Do you like to read like this?

Page 36: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 36

Time box writing tests

You should be driven away from writing long tests and large amounts of code to satisfy them

Page 37: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 37

Privacy

Page 38: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 38

Respect the privacy

Don’t expose methods just because you have to test it: well-designed public methods should use them in such a way they don’t need be directly tested

Page 39: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 39

Reflection + methods swizzling

Don’t expose private methods - even temporarily - you are terrorizing your own design

Page 40: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 40

Edge Cases

Page 41: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 41

Edge Cases and Code

Don’t start with edge cases (empty string, null parameter) or error cases, this has to happen in code!

Page 42: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 42

Tests the Behavior

A test must validate a behavior, not an input otherwise they don’t deliver any business value, nor do validate the product owner’s assumptions

Page 43: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 43

Repetitive Cases

Page 44: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 44

Automate repetitive cases

Use generative tests framework to mock repetitive cases in your tests

Page 45: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 45

describe('pow', function() { it('works for simple cases', function() { assert.equal(pow(2, 1), 2); assert.equal(pow(2, 2), 4); assert.equal(pow(2, 5), 32); assert.equal(pow(2, 8), 256); assert.equal(pow(3, 2), 9); assert.equal(pow(10, 4), 10000); assert.equal(pow(1, 99), 1); }); });

From this mess…

Page 46: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 46

forAll([gentest.types.int, gentest.types.int],

'pow works like builtin',

function(base, exponent) { var builtinPow = Math.pow(base, exponent); var ourPow = pow(base, exponent);

return (builtinPow === ourPow || relativeErrorWithin(0.000000001, builtinPow, ourPow)); });

To a better code!

Page 47: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 47

You feel better!

Page 48: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 48

Clarity

Page 49: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 49

Clarity

The clarity of expectations and assertions is the key for effective TDD

Page 50: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 50

Naming

Don’t be worried about the length of the names, a right name is the key to be understood by the others

Page 51: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 51

Poor descriptions

Tests descriptions are one of the best sources of documentation, never ignore the importance of providing a good description

Page 52: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 52

Respect laziness

Never force a stakeholder to read the code contained in a test to understand its purpose

Page 53: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 53

Arrange, Act, Assert (AAA)

Separate what is going to be tested from the setups and keep the verification steps clearly identified

Page 54: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 54

Conditional Behaviors

Page 55: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 55

Avoid conditional behaviors

If you start to add conditionals to determine if your code is executed by the tests suites, it means that you are in danger

Page 56: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 56

Ambiguous behaviors are dangerous!

Page 57: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 57

“TDD is not about preventing bugs, it’s about proving the

exactness of behaviors”

Page 58: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 58

There are unpredictable conditions!

Page 59: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 59

but you can still mitigate them…

Page 60: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 60

Network Responses and API

Page 61: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 61

Network Responses

Applications rely on external data sources, it’s important to have clear expectations about external APIs responses

Page 62: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 62

Performance

Tests are code: Never ignore performance issues and bottlenecks

Page 63: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 63

Mock Data

Mock only the data you are testing. A mock should be small and understandable

Page 64: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 64

Contract Tests

Create tests in a separate environment that checks the sanity of the external API you rely on

Page 65: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 65

'use strict'; var supertest = require('supertest-as-promised'); var request = supertest('https://ngcourse.herokuapp.com'); var assert = require('chai').assert;

describe('Backend API', function() { it ('should return the list of users', function() { return request.get('/api/v1/users') .expect(200); }); });

Contract Tests in Practice

Page 66: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 66

Behaviors

Page 67: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 67

Behaviors

Focus on testing software behaviors rather then single units of code

Page 68: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 68

Organize behaviors

Group tests and behaviors that are tied to specific, testable functionality of your software

Page 69: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 69

Connect behaviors

Create a connection between behaviors and scenarios with a format like given/then/when

Page 70: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 70

Divide tests logically

Never violate the Single Responsibility Principle neither when writing tests, keep them ordered and in a good shape is the key to get the more value from them…

Page 71: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 71

Measure complexity

If you are not able to organize your tests in such a way it means that you are dealing with confusing (and poor!) requirements

Page 72: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 72

Scenarios -> Use Cases -> Behaviors

This connection is the key to defining tests that clearly describe a feature and its acceptance criteria

Page 73: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 73

End 2 End Testing

Page 74: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 74

Test for pixels

Don’t use e2e tests for testing layout implementations, automate (eventually) screenshots and then examine them

Page 75: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 75

Counting elements

Don’t use e2e testing to count UI elements, use them to describe how the UI *should change* with a specific behavior

Page 76: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 76

Writing E2E tests

Never wait too much, start to write end user tests as soon as the requirements are ready, the lack of automation can bring to not reliable outputs

Page 77: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 77

The Goals of Testing

Page 78: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 78

Do you ever asked to yourself?

Page 79: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 79

Profitable

Tests should always support the end goal of making profit from a software

Page 80: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 80

Maintainability

Tests are a means to increase the maintainability of a software

Page 81: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 81

Common understanding

Don’t separate developers and testers! Keeping them together allows you to have testers that understand the app internals

Page 82: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 82

Interchangeability

Having pairing working alternatively on code and tests is the key to having different perspectives and to improve interchangeability in your team

Page 83: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 83

Conclusions

Page 84: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 84

Preconditions

If you cannot define preconditions you cannot write tests of good quality, potentially there is a big misunderstanding about requirements

Page 85: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 85

Quality

The quality of a test can be highly opinionated; let’s find a metric that is valuable for your organization

Page 86: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 86

Validate

Write tests to validate the understanding of the behaviors a software should implement not to validate the code

Page 87: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 87

Estimate

Writing tests take time. Count it in your estimates but keep always in mind the time that is then saved during the maintaining phase

Page 88: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 88

Questions

Page 89: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 89

Special thanks to:

• Codemotion Italia • codeinvaders.net

Page 90: Giorgio Natili - The Little Shop of TDD Horrors

@giorgionatili #mobiletea 90

THANK YOU