testing without waste - automatic testing

26
Testing without waste Automatic testing Kimmo Brunfeldt 02/2016 @kimmobrunfeldt

Upload: futurice

Post on 10-Feb-2017

387 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Testing Without Waste - Automatic Testing

Testing without wasteAutomatic testing

Kimmo Brunfeldt 02/2016 @kimmobrunfeldt

Page 2: Testing Without Waste - Automatic Testing

// WHY AUTOMATE?

Page 3: Testing Without Waste - Automatic Testing

WHY

- Enables fast movement - Catch embarrassing

regression bugs - Refactor with confidence - Provides examples of

how to use your code

Page 4: Testing Without Waste - Automatic Testing

REALITY

- Budgets are tight - There's no time - "Do we need tests?" - Tests are not a priority - Writing tests = no value

for customer now

(yes)

Page 5: Testing Without Waste - Automatic Testing

// MY RULES OF THUMB

Page 6: Testing Without Waste - Automatic Testing

Test against the most stable API

Tests should help refactoring

Page 7: Testing Without Waste - Automatic Testing

REFACTORING

- You are able to ship features faster, without worrying about breaking previous ones

- Goal: even you refactor most internals of your app, you shouldn't have to change test code

- Do NOT tie your tests too much to internal knowledge. As an example, HTTP API is most likely the least changing API

- If you find yourself maintaining tests every time you change code, that's wrong!

Page 8: Testing Without Waste - Automatic Testing

Even if you don't test every case now, you should setup tests so that writing cases is easy in the future.

Don't leave test setup for later

Page 9: Testing Without Waste - Automatic Testing

NO

YEShttps://medium.com/@kimmobrunfeldt/the-real-benefit-of-tdd-d2fdc9fc4ddf

Page 10: Testing Without Waste - Automatic Testing

It means extra maintenance because you "fake" another API

* Unless that's really, REALLY the only viable option. Are you sure it is?

Don't fake anything*

Page 11: Testing Without Waste - Automatic Testing

NO FAKING

- Do real HTTP requests against your backend. Setup the server running locally and test that.

- Have a real Postgres with test database, have a real S3 test bucket etc.

- Try to replicate the real scenario as close as possible

- You don't want to maintain compability to any external API. You already have to maintain the compability with your own app's API - That is enough work and trouble

Page 12: Testing Without Waste - Automatic Testing

I have found them to be the good in practice.

Focus to integration tests

Page 13: Testing Without Waste - Automatic Testing

INTEGRATION

- Integration tests usually don't know that much about app internals so they are easy to maintain even your internals change. I have done very big refactors with confidence thanks to integration tests

- They may take longer to run, but ... usually they are still more pragmatic than e.g. unit tests

Page 14: Testing Without Waste - Automatic Testing

It will reveal bugs like missing dependencies in app configuration.

Have a CI server also run tests

Page 15: Testing Without Waste - Automatic Testing

CONTINUOUS INTEGRATION

- Good to run tests in a different environment than yours

- It will do a "fresh install", which might reveal some details

- Travis CI and Circle CI are good tools

Page 16: Testing Without Waste - Automatic Testing

You know they'll help you later. They will save your ass when you need to suddenly refactor a lot to do a new feature.

No excuses, take this medicine

Page 17: Testing Without Waste - Automatic Testing

NO EXCUSES

- Quite a lot of people I've spoken with, knows the value of testing, but just somehow they convince themselves not to write them

- Initial setup takes time, but it will save time later - trust yourself.

- If you have bad experience of maintaining tens of breaking tests, try to test from higher layer to minimize maintenance

Page 18: Testing Without Waste - Automatic Testing

// APPLY THESE TIPS

Page 19: Testing Without Waste - Automatic Testing

FRAMEWORK INTERNALS

HTTP LAYER

CORE LOGIC

DATABASE

- Unwraps HTTP request - Does basic validation. "Does it look

correct"

- Doesn’t know HTTP, simple input and output

- Handles database transactions - Minimal input parameters and outputs raw

data structures. Does not reveal storage details to caller.

- Minimize the dependency to a specific database if possible

SPLIT RESPONSIBILITIES

Page 20: Testing Without Waste - Automatic Testing

FRAMEWORK INTERNALS

HTTP LAYER

CORE LOGIC

DATABASE

WHAT TO TEST?

Need to mock HTTP request in tests, so don't

Easy to test. You need db, but no mocking, great .. BUT

Page 21: Testing Without Waste - Automatic Testing

?WHAT TO TEST?

FRAMEWORK INTERNALS

HTTP LAYER

CORE LOGIC

DATABASE

The most maintainable tests know nothing about your application layers.

Page 22: Testing Without Waste - Automatic Testing

?TEST THIS

INPUT X

OUTPUT Y

Was the output correct?

Page 23: Testing Without Waste - Automatic Testing

?IN PRACTICE

GET /posts/1

Was the title and content correct?

{ "title": "First", "content": "..." }

Page 24: Testing Without Waste - Automatic Testing

IF YOU NEED MORE

FRAMEWORK INTERNALS

HTTP LAYER

CORE LOGIC

DATABASE

If you have e.g. bank transactions in your business logic, definitely test them. The good layer would be "core logic" = business logic layer. Even though they will tie the tests to internal knowledge

Page 25: Testing Without Waste - Automatic Testing

// I HAVE SOME BLOG POSTS

https://medium.com/@kimmobrunfeldt/the-real-benefit-of-tdd-d2fdc9fc4ddf#.o62kgijna

https://medium.com/@kimmobrunfeldt/testing-for-startups-10732eb253e8#.o9jy8igti

https://medium.com/@kimmobrunfeldt/introduction-to-sauce-labs-3ad082fc7dd5#.u041fufej

Page 26: Testing Without Waste - Automatic Testing

// THANKS

✅ ➡ 😎