SlideShare a Scribd company logo
1 of 36
Unit Testing: the Clean
Tests
by Serge Kukharev
Unit tests are:
• testing one small unit of code at a time
• isolated from the other code
• fast
• clean
• the form of documentation
• as important as the code itself
Unit tests are NOT:
• Integration tests (violates speed and isolation rules
of unit tests)
• Functional tests (violates speed and scope rules)
Unit Testing benefits
• Helps to be sure about code changes
• Helps to prevent bugs
• Improves QA speed
• Reduces technical dept
• Leads to better design
–Kent Beck
“Tests are the Programmer’s stone, transmuting
fear into boredom.”
One Unit at a time
• One of the most important rules of unit testing.
• In scope of a test case means, that the small(est) unit of code is tested:
• preferably only one class at a time
• sometimes group of classes is OK, if you can call them as one single
“unit”
• In scope of single test, unit means one method.
• This rule helps to be sure that specific unit works properly, as designed.
• Makes the tests very fast.
Test Isolation
Dummy
does nothing, helps run the tests
Stub
Allows to inject desired input for specific test
Spy
Allows us to spy on the dependencies
(should be used very carefully, leads to implementation coupling)
Fake
Allows to simulate behaviour.
Often useful outside of the tests.
Mock
Allows to set dependency behaviour dynamically.
Can act as spy, fake, stub or dummy.
Two styles of Unit
Testing
Classical
Focused on testing class with all dependencies.
t for dependencies are used mostly, and only dependencies like
Classical Pros
• Easier to write
• Less coupling on the implementation
• Less supportive code
• Easier to make cleaner.
Classical Cons
• Ofter tests more than one Unit
• Duplicate tests
• Not always fast
• Problems with dependencies of dependencies
• Only applicable when application has proper
layers
Mockist
Every dependency is mocked.
Mockist Pros
• Allows to write True Unit tests.
• Allows full control over the dependencies.
• Usually is faster.
• Improves test isolation, leading to easier
bug finding.
Mockist Cons
• Sometimes couples you on the implementation.
• More supportive code.
• Test will fail more often when refactoring.
So, what to choose?
Anything will work, pick what you like more.
Anything 3rd party should be
mocked
• DB classes
• File system calls
• API calls
• etc.
• External library classes
• even framework
PHPUnit TestSuit
anatomy
• TestCase consists of 1 or more tests.
• setUp method is used for arranging
shared data
• tearDown used for cleanup
• dataProviders are used to one test
multiple times with different data
each time
• Custom assertements improve
readability and bug detection
• Other private methods used to
improve readability and code reuse.
Data Providers
• Used to run the same test with
multiple different datasets.
• Can simplify things
• Can make things worse
• I prefer to avoid them
Clean Code in Unit Tests
• Clean tests are as much important as Clean Code itself.
• Reasons:
• Tests are part of code base.
• Test are at least as much important as the code itself.
• Tests are documentation.
• Bad smells in tests can lead to maintenance costs of
supporting those tests.
Arrange, Act, Assert rule
• This is advanced rule, that helps to
write Clean Tests
• Every test may consist of 3 parts
• Every part should be in specific order
• First you arrange your test. You define
behaviour of your dependencies.
• Next, you actually call the behaviour
you are testing.
• Finally, you make assortments on that
behaviour.
• This simple rule leads to much cleaner
and readable tests.
Custom Assertments
• Writing custom assertments lead to reusable test code
pieces.
• It helps be more specific about what have failed. Compare
this two messages:
• “Test failed asserting true is false”
• “Test failed asserting that SupplierConfig is instance of
CarConfig”
• This might look like extra work, but in fact it isn’t - PHPStorm
automated refactoring can help you do this in 30 seconds.
TDD: The Next Step
• Test Driven Development is the next step.
• Most people think that writing tests is TDD. That’s
wrong.
• TDD main principle says “You are not allowed to
write production code without writing a failing unit
test first”.
• TDD assures that all written code is covered with
tests.
• TDD leads to much better design.
• TDD is more productive.
• TDD leads to a much bigger amount of tests.
• TDD leads to a much better quality of tests.
TDD: How it works
• TDD has three main steps - Red, Green, Refactor
• In the Red phase you write a minimal specific failing test.
• In the Green phase you implement it.
• In the Refactoring phase you see how you can improve
existing code.
• “Refactoring - to restructure software […] without
changing the observable behavior of the software.” - Kent
Beck
• You continue on, by adding more failing tests.
• The more you write your tests, the less specific they
are.
• “As the tests get more specific, the code gets more
generic.” - Robert Martin
TDD: Myths
• TDD is slow.
• Managers won’t allow it.
• Programmers aren’t testers.
• TDD is too hard.
Unit Testing resources
• “Test Driven Development: by Example” by Kent
Beck
• “Refactoring” by Martin Fowler
• Clean Code video series by Robert Martin (aka
Uncle Bob). Ep. #6, 19, 20, 21, 22, 23
Thank you!
(please give me your feedback, it is really important for me)
(c) Serge Kukharev 2015

More Related Content

What's hot

Avoiding test hell
Avoiding test hellAvoiding test hell
Avoiding test hellYun Ki Lee
 
Agile Mumbai 2020 Conference | How to get the best ROI on Your Test Automati...
Agile Mumbai 2020 Conference |  How to get the best ROI on Your Test Automati...Agile Mumbai 2020 Conference |  How to get the best ROI on Your Test Automati...
Agile Mumbai 2020 Conference | How to get the best ROI on Your Test Automati...AgileNetwork
 
Unit testing
Unit testingUnit testing
Unit testingBrian Hu
 
Olli-Pekka Puolitaival - Model-Based Testing for Integration Testing in Real ...
Olli-Pekka Puolitaival - Model-Based Testing for Integration Testing in Real ...Olli-Pekka Puolitaival - Model-Based Testing for Integration Testing in Real ...
Olli-Pekka Puolitaival - Model-Based Testing for Integration Testing in Real ...TEST Huddle
 
Introduction to Automated Testing
Introduction to Automated TestingIntroduction to Automated Testing
Introduction to Automated TestingLars Thorup
 
How good are your tests?
How good are your tests?How good are your tests?
How good are your tests?Noam Shaish
 
Unit testing (workshop)
Unit testing (workshop)Unit testing (workshop)
Unit testing (workshop)Foyzul Karim
 
Writing better tests for your java script app
Writing better tests for your java script appWriting better tests for your java script app
Writing better tests for your java script appJakeGinnivan
 
Unit Testing and Tools
Unit Testing and ToolsUnit Testing and Tools
Unit Testing and ToolsWilliam Simms
 
Unit Testing and Tools - ADNUG
Unit Testing and Tools - ADNUGUnit Testing and Tools - ADNUG
Unit Testing and Tools - ADNUGWilliam Simms
 
Overview of Testing Talks at Pycon
Overview of Testing Talks at PyconOverview of Testing Talks at Pycon
Overview of Testing Talks at PyconJacqueline Kazil
 
Unit Test + Functional Programming = Love
Unit Test + Functional Programming = LoveUnit Test + Functional Programming = Love
Unit Test + Functional Programming = LoveAlvaro Videla
 
Moq presentation
Moq presentationMoq presentation
Moq presentationLynxStar
 
Getting Ahead of Delivery Issues with Deep SDLC Analysis by Donald Belcham
Getting Ahead of Delivery Issues with Deep SDLC Analysis by Donald BelchamGetting Ahead of Delivery Issues with Deep SDLC Analysis by Donald Belcham
Getting Ahead of Delivery Issues with Deep SDLC Analysis by Donald Belcham.NET Conf UY
 
2020-Feb: Testing: Cables and Chains
2020-Feb: Testing: Cables and Chains2020-Feb: Testing: Cables and Chains
2020-Feb: Testing: Cables and ChainsMark Windholtz
 
Automated Testing but like for PowerShell (April 2012)
Automated Testing but like for PowerShell (April 2012)Automated Testing but like for PowerShell (April 2012)
Automated Testing but like for PowerShell (April 2012)Rob Reynolds
 
Get Testing with tSQLt - SQL In The City Workshop 2014
Get Testing with tSQLt - SQL In The City Workshop 2014Get Testing with tSQLt - SQL In The City Workshop 2014
Get Testing with tSQLt - SQL In The City Workshop 2014Red Gate Software
 
Generalization in Auto-Testing. How we put what we had into new Technological...
Generalization in Auto-Testing. How we put what we had into new Technological...Generalization in Auto-Testing. How we put what we had into new Technological...
Generalization in Auto-Testing. How we put what we had into new Technological...SQALab
 

What's hot (20)

Avoiding test hell
Avoiding test hellAvoiding test hell
Avoiding test hell
 
CNUG TDD June 2014
CNUG TDD June 2014CNUG TDD June 2014
CNUG TDD June 2014
 
Agile Mumbai 2020 Conference | How to get the best ROI on Your Test Automati...
Agile Mumbai 2020 Conference |  How to get the best ROI on Your Test Automati...Agile Mumbai 2020 Conference |  How to get the best ROI on Your Test Automati...
Agile Mumbai 2020 Conference | How to get the best ROI on Your Test Automati...
 
Unit testing
Unit testingUnit testing
Unit testing
 
Olli-Pekka Puolitaival - Model-Based Testing for Integration Testing in Real ...
Olli-Pekka Puolitaival - Model-Based Testing for Integration Testing in Real ...Olli-Pekka Puolitaival - Model-Based Testing for Integration Testing in Real ...
Olli-Pekka Puolitaival - Model-Based Testing for Integration Testing in Real ...
 
Introduction to Automated Testing
Introduction to Automated TestingIntroduction to Automated Testing
Introduction to Automated Testing
 
How good are your tests?
How good are your tests?How good are your tests?
How good are your tests?
 
Unit testing (workshop)
Unit testing (workshop)Unit testing (workshop)
Unit testing (workshop)
 
Writing better tests for your java script app
Writing better tests for your java script appWriting better tests for your java script app
Writing better tests for your java script app
 
Unit Testing and Tools
Unit Testing and ToolsUnit Testing and Tools
Unit Testing and Tools
 
Unit Testing and Tools - ADNUG
Unit Testing and Tools - ADNUGUnit Testing and Tools - ADNUG
Unit Testing and Tools - ADNUG
 
Overview of Testing Talks at Pycon
Overview of Testing Talks at PyconOverview of Testing Talks at Pycon
Overview of Testing Talks at Pycon
 
Unit Test + Functional Programming = Love
Unit Test + Functional Programming = LoveUnit Test + Functional Programming = Love
Unit Test + Functional Programming = Love
 
Moq presentation
Moq presentationMoq presentation
Moq presentation
 
Getting Ahead of Delivery Issues with Deep SDLC Analysis by Donald Belcham
Getting Ahead of Delivery Issues with Deep SDLC Analysis by Donald BelchamGetting Ahead of Delivery Issues with Deep SDLC Analysis by Donald Belcham
Getting Ahead of Delivery Issues with Deep SDLC Analysis by Donald Belcham
 
2020-Feb: Testing: Cables and Chains
2020-Feb: Testing: Cables and Chains2020-Feb: Testing: Cables and Chains
2020-Feb: Testing: Cables and Chains
 
Automated Testing but like for PowerShell (April 2012)
Automated Testing but like for PowerShell (April 2012)Automated Testing but like for PowerShell (April 2012)
Automated Testing but like for PowerShell (April 2012)
 
Get Testing with tSQLt - SQL In The City Workshop 2014
Get Testing with tSQLt - SQL In The City Workshop 2014Get Testing with tSQLt - SQL In The City Workshop 2014
Get Testing with tSQLt - SQL In The City Workshop 2014
 
Generalization in Auto-Testing. How we put what we had into new Technological...
Generalization in Auto-Testing. How we put what we had into new Technological...Generalization in Auto-Testing. How we put what we had into new Technological...
Generalization in Auto-Testing. How we put what we had into new Technological...
 
Agile methodologies
Agile methodologiesAgile methodologies
Agile methodologies
 

Similar to Unit Testing talk

Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven DevelopmentMeilan Ou
 
Week 14 Unit Testing.pptx
Week 14  Unit Testing.pptxWeek 14  Unit Testing.pptx
Week 14 Unit Testing.pptxmianshafa
 
Type mock isolator
Type mock isolatorType mock isolator
Type mock isolatorMaslowB
 
Tdd practices
Tdd practicesTdd practices
Tdd practicesaxykim00
 
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
 
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
 
Test Driven Development on Android (Kotlin Kenya)
Test Driven Development on Android (Kotlin Kenya)Test Driven Development on Android (Kotlin Kenya)
Test Driven Development on Android (Kotlin Kenya)Danny Preussler
 
Understanding TDD - theory, practice, techniques and tips.
Understanding TDD - theory, practice, techniques and tips.Understanding TDD - theory, practice, techniques and tips.
Understanding TDD - theory, practice, techniques and tips.Malinda Kapuruge
 
Testing, a pragmatic approach
Testing, a pragmatic approachTesting, a pragmatic approach
Testing, a pragmatic approachEnrico Da Ros
 
An Introduction To Software Development - Test Driven Development, Part 1
An Introduction To Software Development - Test Driven Development, Part 1An Introduction To Software Development - Test Driven Development, Part 1
An Introduction To Software Development - Test Driven Development, Part 1Blue Elephant Consulting
 
Type mock isolator
Type mock isolatorType mock isolator
Type mock isolatorMaslowB
 
TDD and Unit Testing in Golang
TDD and Unit Testing in GolangTDD and Unit Testing in Golang
TDD and Unit Testing in GolangSofian Hadiwijaya
 
An Introduction to Test Driven Development
An Introduction to Test Driven Development An Introduction to Test Driven Development
An Introduction to Test Driven Development CodeOps Technologies LLP
 
TDD and Related Techniques for Non Developers (2012)
TDD and Related Techniques for Non Developers (2012)TDD and Related Techniques for Non Developers (2012)
TDD and Related Techniques for Non Developers (2012)Peter Kofler
 

Similar to Unit Testing talk (20)

Tdd
TddTdd
Tdd
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven Development
 
Week 14 Unit Testing.pptx
Week 14  Unit Testing.pptxWeek 14  Unit Testing.pptx
Week 14 Unit Testing.pptx
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Type mock isolator
Type mock isolatorType mock isolator
Type mock isolator
 
Tdd practices
Tdd practicesTdd practices
Tdd practices
 
Getting started with Test Driven Development
Getting started with Test Driven DevelopmentGetting started with Test Driven Development
Getting started with Test Driven Development
 
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
 
Test Driven Development on Android (Kotlin Kenya)
Test Driven Development on Android (Kotlin Kenya)Test Driven Development on Android (Kotlin Kenya)
Test Driven Development on Android (Kotlin Kenya)
 
Understanding TDD - theory, practice, techniques and tips.
Understanding TDD - theory, practice, techniques and tips.Understanding TDD - theory, practice, techniques and tips.
Understanding TDD - theory, practice, techniques and tips.
 
Testing, a pragmatic approach
Testing, a pragmatic approachTesting, a pragmatic approach
Testing, a pragmatic approach
 
Lecture 21
Lecture 21Lecture 21
Lecture 21
 
An Introduction To Software Development - Test Driven Development, Part 1
An Introduction To Software Development - Test Driven Development, Part 1An Introduction To Software Development - Test Driven Development, Part 1
An Introduction To Software Development - Test Driven Development, Part 1
 
Unit testing
Unit testingUnit testing
Unit testing
 
Type mock isolator
Type mock isolatorType mock isolator
Type mock isolator
 
TDD and Unit Testing in Golang
TDD and Unit Testing in GolangTDD and Unit Testing in Golang
TDD and Unit Testing in Golang
 
An Introduction to Test Driven Development
An Introduction to Test Driven Development An Introduction to Test Driven Development
An Introduction to Test Driven Development
 
TDD and Related Techniques for Non Developers (2012)
TDD and Related Techniques for Non Developers (2012)TDD and Related Techniques for Non Developers (2012)
TDD and Related Techniques for Non Developers (2012)
 

Recently uploaded

BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
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
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
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
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
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
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
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
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....kzayra69
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
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
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 

Recently uploaded (20)

BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
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...
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
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...
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
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
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
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
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
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...
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 

Unit Testing talk

  • 1. Unit Testing: the Clean Tests by Serge Kukharev
  • 2. Unit tests are: • testing one small unit of code at a time • isolated from the other code • fast • clean • the form of documentation • as important as the code itself
  • 3. Unit tests are NOT: • Integration tests (violates speed and isolation rules of unit tests) • Functional tests (violates speed and scope rules)
  • 4. Unit Testing benefits • Helps to be sure about code changes • Helps to prevent bugs • Improves QA speed • Reduces technical dept • Leads to better design
  • 5. –Kent Beck “Tests are the Programmer’s stone, transmuting fear into boredom.”
  • 6. One Unit at a time • One of the most important rules of unit testing. • In scope of a test case means, that the small(est) unit of code is tested: • preferably only one class at a time • sometimes group of classes is OK, if you can call them as one single “unit” • In scope of single test, unit means one method. • This rule helps to be sure that specific unit works properly, as designed. • Makes the tests very fast.
  • 9. Stub Allows to inject desired input for specific test
  • 10. Spy Allows us to spy on the dependencies (should be used very carefully, leads to implementation coupling)
  • 11. Fake Allows to simulate behaviour. Often useful outside of the tests.
  • 12. Mock Allows to set dependency behaviour dynamically. Can act as spy, fake, stub or dummy.
  • 13. Two styles of Unit Testing
  • 14. Classical Focused on testing class with all dependencies. t for dependencies are used mostly, and only dependencies like
  • 15. Classical Pros • Easier to write • Less coupling on the implementation • Less supportive code • Easier to make cleaner.
  • 16. Classical Cons • Ofter tests more than one Unit • Duplicate tests • Not always fast • Problems with dependencies of dependencies • Only applicable when application has proper layers
  • 18. Mockist Pros • Allows to write True Unit tests. • Allows full control over the dependencies. • Usually is faster. • Improves test isolation, leading to easier bug finding.
  • 19. Mockist Cons • Sometimes couples you on the implementation. • More supportive code. • Test will fail more often when refactoring.
  • 20. So, what to choose? Anything will work, pick what you like more.
  • 21. Anything 3rd party should be mocked • DB classes • File system calls • API calls • etc. • External library classes • even framework
  • 22. PHPUnit TestSuit anatomy • TestCase consists of 1 or more tests. • setUp method is used for arranging shared data • tearDown used for cleanup • dataProviders are used to one test multiple times with different data each time • Custom assertements improve readability and bug detection • Other private methods used to improve readability and code reuse.
  • 23. Data Providers • Used to run the same test with multiple different datasets. • Can simplify things • Can make things worse • I prefer to avoid them
  • 24. Clean Code in Unit Tests • Clean tests are as much important as Clean Code itself. • Reasons: • Tests are part of code base. • Test are at least as much important as the code itself. • Tests are documentation. • Bad smells in tests can lead to maintenance costs of supporting those tests.
  • 25. Arrange, Act, Assert rule • This is advanced rule, that helps to write Clean Tests • Every test may consist of 3 parts • Every part should be in specific order • First you arrange your test. You define behaviour of your dependencies. • Next, you actually call the behaviour you are testing. • Finally, you make assortments on that behaviour. • This simple rule leads to much cleaner and readable tests.
  • 26. Custom Assertments • Writing custom assertments lead to reusable test code pieces. • It helps be more specific about what have failed. Compare this two messages: • “Test failed asserting true is false” • “Test failed asserting that SupplierConfig is instance of CarConfig” • This might look like extra work, but in fact it isn’t - PHPStorm automated refactoring can help you do this in 30 seconds.
  • 28. • Test Driven Development is the next step. • Most people think that writing tests is TDD. That’s wrong. • TDD main principle says “You are not allowed to write production code without writing a failing unit test first”.
  • 29. • TDD assures that all written code is covered with tests. • TDD leads to much better design. • TDD is more productive. • TDD leads to a much bigger amount of tests. • TDD leads to a much better quality of tests.
  • 30. TDD: How it works
  • 31. • TDD has three main steps - Red, Green, Refactor • In the Red phase you write a minimal specific failing test. • In the Green phase you implement it. • In the Refactoring phase you see how you can improve existing code. • “Refactoring - to restructure software […] without changing the observable behavior of the software.” - Kent Beck
  • 32. • You continue on, by adding more failing tests. • The more you write your tests, the less specific they are. • “As the tests get more specific, the code gets more generic.” - Robert Martin
  • 34. • TDD is slow. • Managers won’t allow it. • Programmers aren’t testers. • TDD is too hard.
  • 35. Unit Testing resources • “Test Driven Development: by Example” by Kent Beck • “Refactoring” by Martin Fowler • Clean Code video series by Robert Martin (aka Uncle Bob). Ep. #6, 19, 20, 21, 22, 23
  • 36. Thank you! (please give me your feedback, it is really important for me) (c) Serge Kukharev 2015