Testing Without Waste - Automatic Testing
-
Upload
futurice -
Category
Technology
-
view
397 -
download
0
Transcript of Testing Without Waste - Automatic Testing
![Page 1: Testing Without Waste - Automatic Testing](https://reader031.fdocuments.net/reader031/viewer/2022030213/589d50b11a28abd3338b7143/html5/thumbnails/1.jpg)
Testing without wasteAutomatic testing
Kimmo Brunfeldt 02/2016 @kimmobrunfeldt
![Page 2: Testing Without Waste - Automatic Testing](https://reader031.fdocuments.net/reader031/viewer/2022030213/589d50b11a28abd3338b7143/html5/thumbnails/2.jpg)
// WHY AUTOMATE?
![Page 3: Testing Without Waste - Automatic Testing](https://reader031.fdocuments.net/reader031/viewer/2022030213/589d50b11a28abd3338b7143/html5/thumbnails/3.jpg)
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](https://reader031.fdocuments.net/reader031/viewer/2022030213/589d50b11a28abd3338b7143/html5/thumbnails/4.jpg)
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](https://reader031.fdocuments.net/reader031/viewer/2022030213/589d50b11a28abd3338b7143/html5/thumbnails/5.jpg)
// MY RULES OF THUMB
![Page 6: Testing Without Waste - Automatic Testing](https://reader031.fdocuments.net/reader031/viewer/2022030213/589d50b11a28abd3338b7143/html5/thumbnails/6.jpg)
Test against the most stable API
Tests should help refactoring
![Page 7: Testing Without Waste - Automatic Testing](https://reader031.fdocuments.net/reader031/viewer/2022030213/589d50b11a28abd3338b7143/html5/thumbnails/7.jpg)
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](https://reader031.fdocuments.net/reader031/viewer/2022030213/589d50b11a28abd3338b7143/html5/thumbnails/8.jpg)
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](https://reader031.fdocuments.net/reader031/viewer/2022030213/589d50b11a28abd3338b7143/html5/thumbnails/9.jpg)
NO
YEShttps://medium.com/@kimmobrunfeldt/the-real-benefit-of-tdd-d2fdc9fc4ddf
![Page 10: Testing Without Waste - Automatic Testing](https://reader031.fdocuments.net/reader031/viewer/2022030213/589d50b11a28abd3338b7143/html5/thumbnails/10.jpg)
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](https://reader031.fdocuments.net/reader031/viewer/2022030213/589d50b11a28abd3338b7143/html5/thumbnails/11.jpg)
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](https://reader031.fdocuments.net/reader031/viewer/2022030213/589d50b11a28abd3338b7143/html5/thumbnails/12.jpg)
I have found them to be the good in practice.
Focus to integration tests
![Page 13: Testing Without Waste - Automatic Testing](https://reader031.fdocuments.net/reader031/viewer/2022030213/589d50b11a28abd3338b7143/html5/thumbnails/13.jpg)
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](https://reader031.fdocuments.net/reader031/viewer/2022030213/589d50b11a28abd3338b7143/html5/thumbnails/14.jpg)
It will reveal bugs like missing dependencies in app configuration.
Have a CI server also run tests
![Page 15: Testing Without Waste - Automatic Testing](https://reader031.fdocuments.net/reader031/viewer/2022030213/589d50b11a28abd3338b7143/html5/thumbnails/15.jpg)
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](https://reader031.fdocuments.net/reader031/viewer/2022030213/589d50b11a28abd3338b7143/html5/thumbnails/16.jpg)
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](https://reader031.fdocuments.net/reader031/viewer/2022030213/589d50b11a28abd3338b7143/html5/thumbnails/17.jpg)
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](https://reader031.fdocuments.net/reader031/viewer/2022030213/589d50b11a28abd3338b7143/html5/thumbnails/18.jpg)
// APPLY THESE TIPS
![Page 19: Testing Without Waste - Automatic Testing](https://reader031.fdocuments.net/reader031/viewer/2022030213/589d50b11a28abd3338b7143/html5/thumbnails/19.jpg)
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](https://reader031.fdocuments.net/reader031/viewer/2022030213/589d50b11a28abd3338b7143/html5/thumbnails/20.jpg)
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](https://reader031.fdocuments.net/reader031/viewer/2022030213/589d50b11a28abd3338b7143/html5/thumbnails/21.jpg)
?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](https://reader031.fdocuments.net/reader031/viewer/2022030213/589d50b11a28abd3338b7143/html5/thumbnails/22.jpg)
?TEST THIS
INPUT X
OUTPUT Y
Was the output correct?
![Page 23: Testing Without Waste - Automatic Testing](https://reader031.fdocuments.net/reader031/viewer/2022030213/589d50b11a28abd3338b7143/html5/thumbnails/23.jpg)
?IN PRACTICE
GET /posts/1
Was the title and content correct?
{ "title": "First", "content": "..." }
![Page 24: Testing Without Waste - Automatic Testing](https://reader031.fdocuments.net/reader031/viewer/2022030213/589d50b11a28abd3338b7143/html5/thumbnails/24.jpg)
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](https://reader031.fdocuments.net/reader031/viewer/2022030213/589d50b11a28abd3338b7143/html5/thumbnails/25.jpg)
// 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](https://reader031.fdocuments.net/reader031/viewer/2022030213/589d50b11a28abd3338b7143/html5/thumbnails/26.jpg)
// THANKS
✅ ➡ 😎