SlideShare a Scribd company logo
1 of 47
Download to read offline
TDD done “right” -
tests immutable to
refactor
Grzesiek Miejski
Quick survey
Little about me
● Coding microservices for ~4 years
● Different languages and technologies:
○ now: Go, python
○ previously: Kotlin, Java
○ MongoDB, SQL, ElasticSearch, Cassandra, Kafka, RabbitMQ, blabla
Are there not enough TDD materials
already?
My previous problems with tests
● changing business logic made me change tests a lot
● refactoring made me change tests
● applied “real” TDD only to the simplest code, with structure predicted upfront
Is it worth trying then?
Quick survey 2
But in what context?
When my advices are applicable for me?
● everywhere where you want you software to last and be developed
● when building microservices, as this is what I do right now
● but would try love to try those practices in monolith written from start!
Plan for presentation
1. About architecture
2. Unit and Facade
3. Unit and Integration tests
4. Unit tests in BDD style done right
5. Project immutable to refactor - example
6. DO’s and DONT’s
7. TDD goes live!
Example project - movie renting site
Simplified requirements:
● view available movies
● rent movie by user
● return movie by user
● prolong rented movie
● get movie recommendations
● ...
(Normally should be some user stories or whatever)
Example project - movie renting site
Example user stories:
● As a user I cannot rent more than 2 movies at same time
● As a user I cannot rent movies for which I’m too young
● As a user I cannot rent more movies if I have a fee to pay for keeping to long
● As a user ...
Let’s start coding!!!!!!
What is architecture?
“Architecture is a set of rectangles and arrows, where:
● arrows are directed and
● there are no cycles
Rectangles are on it’s own build from rectangles and
arrows.“
What is architecture?
“Architecture is not discovered with unit testing,
it must be done upfront”
What is a unit?
Units:
● units ~~ top-level rectangles
● communicate with other units
○ synchronously or asynchronously
● fulfills specific business cases (single responsibility)
○ made public to the world via a well designed API (called Facade later)
● can have completely different architecture (CQRS, actors, event sourcing...)
Renting site architecture
Users
Add
Get
Movie
Added
Add
Get
Movies
ListGenre
Rent
Rent
Return
Prolonge
Get
Rented
Movie
Rented
Movie
Prolonged
Movie
Returned
Fees
Get
Fees
Pay
Movie
Rented
Movie
Prolonged
Movie
Returned
Recommendations
Get
Search
Search
Add
Movie
Added
Description
Event
produced
Event
consumed
synchronous call
asynchronous call
Unit
Units API - Facade
● use Facade to export available actions
Example Facade
Terminology - Tests kinds
● Unit ~~ module ~~ bounded context
○ module encapsulates it’s data and logic (use DTO to communicate)
○ modules are sliced vertically (all layers)
● Unit tests - checks your business logic (with no external dependencies)
● Integration tests - check your connection with external stuff (DB, HTTP api,
event bus)
● performance, regression, end2end, etc...
Terminology - Tests kinds
GUI
Integration Tests
(API,DB, etc)
Unit Tests
Terminology - Tests kinds
http://fabiopereira.me/blog/2012/03/05/testing-pyramid-a-case-study/
Hexagonal Architecture
https://herbertograca.com/tag/hexagonal-architecture/
Hexagonal Architecture
https://herbertograca.com/tag/hexagonal-architecture/
unit tests
Integration /
Gui
Tests
Tests rules
Unit tests:
● test ONLY by using units public methods (available through Facade)
● do not touch DB or HTTP, etc... check business logic only
● test as much logic as you can using unit tests (test pyramid)
Tests rules
Integration tests:
● setup DB to find out if you’re properly ‘integrated’ with it
● example: add movie with HTTP POST , save it into postgres, and verify that
everything can be retrieved properly (HTTP GET)
● never hit any live service (test server, etc)
Tests - stolen from BDD
How a test look like?
● use BDD like structure
● use given/when/then to distinguish test parts
● example!
BDD user story example
Scenario: As a user I cannot rent more than maximum number of movies at
the same time
Given a user previously rented one movie
And maximum rented movies count is 1
When user wants to rent second movie
Then user cannot rent second movie
Tests - Given
Section given:
● used to setup test data and state
● use only test-specific data (reuse global data for common tests)
● use Facade API for setup
○ don’t use repository directly - you can get into invalid logic due to logic change and test can still
pass
● stay minimal -> don’t create 20 objects to test pagination, etc
Tests - When
Section when:
● action that is being tested at state set up earlier
● single call of Facade API
Tests - Then
Section then:
● verify that things are working correctly
● use Facade to verify expectations
● test should have single reason to fail
Tests - Then
What makes your test great?
“ Test behaviour, not implementation”
== test most things using only your unit’s facade
Project time!
Things normally done wrong
“Adding new class is not a reason to add new test”
“Adding a method is not a reason to add new test”
Group tests properly
My DONT’s
● Don’t split logic into too many microservices too early
○ design so that you can extract it easily later
● Don’t let you tests run too slow
○ people will resist to write new (or just stop using TDD)
○ people will run them too rarely -> keep a quick feedback loop
● Don’t keep a test, that does not make you feel safe
○ just delete it
● Don’t create a test for each new class or method
My DONT’s
● Don’t mix layers of your application in tests
○ facade is used in each operation in given/when/then
● Don’t be afraid of same path covered in integration and unit test
○ they have different purpose and some are run more frequently
● Don’t overuse table tests
○ seen tests with 2-3 if’s inside based on which data is nil or not
○ better split to distinct tests
My DO’s
● test framework/technology is slow or hard to setup? -> change it!
○ example - Kafka Streams -> you can test everything without Kafka running
● use BDD-like given/when/then
○ make your IDE to generate that for you when creating new test
○ use multiple when/then with comment
● make test setup minimal
○ reuse variables
○ extract common things into methods
○ use builders
● after red/green/refactor - take a look at the name and place of a test
Biggest DO DO DO!
“ Test behaviour, not implementation”
Did it solve my problems?
● proper architecture
● hide logic behind a facade
● encapsulation kept in tests
● keeping given/when/then done right
== Tests immutable to refactor
When TDD is best?
When you have NO IDEA how you will code something!!!!!!!!!!!!!!!!
Lets do TDD!
Other stuff if there’s enough time
When to test single class/method?
● edge cases of simple logic
○ use table driven tests -> https://github.com/golang/go/wiki/TableDrivenTests
● For example some calculations based on numbers -> test all corner cases
When to use integration tests?
● heavily relying on DB queries
○ cannot unit test that
○ even then test using facade of your module
● test minimal integration cases, for example to check:
○ if you properly handle events?
○ are HTTP endpoint setup correctly?
○ are DTO’s properly serialized?
When to use mocks/stubs?
● communication with other units
● external clients (best if they provide them for you)
Thanks!
● Project: https://github.com/gmiejski/dvd-rental-tdd-example

More Related Content

What's hot

Refactoring Legacy Code
Refactoring Legacy CodeRefactoring Legacy Code
Refactoring Legacy CodeAdam Culp
 
How to go about testing in React?
How to go about testing in React? How to go about testing in React?
How to go about testing in React? Lisa Gagarina
 
ATDD Using Robot Framework
ATDD Using Robot FrameworkATDD Using Robot Framework
ATDD Using Robot FrameworkPekka Klärck
 
Android Frameworks: Highlighting the Need for a Solid Development Framework 
Android Frameworks: Highlighting the Need for a Solid Development Framework Android Frameworks: Highlighting the Need for a Solid Development Framework 
Android Frameworks: Highlighting the Need for a Solid Development Framework Mutual Mobile
 
Agile Programming Systems # TDD intro
Agile Programming Systems # TDD introAgile Programming Systems # TDD intro
Agile Programming Systems # TDD introVitaliy Kulikov
 
POUG2019 - Test your PL/SQL - your database will love you
POUG2019 - Test your PL/SQL - your database will love youPOUG2019 - Test your PL/SQL - your database will love you
POUG2019 - Test your PL/SQL - your database will love youJacek Gebal
 
8 - Javascript unit testing framework
8 - Javascript unit testing framework8 - Javascript unit testing framework
8 - Javascript unit testing frameworkNguyen Duc Phu
 
TDD for APIs @ Europython 2015, Bilbao by Michael Kuehne
TDD for APIs @ Europython 2015, Bilbao by Michael KuehneTDD for APIs @ Europython 2015, Bilbao by Michael Kuehne
TDD for APIs @ Europython 2015, Bilbao by Michael KuehneMichael Kuehne-Schlinkert
 
Meetup React Sanca - 29/11/18 - React Testing
Meetup React Sanca - 29/11/18 - React TestingMeetup React Sanca - 29/11/18 - React Testing
Meetup React Sanca - 29/11/18 - React TestingAugusto Lazaro
 
Unit Testing your React / Redux app (@BucharestJS)
Unit Testing your React / Redux app (@BucharestJS)Unit Testing your React / Redux app (@BucharestJS)
Unit Testing your React / Redux app (@BucharestJS)Alin Pandichi
 
The Professional Programmer
The Professional ProgrammerThe Professional Programmer
The Professional ProgrammerDave Cross
 
TDD - Designing with Expectations, not Implementations
TDD - Designing with Expectations, not ImplementationsTDD - Designing with Expectations, not Implementations
TDD - Designing with Expectations, not ImplementationsHarshith Shetty
 
Test-Driven Development with Plone
Test-Driven Development with PloneTest-Driven Development with Plone
Test-Driven Development with PloneTimo Stollenwerk
 
Unit testing (workshop)
Unit testing (workshop)Unit testing (workshop)
Unit testing (workshop)Foyzul Karim
 
Testing activities in CI/CD as exploratory tester
Testing activities in CI/CD as exploratory testerTesting activities in CI/CD as exploratory tester
Testing activities in CI/CD as exploratory testerSrinivas Kadiyala
 
Testing activities in continuous integration and continuous delivery as an ex...
Testing activities in continuous integration and continuous delivery as an ex...Testing activities in continuous integration and continuous delivery as an ex...
Testing activities in continuous integration and continuous delivery as an ex...Srinivas Kadiyala
 

What's hot (20)

Unit testing in PHP
Unit testing in PHPUnit testing in PHP
Unit testing in PHP
 
Refactoring Legacy Code
Refactoring Legacy CodeRefactoring Legacy Code
Refactoring Legacy Code
 
How to go about testing in React?
How to go about testing in React? How to go about testing in React?
How to go about testing in React?
 
ATDD Using Robot Framework
ATDD Using Robot FrameworkATDD Using Robot Framework
ATDD Using Robot Framework
 
Android Frameworks: Highlighting the Need for a Solid Development Framework 
Android Frameworks: Highlighting the Need for a Solid Development Framework Android Frameworks: Highlighting the Need for a Solid Development Framework 
Android Frameworks: Highlighting the Need for a Solid Development Framework 
 
Agile Programming Systems # TDD intro
Agile Programming Systems # TDD introAgile Programming Systems # TDD intro
Agile Programming Systems # TDD intro
 
Test driving QML
Test driving QMLTest driving QML
Test driving QML
 
POUG2019 - Test your PL/SQL - your database will love you
POUG2019 - Test your PL/SQL - your database will love youPOUG2019 - Test your PL/SQL - your database will love you
POUG2019 - Test your PL/SQL - your database will love you
 
8 - Javascript unit testing framework
8 - Javascript unit testing framework8 - Javascript unit testing framework
8 - Javascript unit testing framework
 
TDD for APIs @ Europython 2015, Bilbao by Michael Kuehne
TDD for APIs @ Europython 2015, Bilbao by Michael KuehneTDD for APIs @ Europython 2015, Bilbao by Michael Kuehne
TDD for APIs @ Europython 2015, Bilbao by Michael Kuehne
 
Functional Tests. PHP Unconf 2016
Functional Tests. PHP Unconf 2016Functional Tests. PHP Unconf 2016
Functional Tests. PHP Unconf 2016
 
Meetup React Sanca - 29/11/18 - React Testing
Meetup React Sanca - 29/11/18 - React TestingMeetup React Sanca - 29/11/18 - React Testing
Meetup React Sanca - 29/11/18 - React Testing
 
Unit Testing your React / Redux app (@BucharestJS)
Unit Testing your React / Redux app (@BucharestJS)Unit Testing your React / Redux app (@BucharestJS)
Unit Testing your React / Redux app (@BucharestJS)
 
The Professional Programmer
The Professional ProgrammerThe Professional Programmer
The Professional Programmer
 
TDD - Designing with Expectations, not Implementations
TDD - Designing with Expectations, not ImplementationsTDD - Designing with Expectations, not Implementations
TDD - Designing with Expectations, not Implementations
 
TDD
TDDTDD
TDD
 
Test-Driven Development with Plone
Test-Driven Development with PloneTest-Driven Development with Plone
Test-Driven Development with Plone
 
Unit testing (workshop)
Unit testing (workshop)Unit testing (workshop)
Unit testing (workshop)
 
Testing activities in CI/CD as exploratory tester
Testing activities in CI/CD as exploratory testerTesting activities in CI/CD as exploratory tester
Testing activities in CI/CD as exploratory tester
 
Testing activities in continuous integration and continuous delivery as an ex...
Testing activities in continuous integration and continuous delivery as an ex...Testing activities in continuous integration and continuous delivery as an ex...
Testing activities in continuous integration and continuous delivery as an ex...
 

Similar to TDD done right - tests immutable to refactor

Tests immutable when refactoring - SegFault Unconference Cracow 2019
Tests immutable when refactoring - SegFault Unconference Cracow 2019Tests immutable when refactoring - SegFault Unconference Cracow 2019
Tests immutable when refactoring - SegFault Unconference Cracow 2019Grzegorz Miejski
 
A la découverte des google/test (aka gtest)
A la découverte des google/test (aka gtest)A la découverte des google/test (aka gtest)
A la découverte des google/test (aka gtest)Thierry Gayet
 
May 2021 Spark Testing ... or how to farm reputation on StackOverflow
May 2021 Spark Testing ... or how to farm reputation on StackOverflowMay 2021 Spark Testing ... or how to farm reputation on StackOverflow
May 2021 Spark Testing ... or how to farm reputation on StackOverflowAdam Doyle
 
Expedia 3x3 presentation
Expedia 3x3 presentationExpedia 3x3 presentation
Expedia 3x3 presentationDrew Hannay
 
Write unit test from scratch
Write unit test from scratchWrite unit test from scratch
Write unit test from scratchWen-Shih Chao
 
Thucydides - a brief review
Thucydides - a brief reviewThucydides - a brief review
Thucydides - a brief reviewCristian COȚOI
 
Cloud Native CI/CD with Spring Cloud Pipelines
Cloud Native CI/CD with Spring Cloud PipelinesCloud Native CI/CD with Spring Cloud Pipelines
Cloud Native CI/CD with Spring Cloud PipelinesLars Rosenquist
 
Cloud Native CI/CD with Spring Cloud Pipelines
Cloud Native CI/CD with Spring Cloud PipelinesCloud Native CI/CD with Spring Cloud Pipelines
Cloud Native CI/CD with Spring Cloud PipelinesLars Rosenquist
 
DevOps for TYPO3 Teams and Projects
DevOps for TYPO3 Teams and ProjectsDevOps for TYPO3 Teams and Projects
DevOps for TYPO3 Teams and ProjectsFedir RYKHTIK
 
Into The Box 2018 | Assert control over your legacy applications
Into The Box 2018 | Assert control over your legacy applicationsInto The Box 2018 | Assert control over your legacy applications
Into The Box 2018 | Assert control over your legacy applicationsOrtus Solutions, Corp
 
Test all the things! Automated testing with Drupal 8
Test all the things! Automated testing with Drupal 8Test all the things! Automated testing with Drupal 8
Test all the things! Automated testing with Drupal 8Sam Becker
 
Unit Testing Full@
Unit Testing Full@Unit Testing Full@
Unit Testing Full@Alex Borsuk
 
Developers Testing - Girl Code at bloomon
Developers Testing - Girl Code at bloomonDevelopers Testing - Girl Code at bloomon
Developers Testing - Girl Code at bloomonIneke Scheffers
 
What is this agile thing anyway
What is this agile thing anywayWhat is this agile thing anyway
What is this agile thing anywayLisa Van Gelder
 
Effective .NET Core Unit Testing with SQLite and Dapper
Effective .NET Core Unit Testing with SQLite and DapperEffective .NET Core Unit Testing with SQLite and Dapper
Effective .NET Core Unit Testing with SQLite and DapperMike Melusky
 
TDD - Seriously, try it! - Trójmiasto Java User Group (17th May '23)
TDD - Seriously, try it! - Trójmiasto Java User Group (17th May '23)TDD - Seriously, try it! - Trójmiasto Java User Group (17th May '23)
TDD - Seriously, try it! - Trójmiasto Java User Group (17th May '23)ssusercaf6c1
 
TDD - Seriously, try it! - Trjjmiasto JUG (17th May '23)
TDD - Seriously, try it! - Trjjmiasto JUG (17th May '23)TDD - Seriously, try it! - Trjjmiasto JUG (17th May '23)
TDD - Seriously, try it! - Trjjmiasto JUG (17th May '23)Nacho Cougil
 

Similar to TDD done right - tests immutable to refactor (20)

Tests immutable when refactoring - SegFault Unconference Cracow 2019
Tests immutable when refactoring - SegFault Unconference Cracow 2019Tests immutable when refactoring - SegFault Unconference Cracow 2019
Tests immutable when refactoring - SegFault Unconference Cracow 2019
 
A la découverte des google/test (aka gtest)
A la découverte des google/test (aka gtest)A la découverte des google/test (aka gtest)
A la découverte des google/test (aka gtest)
 
May 2021 Spark Testing ... or how to farm reputation on StackOverflow
May 2021 Spark Testing ... or how to farm reputation on StackOverflowMay 2021 Spark Testing ... or how to farm reputation on StackOverflow
May 2021 Spark Testing ... or how to farm reputation on StackOverflow
 
Expedia 3x3 presentation
Expedia 3x3 presentationExpedia 3x3 presentation
Expedia 3x3 presentation
 
Write unit test from scratch
Write unit test from scratchWrite unit test from scratch
Write unit test from scratch
 
Thucydides - a brief review
Thucydides - a brief reviewThucydides - a brief review
Thucydides - a brief review
 
Cloud Native CI/CD with Spring Cloud Pipelines
Cloud Native CI/CD with Spring Cloud PipelinesCloud Native CI/CD with Spring Cloud Pipelines
Cloud Native CI/CD with Spring Cloud Pipelines
 
Cloud Native CI/CD with Spring Cloud Pipelines
Cloud Native CI/CD with Spring Cloud PipelinesCloud Native CI/CD with Spring Cloud Pipelines
Cloud Native CI/CD with Spring Cloud Pipelines
 
Usable Software Design
Usable Software DesignUsable Software Design
Usable Software Design
 
Agile
AgileAgile
Agile
 
DevOps for TYPO3 Teams and Projects
DevOps for TYPO3 Teams and ProjectsDevOps for TYPO3 Teams and Projects
DevOps for TYPO3 Teams and Projects
 
Into The Box 2018 | Assert control over your legacy applications
Into The Box 2018 | Assert control over your legacy applicationsInto The Box 2018 | Assert control over your legacy applications
Into The Box 2018 | Assert control over your legacy applications
 
Test all the things! Automated testing with Drupal 8
Test all the things! Automated testing with Drupal 8Test all the things! Automated testing with Drupal 8
Test all the things! Automated testing with Drupal 8
 
DDD with Behat
DDD with BehatDDD with Behat
DDD with Behat
 
Unit Testing Full@
Unit Testing Full@Unit Testing Full@
Unit Testing Full@
 
Developers Testing - Girl Code at bloomon
Developers Testing - Girl Code at bloomonDevelopers Testing - Girl Code at bloomon
Developers Testing - Girl Code at bloomon
 
What is this agile thing anyway
What is this agile thing anywayWhat is this agile thing anyway
What is this agile thing anyway
 
Effective .NET Core Unit Testing with SQLite and Dapper
Effective .NET Core Unit Testing with SQLite and DapperEffective .NET Core Unit Testing with SQLite and Dapper
Effective .NET Core Unit Testing with SQLite and Dapper
 
TDD - Seriously, try it! - Trójmiasto Java User Group (17th May '23)
TDD - Seriously, try it! - Trójmiasto Java User Group (17th May '23)TDD - Seriously, try it! - Trójmiasto Java User Group (17th May '23)
TDD - Seriously, try it! - Trójmiasto Java User Group (17th May '23)
 
TDD - Seriously, try it! - Trjjmiasto JUG (17th May '23)
TDD - Seriously, try it! - Trjjmiasto JUG (17th May '23)TDD - Seriously, try it! - Trjjmiasto JUG (17th May '23)
TDD - Seriously, try it! - Trjjmiasto JUG (17th May '23)
 

Recently uploaded

High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2RajaP95
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 

Recently uploaded (20)

High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 

TDD done right - tests immutable to refactor

  • 1. TDD done “right” - tests immutable to refactor Grzesiek Miejski
  • 3. Little about me ● Coding microservices for ~4 years ● Different languages and technologies: ○ now: Go, python ○ previously: Kotlin, Java ○ MongoDB, SQL, ElasticSearch, Cassandra, Kafka, RabbitMQ, blabla
  • 4. Are there not enough TDD materials already?
  • 5. My previous problems with tests ● changing business logic made me change tests a lot ● refactoring made me change tests ● applied “real” TDD only to the simplest code, with structure predicted upfront Is it worth trying then?
  • 7. But in what context? When my advices are applicable for me? ● everywhere where you want you software to last and be developed ● when building microservices, as this is what I do right now ● but would try love to try those practices in monolith written from start!
  • 8. Plan for presentation 1. About architecture 2. Unit and Facade 3. Unit and Integration tests 4. Unit tests in BDD style done right 5. Project immutable to refactor - example 6. DO’s and DONT’s 7. TDD goes live!
  • 9. Example project - movie renting site Simplified requirements: ● view available movies ● rent movie by user ● return movie by user ● prolong rented movie ● get movie recommendations ● ... (Normally should be some user stories or whatever)
  • 10. Example project - movie renting site Example user stories: ● As a user I cannot rent more than 2 movies at same time ● As a user I cannot rent movies for which I’m too young ● As a user I cannot rent more movies if I have a fee to pay for keeping to long ● As a user ...
  • 12. What is architecture? “Architecture is a set of rectangles and arrows, where: ● arrows are directed and ● there are no cycles Rectangles are on it’s own build from rectangles and arrows.“
  • 13. What is architecture? “Architecture is not discovered with unit testing, it must be done upfront”
  • 14. What is a unit? Units: ● units ~~ top-level rectangles ● communicate with other units ○ synchronously or asynchronously ● fulfills specific business cases (single responsibility) ○ made public to the world via a well designed API (called Facade later) ● can have completely different architecture (CQRS, actors, event sourcing...)
  • 16. Units API - Facade ● use Facade to export available actions
  • 18. Terminology - Tests kinds ● Unit ~~ module ~~ bounded context ○ module encapsulates it’s data and logic (use DTO to communicate) ○ modules are sliced vertically (all layers) ● Unit tests - checks your business logic (with no external dependencies) ● Integration tests - check your connection with external stuff (DB, HTTP api, event bus) ● performance, regression, end2end, etc...
  • 19. Terminology - Tests kinds GUI Integration Tests (API,DB, etc) Unit Tests
  • 20. Terminology - Tests kinds http://fabiopereira.me/blog/2012/03/05/testing-pyramid-a-case-study/
  • 23. Tests rules Unit tests: ● test ONLY by using units public methods (available through Facade) ● do not touch DB or HTTP, etc... check business logic only ● test as much logic as you can using unit tests (test pyramid)
  • 24. Tests rules Integration tests: ● setup DB to find out if you’re properly ‘integrated’ with it ● example: add movie with HTTP POST , save it into postgres, and verify that everything can be retrieved properly (HTTP GET) ● never hit any live service (test server, etc)
  • 25. Tests - stolen from BDD How a test look like? ● use BDD like structure ● use given/when/then to distinguish test parts ● example!
  • 26. BDD user story example Scenario: As a user I cannot rent more than maximum number of movies at the same time Given a user previously rented one movie And maximum rented movies count is 1 When user wants to rent second movie Then user cannot rent second movie
  • 27. Tests - Given Section given: ● used to setup test data and state ● use only test-specific data (reuse global data for common tests) ● use Facade API for setup ○ don’t use repository directly - you can get into invalid logic due to logic change and test can still pass ● stay minimal -> don’t create 20 objects to test pagination, etc
  • 28. Tests - When Section when: ● action that is being tested at state set up earlier ● single call of Facade API
  • 29. Tests - Then Section then: ● verify that things are working correctly ● use Facade to verify expectations ● test should have single reason to fail
  • 31. What makes your test great? “ Test behaviour, not implementation” == test most things using only your unit’s facade
  • 33. Things normally done wrong “Adding new class is not a reason to add new test” “Adding a method is not a reason to add new test”
  • 35. My DONT’s ● Don’t split logic into too many microservices too early ○ design so that you can extract it easily later ● Don’t let you tests run too slow ○ people will resist to write new (or just stop using TDD) ○ people will run them too rarely -> keep a quick feedback loop ● Don’t keep a test, that does not make you feel safe ○ just delete it ● Don’t create a test for each new class or method
  • 36. My DONT’s ● Don’t mix layers of your application in tests ○ facade is used in each operation in given/when/then ● Don’t be afraid of same path covered in integration and unit test ○ they have different purpose and some are run more frequently ● Don’t overuse table tests ○ seen tests with 2-3 if’s inside based on which data is nil or not ○ better split to distinct tests
  • 37. My DO’s ● test framework/technology is slow or hard to setup? -> change it! ○ example - Kafka Streams -> you can test everything without Kafka running ● use BDD-like given/when/then ○ make your IDE to generate that for you when creating new test ○ use multiple when/then with comment ● make test setup minimal ○ reuse variables ○ extract common things into methods ○ use builders ● after red/green/refactor - take a look at the name and place of a test
  • 38. Biggest DO DO DO! “ Test behaviour, not implementation”
  • 39. Did it solve my problems? ● proper architecture ● hide logic behind a facade ● encapsulation kept in tests ● keeping given/when/then done right == Tests immutable to refactor
  • 40. When TDD is best? When you have NO IDEA how you will code something!!!!!!!!!!!!!!!!
  • 41.
  • 43. Other stuff if there’s enough time
  • 44. When to test single class/method? ● edge cases of simple logic ○ use table driven tests -> https://github.com/golang/go/wiki/TableDrivenTests ● For example some calculations based on numbers -> test all corner cases
  • 45. When to use integration tests? ● heavily relying on DB queries ○ cannot unit test that ○ even then test using facade of your module ● test minimal integration cases, for example to check: ○ if you properly handle events? ○ are HTTP endpoint setup correctly? ○ are DTO’s properly serialized?
  • 46. When to use mocks/stubs? ● communication with other units ● external clients (best if they provide them for you)