SlideShare a Scribd company logo
1 of 44
Download to read offline
"TDD — Are you sure you
      properly test code?"
                        Dmitriy Nesteryuk
                        06 October, 2012


Contacts:
Email: nesterukd@gmail.com
Skype: nest_d
Which kind of tests do we know?

● acceptance
● integration
● unit
TDD Cycle




The schema was taken from http://speakerdeck.com/u/froots/p/testing-your-
backbone-from-the-outside-in
What is the best way to choose?


less acceptance tests, more unit tests

                OR
more acceptance tests, less unit tests
Less acceptance tests, more unit tests

Advantages:
●   faster tests
●   better quality of code
●   better architecture of an application
●   more flexible code


Disadvantages:
●   more time for refactoring
●   more time for implementing new features
More acceptance tests, less unit tests

Advantages:
●   less time for refactoring
●   less time for implementing new features


Disadvantages:
●   slow tests
●   worse quality of code
●   worse architecture of an application
Typical mistakes
Rule #1: Write tests before coding

If you don't follow this rule, you lose:
●   requirements to your code
●   powerful tool for improving your code
●   ~100% code coverage
●   correct tests
Rule #2: Don't test everything

A lot of people tell they stopped writing tests,
because it consumes a lot of time. But, they
don't investigate why it happens to them.

In some cases it happens because they write
needless tests.
Rule #2: Don't test everything




            Example:
Rule #2: Don't test everything
Rule #2: Don't test everything
Rule #2: Don't test everything
Questions to such tests:
●   Why do we have to test DOM methods? Did we write them?
●   What will we have to do if the tag name is changed?
●   What will we have to do if the css class is changed?
Rule #2: Don't test everything
Conclusion:
Test only your own code, it is needless to test libraries/tools which you cannot
control. If something goes wrong with your units in this case,
acceptance/integration tests will catch it. Using this way, you will reduce
amount of tests which you have to support.

Test only logic of your application, is the name of the css class the logic of your
application? Even if it isn't here, your application will continue to work, maybe it
will be displayed a bit ugly, but it will still be working. The same thing we can
say about the tag name.
Rule #3: Test only public API




Some people test code in the way which won't be used in a
   real app. Eventually, they run into a problem with
                 supporting such code.
Rule #3: Test only public API




            Example:
Rule #3: Test only public API
Rule #3: Test only public API
You should not test protected/public methods,
because:
●   You violate encapsulation
●   Private/Protected methods are created to reduce code duplication within
    an object
●   Sometimes private/protected methods are internal helpers
●   You don't call private/protected methods in a real app, therefore, you will
    test your unit in way which won't be used. Is it helpful? You must test your
    units in the same way how it is used in your app.
Rule #3: Test only public API
Rule #3: Test only public API
Conclusion:
To check the same behavior within a few methods, the shared tests can be used.
But... If you have a lot of them, it looks like code smell and you have to think more
about design of your code, may be your class has more than one responsibility
and you have to refactore your class.
Rule #4: Don't create extra getters




 Some people add extra methods/properties for testing
                  purposes only.
Rule #4: Don't create extra getters




            Example:
Rule #4: Don't create extra getters
Rule #4: Don't create extra getters
Rule #4: Don't create extra getters



    We take a look at our code and start thinking:
"Hmm... It looks like I have to tests the fact that 'collection'
          and 'layout' properties contain objects"
Rule #4: Don't create extra getters
Rule #4: Don't create extra getters
Rule #4: Don't create extra getters
Questions occurred in mind:
●   Do I need those getters in a real application?
Rule #4: Don't create extra getters
Rule #4: Don't create extra getters
Conclusion:
Don't create extra methods/properties only to be able to tests your unit.
Eventually, you will have to support them. It will be wasting of your time.
Properties (instance variables) somehow are used within your object, therefore, if
some property (instance variable) won't contain correct value, tests for other
methods (units) will be broken and you will be notified about mistakes in your
code.
Rule #5: Listen to the tests




Some application has a lot of tests, but they have very low
  quality of code, inflexible code, incorrect architecture.
Rule #5: Listen to the tests




            Example:
Rule #5: Listen to the tests
Rule #5: Listen to the tests
Rule #5: Listen to the tests
Question occurred in mind:
●   Why did we have to write almost the same tests?
●   What will happen if we will need to add a new route with similar behavior?
Rule #5: Listen to the tests




 Tests told us about code duplication. They made us be
                           lazy.
  Good developer is a lazy developer. You should be
      interested in solving only unique challenges.
Rule #5: Listen to the tests
Rule #5: Listen to the tests
Rule #5: Listen to the tests



Great!!! But... It only a half of what tests tell us. We have
  not listened to tests where they told us about a few
   responsibilities in our class (prototype since it is
                         JavaScript).
Rule #5: Listen to the tests
Rule #5: Listen to the tests
Rule #5: Listen to the tests
Rule #5: Listen to the tests
Recommended book




"Growing Object-Oriented Software Guided
               by Tests"

More Related Content

What's hot

Code Review: How and When
Code Review: How and WhenCode Review: How and When
Code Review: How and WhenPaul Gower
 
How to get the most out of code reviews
How to get the most out of code reviewsHow to get the most out of code reviews
How to get the most out of code reviewsJavaDayUA
 
Introduction to test programming
Introduction to test programmingIntroduction to test programming
Introduction to test programmingopenfinanceDev
 
Real Life Unit Testing
Real Life Unit TestingReal Life Unit Testing
Real Life Unit TestingDror Helper
 
Code Review Matters and Manners
Code Review Matters and MannersCode Review Matters and Manners
Code Review Matters and MannersTrisha Gee
 
Code review in practice
Code review in practiceCode review in practice
Code review in practiceEdorian
 
How to successfully grow a code review culture
How to successfully grow a code review cultureHow to successfully grow a code review culture
How to successfully grow a code review cultureNina Zakharenko
 
Code Review tool for personal effectiveness and waste analysis
Code Review tool for personal effectiveness and waste analysisCode Review tool for personal effectiveness and waste analysis
Code Review tool for personal effectiveness and waste analysisMikalai Alimenkou
 
Code review
Code reviewCode review
Code reviewdqpi
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven DevelopmentSamnang Chhun
 
Clean code - Getting your R&D on board
Clean code - Getting your R&D on boardClean code - Getting your R&D on board
Clean code - Getting your R&D on boardRuth Sperer
 
Mistakes I have made
Mistakes I have madeMistakes I have made
Mistakes I have madeAndrew Grimm
 
Improving Code Quality Through Effective Review Process
Improving Code Quality Through Effective  Review ProcessImproving Code Quality Through Effective  Review Process
Improving Code Quality Through Effective Review ProcessDr. Syed Hassan Amin
 
OSS Java Analysis - What You Might Be Missing
OSS Java Analysis - What You Might Be MissingOSS Java Analysis - What You Might Be Missing
OSS Java Analysis - What You Might Be MissingCoverity
 
Documenting Code - Patterns and Anti-patterns - NLPW 2016
Documenting Code - Patterns and Anti-patterns - NLPW 2016Documenting Code - Patterns and Anti-patterns - NLPW 2016
Documenting Code - Patterns and Anti-patterns - NLPW 2016Søren Lund
 
Code Review
Code ReviewCode Review
Code Reviewrantav
 
Beyond Unit Testing
Beyond Unit TestingBeyond Unit Testing
Beyond Unit TestingSøren Lund
 

What's hot (19)

Code Review: How and When
Code Review: How and WhenCode Review: How and When
Code Review: How and When
 
Code review
Code reviewCode review
Code review
 
How to get the most out of code reviews
How to get the most out of code reviewsHow to get the most out of code reviews
How to get the most out of code reviews
 
Introduction to test programming
Introduction to test programmingIntroduction to test programming
Introduction to test programming
 
Real Life Unit Testing
Real Life Unit TestingReal Life Unit Testing
Real Life Unit Testing
 
Code Review Matters and Manners
Code Review Matters and MannersCode Review Matters and Manners
Code Review Matters and Manners
 
Code review in practice
Code review in practiceCode review in practice
Code review in practice
 
Code review at large scale
Code review at large scaleCode review at large scale
Code review at large scale
 
How to successfully grow a code review culture
How to successfully grow a code review cultureHow to successfully grow a code review culture
How to successfully grow a code review culture
 
Code Review tool for personal effectiveness and waste analysis
Code Review tool for personal effectiveness and waste analysisCode Review tool for personal effectiveness and waste analysis
Code Review tool for personal effectiveness and waste analysis
 
Code review
Code reviewCode review
Code review
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Clean code - Getting your R&D on board
Clean code - Getting your R&D on boardClean code - Getting your R&D on board
Clean code - Getting your R&D on board
 
Mistakes I have made
Mistakes I have madeMistakes I have made
Mistakes I have made
 
Improving Code Quality Through Effective Review Process
Improving Code Quality Through Effective  Review ProcessImproving Code Quality Through Effective  Review Process
Improving Code Quality Through Effective Review Process
 
OSS Java Analysis - What You Might Be Missing
OSS Java Analysis - What You Might Be MissingOSS Java Analysis - What You Might Be Missing
OSS Java Analysis - What You Might Be Missing
 
Documenting Code - Patterns and Anti-patterns - NLPW 2016
Documenting Code - Patterns and Anti-patterns - NLPW 2016Documenting Code - Patterns and Anti-patterns - NLPW 2016
Documenting Code - Patterns and Anti-patterns - NLPW 2016
 
Code Review
Code ReviewCode Review
Code Review
 
Beyond Unit Testing
Beyond Unit TestingBeyond Unit Testing
Beyond Unit Testing
 

Similar to TDD — Are You Properly Testing Code

[DevDay2018] Let’s all get along. Clean Code please! - By: Christophe K. Ngo,...
[DevDay2018] Let’s all get along. Clean Code please! - By: Christophe K. Ngo,...[DevDay2018] Let’s all get along. Clean Code please! - By: Christophe K. Ngo,...
[DevDay2018] Let’s all get along. Clean Code please! - By: Christophe K. Ngo,...DevDay.org
 
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
 
Test driven development
Test driven developmentTest driven development
Test driven developmentnamkha87
 
assertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
assertYourself - Breaking the Theories and Assumptions of Unit Testing in FlexassertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
assertYourself - Breaking the Theories and Assumptions of Unit Testing in Flexmichael.labriola
 
Unit Test Lab - Why Write Unit Tests?
Unit Test Lab - Why Write Unit Tests?Unit Test Lab - Why Write Unit Tests?
Unit Test Lab - Why Write Unit Tests?Danny van Kasteel
 
A Brief Introduction to Test-Driven Development
A Brief Introduction to Test-Driven DevelopmentA Brief Introduction to Test-Driven Development
A Brief Introduction to Test-Driven DevelopmentShawn Jones
 
Unit testing
Unit testingUnit testing
Unit testingPiXeL16
 
An Introduction To Software Development - Final Review
An Introduction To Software Development - Final ReviewAn Introduction To Software Development - Final Review
An Introduction To Software Development - Final ReviewBlue Elephant Consulting
 
Refactoring Legacy Code - true story
Refactoring Legacy Code - true storyRefactoring Legacy Code - true story
Refactoring Legacy Code - true storyAki Salmi
 
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
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven DevelopmentZendCon
 
Clean code: understanding Boundaries and Unit Tests
Clean code: understanding Boundaries and Unit TestsClean code: understanding Boundaries and Unit Tests
Clean code: understanding Boundaries and Unit Testsradin reth
 
Clean Code - Part 2
Clean Code - Part 2Clean Code - Part 2
Clean Code - Part 2Knoldus Inc.
 
Alexandru Bolboaca - Unit Testing from the Trenches
Alexandru Bolboaca - Unit Testing from the TrenchesAlexandru Bolboaca - Unit Testing from the Trenches
Alexandru Bolboaca - Unit Testing from the TrenchesMozaic Works
 
5 reasons you'll love to hate Agile Development
5 reasons you'll love to hate Agile Development5 reasons you'll love to hate Agile Development
5 reasons you'll love to hate Agile DevelopmentArin Sime
 
Unit Testing Best Practices
Unit Testing Best PracticesUnit Testing Best Practices
Unit Testing Best PracticesTomaš Maconko
 

Similar to TDD — Are You Properly Testing Code (20)

[DevDay2018] Let’s all get along. Clean Code please! - By: Christophe K. Ngo,...
[DevDay2018] Let’s all get along. Clean Code please! - By: Christophe K. Ngo,...[DevDay2018] Let’s all get along. Clean Code please! - By: Christophe K. Ngo,...
[DevDay2018] Let’s all get along. Clean Code please! - By: Christophe K. Ngo,...
 
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
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
assertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
assertYourself - Breaking the Theories and Assumptions of Unit Testing in FlexassertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
assertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
 
Unit Test Lab - Why Write Unit Tests?
Unit Test Lab - Why Write Unit Tests?Unit Test Lab - Why Write Unit Tests?
Unit Test Lab - Why Write Unit Tests?
 
A Brief Introduction to Test-Driven Development
A Brief Introduction to Test-Driven DevelopmentA Brief Introduction to Test-Driven Development
A Brief Introduction to Test-Driven Development
 
Unit testing
Unit testingUnit testing
Unit testing
 
An Introduction To Software Development - Final Review
An Introduction To Software Development - Final ReviewAn Introduction To Software Development - Final Review
An Introduction To Software Development - Final Review
 
Refactoring Legacy Code - true story
Refactoring Legacy Code - true storyRefactoring Legacy Code - true story
Refactoring Legacy Code - true story
 
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
 
Software testing
Software testingSoftware testing
Software testing
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Intro to TDD
Intro to TDDIntro to TDD
Intro to TDD
 
Clean code: understanding Boundaries and Unit Tests
Clean code: understanding Boundaries and Unit TestsClean code: understanding Boundaries and Unit Tests
Clean code: understanding Boundaries and Unit Tests
 
Clean Code - Part 2
Clean Code - Part 2Clean Code - Part 2
Clean Code - Part 2
 
Alexandru Bolboaca - Unit Testing from the Trenches
Alexandru Bolboaca - Unit Testing from the TrenchesAlexandru Bolboaca - Unit Testing from the Trenches
Alexandru Bolboaca - Unit Testing from the Trenches
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
5 reasons you'll love to hate Agile Development
5 reasons you'll love to hate Agile Development5 reasons you'll love to hate Agile Development
5 reasons you'll love to hate Agile Development
 
Unit Testing Best Practices
Unit Testing Best PracticesUnit Testing Best Practices
Unit Testing Best Practices
 

Recently uploaded

Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxShobhayan Kirtania
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...Pooja Nehwal
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...anjaliyadav012327
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 

Recently uploaded (20)

Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 

TDD — Are You Properly Testing Code

  • 1. "TDD — Are you sure you properly test code?" Dmitriy Nesteryuk 06 October, 2012 Contacts: Email: nesterukd@gmail.com Skype: nest_d
  • 2. Which kind of tests do we know? ● acceptance ● integration ● unit
  • 3. TDD Cycle The schema was taken from http://speakerdeck.com/u/froots/p/testing-your- backbone-from-the-outside-in
  • 4. What is the best way to choose? less acceptance tests, more unit tests OR more acceptance tests, less unit tests
  • 5. Less acceptance tests, more unit tests Advantages: ● faster tests ● better quality of code ● better architecture of an application ● more flexible code Disadvantages: ● more time for refactoring ● more time for implementing new features
  • 6. More acceptance tests, less unit tests Advantages: ● less time for refactoring ● less time for implementing new features Disadvantages: ● slow tests ● worse quality of code ● worse architecture of an application
  • 8. Rule #1: Write tests before coding If you don't follow this rule, you lose: ● requirements to your code ● powerful tool for improving your code ● ~100% code coverage ● correct tests
  • 9. Rule #2: Don't test everything A lot of people tell they stopped writing tests, because it consumes a lot of time. But, they don't investigate why it happens to them. In some cases it happens because they write needless tests.
  • 10. Rule #2: Don't test everything Example:
  • 11. Rule #2: Don't test everything
  • 12. Rule #2: Don't test everything
  • 13. Rule #2: Don't test everything Questions to such tests: ● Why do we have to test DOM methods? Did we write them? ● What will we have to do if the tag name is changed? ● What will we have to do if the css class is changed?
  • 14. Rule #2: Don't test everything Conclusion: Test only your own code, it is needless to test libraries/tools which you cannot control. If something goes wrong with your units in this case, acceptance/integration tests will catch it. Using this way, you will reduce amount of tests which you have to support. Test only logic of your application, is the name of the css class the logic of your application? Even if it isn't here, your application will continue to work, maybe it will be displayed a bit ugly, but it will still be working. The same thing we can say about the tag name.
  • 15. Rule #3: Test only public API Some people test code in the way which won't be used in a real app. Eventually, they run into a problem with supporting such code.
  • 16. Rule #3: Test only public API Example:
  • 17. Rule #3: Test only public API
  • 18. Rule #3: Test only public API You should not test protected/public methods, because: ● You violate encapsulation ● Private/Protected methods are created to reduce code duplication within an object ● Sometimes private/protected methods are internal helpers ● You don't call private/protected methods in a real app, therefore, you will test your unit in way which won't be used. Is it helpful? You must test your units in the same way how it is used in your app.
  • 19. Rule #3: Test only public API
  • 20. Rule #3: Test only public API Conclusion: To check the same behavior within a few methods, the shared tests can be used. But... If you have a lot of them, it looks like code smell and you have to think more about design of your code, may be your class has more than one responsibility and you have to refactore your class.
  • 21. Rule #4: Don't create extra getters Some people add extra methods/properties for testing purposes only.
  • 22. Rule #4: Don't create extra getters Example:
  • 23. Rule #4: Don't create extra getters
  • 24. Rule #4: Don't create extra getters
  • 25. Rule #4: Don't create extra getters We take a look at our code and start thinking: "Hmm... It looks like I have to tests the fact that 'collection' and 'layout' properties contain objects"
  • 26. Rule #4: Don't create extra getters
  • 27. Rule #4: Don't create extra getters
  • 28. Rule #4: Don't create extra getters Questions occurred in mind: ● Do I need those getters in a real application?
  • 29. Rule #4: Don't create extra getters
  • 30. Rule #4: Don't create extra getters Conclusion: Don't create extra methods/properties only to be able to tests your unit. Eventually, you will have to support them. It will be wasting of your time. Properties (instance variables) somehow are used within your object, therefore, if some property (instance variable) won't contain correct value, tests for other methods (units) will be broken and you will be notified about mistakes in your code.
  • 31. Rule #5: Listen to the tests Some application has a lot of tests, but they have very low quality of code, inflexible code, incorrect architecture.
  • 32. Rule #5: Listen to the tests Example:
  • 33. Rule #5: Listen to the tests
  • 34. Rule #5: Listen to the tests
  • 35. Rule #5: Listen to the tests Question occurred in mind: ● Why did we have to write almost the same tests? ● What will happen if we will need to add a new route with similar behavior?
  • 36. Rule #5: Listen to the tests Tests told us about code duplication. They made us be lazy. Good developer is a lazy developer. You should be interested in solving only unique challenges.
  • 37. Rule #5: Listen to the tests
  • 38. Rule #5: Listen to the tests
  • 39. Rule #5: Listen to the tests Great!!! But... It only a half of what tests tell us. We have not listened to tests where they told us about a few responsibilities in our class (prototype since it is JavaScript).
  • 40. Rule #5: Listen to the tests
  • 41. Rule #5: Listen to the tests
  • 42. Rule #5: Listen to the tests
  • 43. Rule #5: Listen to the tests
  • 44. Recommended book "Growing Object-Oriented Software Guided by Tests"