SlideShare a Scribd company logo
1 of 47
Download to read offline
Jens Happe
Head of Software Engineering - Chrono24
Co-Founder - Sparkteams
Write tests you love, not hate
What does the following test check?
What does this test check?
Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 3
What does this test check?
Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 4
Call to component under test (CUT)
Configuration of mocked objects
Verification
Set the next user id
Set the next activation code
Register new user
Verify email sent
Verify user saved
Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 5
Too much boilerplate code
• Tests are hard to understand
• Writing tests is a lot of effort
The Fragile Test Problem
• Adjusting tests after a minor change
takes two days
• Tests generate too many false positives
Tests are running too long
à No one likes writing tests
https://imgs.xkcd.com/comics/compiling.png
Common Problems with Unit Testing
Okay, no one likes writing tests. So what?!
The consequences of a bad test structure (simplified)
Write tests you love, not hate | Jens Happe | Chrono24 6
Sep-23
What if…
The consequences of a good test structure (simplified)
Write tests you love, not hate | Jens Happe | Chrono24 7
Sep-23
Sep-23
Agenda
Common problems with Unit Testing
Increase readability
by getting the basics right
Reduce boilerplate code
by using Trainers and Entity Builders
Resolve the Fragile Test Problem
by decoupling tests and implementation
Speed up slow tests
by abstraction of the platform
Increase readability
Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 9
http://xunitpatterns.com/Test%20Utility%20Method.html
https://cucumber.io/docs/gherkin/reference/
when
given
then
Set the next user id
Set the next activation code
Register new user
Verify email sent
Verify user saved
Extract method with Given / When / Then
Increase readability
Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 10
Extract method with Given / When / Then
when
given
then
https://martinfowler.com/bliki/GivenWhenThen.html
Increase readability
Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 11
Explicit relationship between Given and Then
when
given
then
Getting the basic right
for tests already gets you pretty far.
So, we are done now, right?
Thank you for your attention!
It was a pleasure J
Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 13
Of course not.
• Our Test Class is still a mess
containing almost only
boiler plate code.
• We cannot reuse the given…
and then… methods for
other test cases.
Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 14
Test Boilerplate
Sep-23
Agenda
Common problems with Unit Testing
Increase readability
by getting the basics right
Reduce boilerplate code
by using Trainers and Entity Builders
Resolve the Fragile Test Problem
by decoupling tests and implementation
Speed up slow tests
by abstraction of the platform
Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 16
Entity Builders simplify test setup
https://github.com/casid/jusecase-builders-generator
Trainers
Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 17
Problem: Configuring mocked objects is very repetitive and verbose
Trainers
Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 18
Encapsulate the configuration in separate classes called Trainers
Trainers
Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 19
Encapsulate the configuration in separate classes called Trainers
…
Trainers
Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 20
Encapsulate the configuration in separate classes called Trainers
…
Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 21
• Configuration of mocked objects outside of the test classes
-> increased readability
-> increased reusability
• Generic Trainers can be developed, that further simplify the test setup
Trainers
Benefits
Entity Builders and Trainers
simplify the setup of the test fixture.
Sep-23
Agenda
Common problems with Unit Testing
Increase readability
by getting the basics right
Reduce boilerplate code
by using Trainers and Entity Builders
Resolve the Fragile Test Problem
by decoupling tests and implementation
Speed up slow tests
by abstraction of the platform
Fragile Test Problem
Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 24
Definition
Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 25
Fragile Test Problem
Definition
Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 26
Fragile Test Problem
Definition
Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 27
Fragile Test Problem
Definition
Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 28
Fragile Test Problem
Definition
Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 29
Fragile Test Problem
This is fine.
Fragile Test Problem
Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 30
This is no refactoring!
Fragile Test Problem
Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 31
Tight Coupling
Fragile Test Problem
Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 32
Tight Coupling = Bad Design
Fragile Test Problem
Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 33
Solution: Loosely coupled tests
https://blog.cleancoder.com/uncle-bob/2017/10/03/TestContravariance.html
Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 34
Fragile Test Problem
Solution: Loosely coupled tests
https://blog.cleancoder.com/uncle-bob/2017/10/03/TestContravariance.html
That breaks the rules!
For every class X there should
be a test class XTest.
Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 35
But wait.
Fragile Test Problem
Isn’t that indirect testing?
Typical example for indirect
testing: Testing through the
presentation layer
No. Here we test the result of
the interaction between the
services, not individual
services.
Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 36
But wait.
Fragile Test Problem
http://xunitpatterns.com/Obscure%20Test.html#Indirect%20Testing
It's much harder now to
identify the cause of a failing
test!
No. You should work in small
increments and run tests after
every change.
That makes it easy to identify
the cause of a failing test.
Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 37
But wait.
Fragile Test Problem
Tests should be useful.
So, design them that way.
Sep-23
Agenda
Common problems with Unit Testing
Increase readability
by getting the basics right
Reduce boilerplate code
by using Trainers and Entity Builders
Resolve the Fragile Test Problem
by decoupling tests and implementation
Speed up slow tests
by abstraction of the platform
Speed up slow tests
Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 40
External dependencies slow down test execution
Speed up slow tests
Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 41
Push all external dependencies outside and only test the logic
• Dependency Rule
• Outside concrete, inside
abstract
-> Intrinsically testable
Speed up slow tests
Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 42
However, the reality is messy.
Speed up slow tests
Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 43
However, the reality is messy. So, let’s deal with it.
Don’t be afraid of tests.
Our journey
2003 – 2016
• Almost no automated tests
2016 – 2018
• Test-Driven Development ideas
• Entity Builder and Trainer
• Establishment of use case tests
2019
• Move from Jenkins to GitLab CI
• Run tests with every push
2022
• Monorepo with parallel build
2023
à 11.935 use case (unit) tests that run in less than a minute
Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 45
… until now
• Removing and simplifying supporting frameworks to run tests
à Fast test execution
Separating tests from external dependencies
• Testing at the borders of what matters at this point
à Tests and implementation can be structured independently
Decoupling test and implementation
• Entity Builders allow clear data definition
• Trainers simplify the configuration of dependencies
à Reusable and simple setup
Defining scenarios simplified
• Given / When / Then gives structure
• Explicit relationship between pre- and post-condition
à Increased readability
Getting the basics right
Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 46
Key Takeaways
Many people contributed to this work:
• Andreas Hager
• Niklas Keller
• Martin Hofmann-Sobik
• And many others…
Email:
jens.happe@chrono24.com
X / Twitter:
@HappeJens
LinkedIn:
https://www.linkedin.com/in/jens-happe-idea-to-impact/
Thank you!
Maybe you? Join us!
https://about.chrono24.com/jobs/technology/

More Related Content

Similar to Jens Happe - Write tests you love, not hate.pdf

Engl317 project4 slidedoc2_stepsto_designux_test
Engl317 project4 slidedoc2_stepsto_designux_testEngl317 project4 slidedoc2_stepsto_designux_test
Engl317 project4 slidedoc2_stepsto_designux_testZachary Williamson
 
Introduction to Test Driven Development
Introduction to Test Driven DevelopmentIntroduction to Test Driven Development
Introduction to Test Driven DevelopmentMichael Denomy
 
TDD — Are you sure you properly test code?
TDD — Are you sure you properly test code?TDD — Are you sure you properly test code?
TDD — Are you sure you properly test code?Dmitriy Nesteryuk
 
Approval Tests in Action: A LEGO Exercise and an Experience Report
Approval Tests in Action: A LEGO Exercise and an Experience ReportApproval Tests in Action: A LEGO Exercise and an Experience Report
Approval Tests in Action: A LEGO Exercise and an Experience Reporthouseofyin
 
Lessons learned from measuring software development processes
Lessons learned from measuring software development processesLessons learned from measuring software development processes
Lessons learned from measuring software development processesMarkus Unterauer
 
Writing acceptable patches: an empirical study of open source project patches
Writing acceptable patches: an empirical study of open source project patchesWriting acceptable patches: an empirical study of open source project patches
Writing acceptable patches: an empirical study of open source project patchesYida Tao
 
The future of testing magento 2 james cowie from shero commerce - 10 24-20 ...
The future of testing magento 2   james cowie from shero commerce - 10 24-20 ...The future of testing magento 2   james cowie from shero commerce - 10 24-20 ...
The future of testing magento 2 james cowie from shero commerce - 10 24-20 ...James Cowie
 
Lecture 10: ML Testing & Explainability (Full Stack Deep Learning - Spring 2021)
Lecture 10: ML Testing & Explainability (Full Stack Deep Learning - Spring 2021)Lecture 10: ML Testing & Explainability (Full Stack Deep Learning - Spring 2021)
Lecture 10: ML Testing & Explainability (Full Stack Deep Learning - Spring 2021)Sergey Karayev
 
How to improve your unit tests?
How to improve your unit tests?How to improve your unit tests?
How to improve your unit tests?Péter Módos
 
VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)Rob Hale
 
www.tutorialsbook.com presents Manual testing
www.tutorialsbook.com presents Manual testingwww.tutorialsbook.com presents Manual testing
www.tutorialsbook.com presents Manual testingTutorials Book
 
{10.0} Test Driven Development.pptx
{10.0} Test Driven Development.pptx{10.0} Test Driven Development.pptx
{10.0} Test Driven Development.pptxAmalEldhose2
 
AgileTestingOverview
AgileTestingOverviewAgileTestingOverview
AgileTestingOverviewUmair Anis
 
Getting started with Test Driven Development - Ferdous Mahmud Shaon
Getting started with Test Driven Development - Ferdous Mahmud ShaonGetting started with Test Driven Development - Ferdous Mahmud Shaon
Getting started with Test Driven Development - Ferdous Mahmud ShaonCefalo
 
Getting started with Test Driven Development
Getting started with Test Driven DevelopmentGetting started with Test Driven Development
Getting started with Test Driven DevelopmentFerdous Mahmud Shaon
 
Bigger Unit Test Are Better
Bigger Unit Test Are BetterBigger Unit Test Are Better
Bigger Unit Test Are BetterPeter Schuler
 
IT Software - Release cycle & Delivery roadmap
IT Software - Release cycle & Delivery roadmapIT Software - Release cycle & Delivery roadmap
IT Software - Release cycle & Delivery roadmapJean-François Nguyen
 

Similar to Jens Happe - Write tests you love, not hate.pdf (20)

Agile Practices
Agile PracticesAgile Practices
Agile Practices
 
Engl317 project4 slidedoc2_stepsto_designux_test
Engl317 project4 slidedoc2_stepsto_designux_testEngl317 project4 slidedoc2_stepsto_designux_test
Engl317 project4 slidedoc2_stepsto_designux_test
 
Introduction to Test Driven Development
Introduction to Test Driven DevelopmentIntroduction to Test Driven Development
Introduction to Test Driven Development
 
TDD — Are you sure you properly test code?
TDD — Are you sure you properly test code?TDD — Are you sure you properly test code?
TDD — Are you sure you properly test code?
 
Approval Tests in Action: A LEGO Exercise and an Experience Report
Approval Tests in Action: A LEGO Exercise and an Experience ReportApproval Tests in Action: A LEGO Exercise and an Experience Report
Approval Tests in Action: A LEGO Exercise and an Experience Report
 
Lessons learned from measuring software development processes
Lessons learned from measuring software development processesLessons learned from measuring software development processes
Lessons learned from measuring software development processes
 
Writing acceptable patches: an empirical study of open source project patches
Writing acceptable patches: an empirical study of open source project patchesWriting acceptable patches: an empirical study of open source project patches
Writing acceptable patches: an empirical study of open source project patches
 
midterm_fa08.pdf
midterm_fa08.pdfmidterm_fa08.pdf
midterm_fa08.pdf
 
The future of testing magento 2 james cowie from shero commerce - 10 24-20 ...
The future of testing magento 2   james cowie from shero commerce - 10 24-20 ...The future of testing magento 2   james cowie from shero commerce - 10 24-20 ...
The future of testing magento 2 james cowie from shero commerce - 10 24-20 ...
 
Lecture 10: ML Testing & Explainability (Full Stack Deep Learning - Spring 2021)
Lecture 10: ML Testing & Explainability (Full Stack Deep Learning - Spring 2021)Lecture 10: ML Testing & Explainability (Full Stack Deep Learning - Spring 2021)
Lecture 10: ML Testing & Explainability (Full Stack Deep Learning - Spring 2021)
 
How to improve your unit tests?
How to improve your unit tests?How to improve your unit tests?
How to improve your unit tests?
 
VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)
 
www.tutorialsbook.com presents Manual testing
www.tutorialsbook.com presents Manual testingwww.tutorialsbook.com presents Manual testing
www.tutorialsbook.com presents Manual testing
 
{10.0} Test Driven Development.pptx
{10.0} Test Driven Development.pptx{10.0} Test Driven Development.pptx
{10.0} Test Driven Development.pptx
 
AgileTestingOverview
AgileTestingOverviewAgileTestingOverview
AgileTestingOverview
 
Getting started with Test Driven Development - Ferdous Mahmud Shaon
Getting started with Test Driven Development - Ferdous Mahmud ShaonGetting started with Test Driven Development - Ferdous Mahmud Shaon
Getting started with Test Driven Development - Ferdous Mahmud Shaon
 
Getting started with Test Driven Development
Getting started with Test Driven DevelopmentGetting started with Test Driven Development
Getting started with Test Driven Development
 
Bigger Unit Test Are Better
Bigger Unit Test Are BetterBigger Unit Test Are Better
Bigger Unit Test Are Better
 
Peer review
Peer reviewPeer review
Peer review
 
IT Software - Release cycle & Delivery roadmap
IT Software - Release cycle & Delivery roadmapIT Software - Release cycle & Delivery roadmap
IT Software - Release cycle & Delivery roadmap
 

Recently uploaded

DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsMehedi Hasan Shohan
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
cybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningcybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningVitsRangannavar
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 

Recently uploaded (20)

DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software Solutions
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
cybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningcybersecurity notes for mca students for learning
cybersecurity notes for mca students for learning
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 

Jens Happe - Write tests you love, not hate.pdf

  • 1. Jens Happe Head of Software Engineering - Chrono24 Co-Founder - Sparkteams Write tests you love, not hate
  • 2. What does the following test check?
  • 3. What does this test check? Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 3
  • 4. What does this test check? Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 4 Call to component under test (CUT) Configuration of mocked objects Verification Set the next user id Set the next activation code Register new user Verify email sent Verify user saved
  • 5. Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 5 Too much boilerplate code • Tests are hard to understand • Writing tests is a lot of effort The Fragile Test Problem • Adjusting tests after a minor change takes two days • Tests generate too many false positives Tests are running too long à No one likes writing tests https://imgs.xkcd.com/comics/compiling.png Common Problems with Unit Testing
  • 6. Okay, no one likes writing tests. So what?! The consequences of a bad test structure (simplified) Write tests you love, not hate | Jens Happe | Chrono24 6 Sep-23
  • 7. What if… The consequences of a good test structure (simplified) Write tests you love, not hate | Jens Happe | Chrono24 7 Sep-23
  • 8. Sep-23 Agenda Common problems with Unit Testing Increase readability by getting the basics right Reduce boilerplate code by using Trainers and Entity Builders Resolve the Fragile Test Problem by decoupling tests and implementation Speed up slow tests by abstraction of the platform
  • 9. Increase readability Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 9 http://xunitpatterns.com/Test%20Utility%20Method.html https://cucumber.io/docs/gherkin/reference/ when given then Set the next user id Set the next activation code Register new user Verify email sent Verify user saved Extract method with Given / When / Then
  • 10. Increase readability Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 10 Extract method with Given / When / Then when given then https://martinfowler.com/bliki/GivenWhenThen.html
  • 11. Increase readability Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 11 Explicit relationship between Given and Then when given then
  • 12. Getting the basic right for tests already gets you pretty far.
  • 13. So, we are done now, right? Thank you for your attention! It was a pleasure J Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 13
  • 14. Of course not. • Our Test Class is still a mess containing almost only boiler plate code. • We cannot reuse the given… and then… methods for other test cases. Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 14 Test Boilerplate
  • 15. Sep-23 Agenda Common problems with Unit Testing Increase readability by getting the basics right Reduce boilerplate code by using Trainers and Entity Builders Resolve the Fragile Test Problem by decoupling tests and implementation Speed up slow tests by abstraction of the platform
  • 16. Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 16 Entity Builders simplify test setup https://github.com/casid/jusecase-builders-generator
  • 17. Trainers Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 17 Problem: Configuring mocked objects is very repetitive and verbose
  • 18. Trainers Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 18 Encapsulate the configuration in separate classes called Trainers
  • 19. Trainers Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 19 Encapsulate the configuration in separate classes called Trainers …
  • 20. Trainers Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 20 Encapsulate the configuration in separate classes called Trainers …
  • 21. Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 21 • Configuration of mocked objects outside of the test classes -> increased readability -> increased reusability • Generic Trainers can be developed, that further simplify the test setup Trainers Benefits
  • 22. Entity Builders and Trainers simplify the setup of the test fixture.
  • 23. Sep-23 Agenda Common problems with Unit Testing Increase readability by getting the basics right Reduce boilerplate code by using Trainers and Entity Builders Resolve the Fragile Test Problem by decoupling tests and implementation Speed up slow tests by abstraction of the platform
  • 24. Fragile Test Problem Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 24 Definition
  • 25. Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 25 Fragile Test Problem Definition
  • 26. Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 26 Fragile Test Problem Definition
  • 27. Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 27 Fragile Test Problem Definition
  • 28. Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 28 Fragile Test Problem Definition
  • 29. Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 29 Fragile Test Problem This is fine.
  • 30. Fragile Test Problem Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 30 This is no refactoring!
  • 31. Fragile Test Problem Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 31 Tight Coupling
  • 32. Fragile Test Problem Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 32 Tight Coupling = Bad Design
  • 33. Fragile Test Problem Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 33 Solution: Loosely coupled tests https://blog.cleancoder.com/uncle-bob/2017/10/03/TestContravariance.html
  • 34. Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 34 Fragile Test Problem Solution: Loosely coupled tests https://blog.cleancoder.com/uncle-bob/2017/10/03/TestContravariance.html
  • 35. That breaks the rules! For every class X there should be a test class XTest. Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 35 But wait. Fragile Test Problem
  • 36. Isn’t that indirect testing? Typical example for indirect testing: Testing through the presentation layer No. Here we test the result of the interaction between the services, not individual services. Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 36 But wait. Fragile Test Problem http://xunitpatterns.com/Obscure%20Test.html#Indirect%20Testing
  • 37. It's much harder now to identify the cause of a failing test! No. You should work in small increments and run tests after every change. That makes it easy to identify the cause of a failing test. Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 37 But wait. Fragile Test Problem
  • 38. Tests should be useful. So, design them that way.
  • 39. Sep-23 Agenda Common problems with Unit Testing Increase readability by getting the basics right Reduce boilerplate code by using Trainers and Entity Builders Resolve the Fragile Test Problem by decoupling tests and implementation Speed up slow tests by abstraction of the platform
  • 40. Speed up slow tests Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 40 External dependencies slow down test execution
  • 41. Speed up slow tests Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 41 Push all external dependencies outside and only test the logic • Dependency Rule • Outside concrete, inside abstract -> Intrinsically testable
  • 42. Speed up slow tests Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 42 However, the reality is messy.
  • 43. Speed up slow tests Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 43 However, the reality is messy. So, let’s deal with it.
  • 44. Don’t be afraid of tests.
  • 45. Our journey 2003 – 2016 • Almost no automated tests 2016 – 2018 • Test-Driven Development ideas • Entity Builder and Trainer • Establishment of use case tests 2019 • Move from Jenkins to GitLab CI • Run tests with every push 2022 • Monorepo with parallel build 2023 à 11.935 use case (unit) tests that run in less than a minute Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 45 … until now
  • 46. • Removing and simplifying supporting frameworks to run tests à Fast test execution Separating tests from external dependencies • Testing at the borders of what matters at this point à Tests and implementation can be structured independently Decoupling test and implementation • Entity Builders allow clear data definition • Trainers simplify the configuration of dependencies à Reusable and simple setup Defining scenarios simplified • Given / When / Then gives structure • Explicit relationship between pre- and post-condition à Increased readability Getting the basics right Sep-23 Write tests you love, not hate | Jens Happe | Chrono24 46 Key Takeaways
  • 47. Many people contributed to this work: • Andreas Hager • Niklas Keller • Martin Hofmann-Sobik • And many others… Email: jens.happe@chrono24.com X / Twitter: @HappeJens LinkedIn: https://www.linkedin.com/in/jens-happe-idea-to-impact/ Thank you! Maybe you? Join us! https://about.chrono24.com/jobs/technology/