SlideShare uma empresa Scribd logo
1 de 18
TEST DRIVEN DEVELOPMENT
Content


 1. Definition of Unit Test and benefits of writing unit
    tests.
 2. TDD introduction, methodology and benefits.
 3. Effective use of TDD.
 4. Applying TDD to our project.
What is a “Unit Test”?


  A "unit" is a piece of code, method or function that
  has a single responsibility.
  A "unit test" is a piece of code that invokes another
  piece of code and checks the correctness of some
  assumptions. If the assumptions turn out to be
  wrong, the unit test has failed.
Unit Test Benefits
  compare to Integration Test




             Integration Test                                 Unit Test
• A integration test group 2 or more        • A unit test tests only a single unit in
  dependent units and test to see if they     isolation
  work properly together.
• Integration tests are harder to write,    • Unit tests are easy to implement, they
  easy to fail if they have an external       works with fake dependencies(Stub and
  dependency which is unavailable for         Mock), run and give results quickly
  some reason
• Integration tests are hard to maintain.   • Unit tests are super easy to maintain
  When a test fail, we have to debug to       because they cover a small piece of
  see what is our problem.                    functionality.
• Integration tests help to document        • Unit tests help to design and document
  main features of our application.           our code.
TDD Introduction


  Traditional approach: many people feel that the best time
  to write unit tests for software is after the software has
  been written. It sounds reasonable but there is code we
  can’t write unit test without modifying. There’re many
  cases we have to modify our code to make it unit-testable,
  which may lead to other problems, slow down our
  schedule…
  Test Driven Development: Write tests first, use unit tests
  drive the design and document our code. With TDD, we
  begin with the end in mind.
TDD Methodology


  The general TDD cycle goes as follows:
  1.   Write a failing test first to prove code or functionality is
       missing. The test will fail because we haven't implemented
       that functionality yet.
  2.   Make the test pass by writing production code that meets
       the expectations of your test. At that moment, we just need
       to pass the test, the code should be as simple as possible.
  3.   Refactor your code when the test passes, make it more
       readable, remove code duplication...
  By writing and passing unit tests, we add more
  functionality, a bit at a time until all requirements are
  fulfilled.
TDD Benefits


  Writing unit tests actively will make most of our code are
  covered by unit tests. That will raise the confidence in the
  code.
  Help to create quality code, better designs, document our
  code....Write test first help us really understand the design
  of code. Instead of writing code to do something, we're
  starting by outlining all conditions we may have, all
  expected output.
  Speed up our development in a long run. We done when
  we finish the last line of code, we don't need to spend
  much time debugging, we don't need to manually check if
  our new features break the old code, existing features.
TDD Benefits
Effective use of TDD


TDD brings us a lot of benefits.          TDD = Too
However, there is no silver bullet, TDD     Damn
doesn’t ensure project success. On the     Difficult
other hand, TDD is a double-edges
sword and if it is done incorrectly, it
will waste our time, lower our code
quality.
In order to apply TDD effectively, we
have to know exactly what to test and
how to write a good unit test.
What to test?


  Any classes and components that contain logical
  code, e.g. if…else, for, switch…case, any calculation
  or interaction with other components, calls to
  external methods.
  Logical code could be found on server side or client
  size. We have to test them all.
Unit testing on server side


   Repository Layer: verify the persistence and retrieval
   implementations produce the correct results.
   Business Logic Layer: focus on verifying the business rules
   and their interactions with Repository Layer. The tests do
   not actually store data in the repository but use a fake
   repository and verify the business services layers uses it
   correctly.
   Web Layer: we create unit test for request handlers, verify
   their interactions with business service, e.g., "Are they
   called correctly?", "Are the view models returned
   correctly?"
Unit testing on client side


   The basic rule is to test anything a designer would not
   change. We would not test that a specific font value
   was set or a specific background color of an element
   was used.
   We test the property values of a view model, whether
   a calculation is correct, an event is triggered, a DOM
   element are modified correctly, a AJAX callback
   function has been called or not...
How to write a good unit test?


 A good unit test must be simple, trustworthy, maintainable and
 readable.
   Naming unit test:
   MethodUnderTest_Scenario_ExpectedBehavior()
    ex: AuthorizeUser_WrongUserName_ReturnFalse()
   Avoid logic in tests: having logic will increase chances of having
   bugs in unit tests. avoid "if, switch, for..." to generate input, try
   to hard code.
   Testing only one thing, avoid testing multiple aspects: each unit
   test should generally take one action and make one assertion. if
   our tests contain more than a single assert and the first assert
   fails, it will throw exception and the second assert never run.
How to write a good unit test?


  Avoid duplication: put duplicated code in TestCleanup and
  TestInitialize method for maintainable reason.
  Don't test things that obviously work.
  Enforce test isolation: a test should always run in its little
  world, isolated from its dependencies. We do not want to
  think our subject under test is broken because one of its
  dependencies broke and caused the subject under test to
  fail. If We test like this, We might spend a lot of time
  tracking down bugs in the wrong place. Testing how things
  operate together is the domain of integration testing, not
  unit testing.
Applying TDD to our project


  Server side: We still have not implemented business
  services yet, so, most of code reside in action’s
  Execute methods. In those methods, we don’t care
  about what business services do or what they return,
  we only need to test, if we have to, whether they
  have been called correctly or not.
Applying TDD to our project


 On client side, we have 3 layers that contain logic:
   View Model: verify all behaviors(CopyUser, LoadUser…),
   computed properties
   Repository: verify whether the call to server has been
   made and callback function has been triggered correctly or
   not.
   Presenter: most of our UI logic resides here. We should
   write unit test to verify circumstances, e.g. whether a
   button is enabled/disabled, a list is empty, selected item
   changed, user click Save button.
How to write unit-testable code?


Just need to remember: “objects do not create other objects on
which they rely to do their work. Instead, they get the objects that
they need from an outside source”.

We have 4 common ways to achieve this: Constructor, Setter and
Getter, Interface, Service Locator(factory pattern, local factory
method).

All necessary techniques can be found in:
   The Art of Unit Testing – Manning
   Test-driven Development By Example - Addison Wesley
   Dependency Injection in .NET - Manning
Q&A

Mais conteúdo relacionado

Mais procurados

Unit testing & TDD concepts with best practice guidelines.
Unit testing & TDD concepts with best practice guidelines.Unit testing & TDD concepts with best practice guidelines.
Unit testing & TDD concepts with best practice guidelines.Mohamed Taman
 
Selenium Testing Project report
Selenium Testing Project reportSelenium Testing Project report
Selenium Testing Project reportKapil Rajpurohit
 
Qa mockup interview for automation testing
Qa mockup interview for automation testingQa mockup interview for automation testing
Qa mockup interview for automation testingKadharBashaJ
 
Manual testing interview question by INFOTECH
Manual testing interview question by INFOTECHManual testing interview question by INFOTECH
Manual testing interview question by INFOTECHPravinsinh
 
Tutorial of web application load testing in selinium
Tutorial of web application load testing in seliniumTutorial of web application load testing in selinium
Tutorial of web application load testing in seliniumKevalkumar Shah
 
Automated Testing vs Manual Testing
Automated Testing vs Manual TestingAutomated Testing vs Manual Testing
Automated Testing vs Manual Testingdidev
 
Software testing-in-gurgaon
Software testing-in-gurgaonSoftware testing-in-gurgaon
Software testing-in-gurgaonAP EDUSOFT
 
Learning's from mobile testing
Learning's from mobile testingLearning's from mobile testing
Learning's from mobile testingVikrant Chauhan
 
Software testing and quality assurance
Software testing and quality assuranceSoftware testing and quality assurance
Software testing and quality assuranceBenjamin Baumann
 
Testing Experience - Evolution of Test Automation Frameworks
Testing Experience - Evolution of Test Automation FrameworksTesting Experience - Evolution of Test Automation Frameworks
Testing Experience - Evolution of Test Automation FrameworksŁukasz Morawski
 
Automation testing interview pdf org
Automation testing interview pdf orgAutomation testing interview pdf org
Automation testing interview pdf orgTestbytes
 
Software Testing without Requirements: Survival Guide
Software Testing without Requirements: Survival GuideSoftware Testing without Requirements: Survival Guide
Software Testing without Requirements: Survival GuideOleksandr Lutsaievskyi
 
Tech talks annual 2015 izzet mustafayev_performance testing - the way to make...
Tech talks annual 2015 izzet mustafayev_performance testing - the way to make...Tech talks annual 2015 izzet mustafayev_performance testing - the way to make...
Tech talks annual 2015 izzet mustafayev_performance testing - the way to make...TechTalks
 
Testing concept definition
Testing concept definitionTesting concept definition
Testing concept definitionVivek V
 
Testing a GPS application | Testbytes
Testing a GPS application | TestbytesTesting a GPS application | Testbytes
Testing a GPS application | TestbytesTestbytes
 
Unit 1 basic concepts of testing & quality
Unit 1   basic concepts of testing & qualityUnit 1   basic concepts of testing & quality
Unit 1 basic concepts of testing & qualityravikhimani1984
 
Capital One DevOps Case Study: A Bank with the Heart of Tech Company
Capital One DevOps Case Study: A Bank with the Heart of Tech CompanyCapital One DevOps Case Study: A Bank with the Heart of Tech Company
Capital One DevOps Case Study: A Bank with the Heart of Tech CompanySimform
 

Mais procurados (20)

Unit testing & TDD concepts with best practice guidelines.
Unit testing & TDD concepts with best practice guidelines.Unit testing & TDD concepts with best practice guidelines.
Unit testing & TDD concepts with best practice guidelines.
 
Selenium Testing Project report
Selenium Testing Project reportSelenium Testing Project report
Selenium Testing Project report
 
Qa mockup interview for automation testing
Qa mockup interview for automation testingQa mockup interview for automation testing
Qa mockup interview for automation testing
 
Formal method
Formal methodFormal method
Formal method
 
Manual testing interview question by INFOTECH
Manual testing interview question by INFOTECHManual testing interview question by INFOTECH
Manual testing interview question by INFOTECH
 
Tutorial of web application load testing in selinium
Tutorial of web application load testing in seliniumTutorial of web application load testing in selinium
Tutorial of web application load testing in selinium
 
Automated Testing vs Manual Testing
Automated Testing vs Manual TestingAutomated Testing vs Manual Testing
Automated Testing vs Manual Testing
 
Software testing-in-gurgaon
Software testing-in-gurgaonSoftware testing-in-gurgaon
Software testing-in-gurgaon
 
Learning's from mobile testing
Learning's from mobile testingLearning's from mobile testing
Learning's from mobile testing
 
Software testing and quality assurance
Software testing and quality assuranceSoftware testing and quality assurance
Software testing and quality assurance
 
Test plan
Test planTest plan
Test plan
 
Testing Experience - Evolution of Test Automation Frameworks
Testing Experience - Evolution of Test Automation FrameworksTesting Experience - Evolution of Test Automation Frameworks
Testing Experience - Evolution of Test Automation Frameworks
 
Automation testing interview pdf org
Automation testing interview pdf orgAutomation testing interview pdf org
Automation testing interview pdf org
 
Software Testing without Requirements: Survival Guide
Software Testing without Requirements: Survival GuideSoftware Testing without Requirements: Survival Guide
Software Testing without Requirements: Survival Guide
 
Tech talks annual 2015 izzet mustafayev_performance testing - the way to make...
Tech talks annual 2015 izzet mustafayev_performance testing - the way to make...Tech talks annual 2015 izzet mustafayev_performance testing - the way to make...
Tech talks annual 2015 izzet mustafayev_performance testing - the way to make...
 
Testing concept definition
Testing concept definitionTesting concept definition
Testing concept definition
 
Testing a GPS application | Testbytes
Testing a GPS application | TestbytesTesting a GPS application | Testbytes
Testing a GPS application | Testbytes
 
Manual Testing.
Manual Testing.Manual Testing.
Manual Testing.
 
Unit 1 basic concepts of testing & quality
Unit 1   basic concepts of testing & qualityUnit 1   basic concepts of testing & quality
Unit 1 basic concepts of testing & quality
 
Capital One DevOps Case Study: A Bank with the Heart of Tech Company
Capital One DevOps Case Study: A Bank with the Heart of Tech CompanyCapital One DevOps Case Study: A Bank with the Heart of Tech Company
Capital One DevOps Case Study: A Bank with the Heart of Tech Company
 

Semelhante a Test driven development

Unit Testing Full@
Unit Testing Full@Unit Testing Full@
Unit Testing Full@Alex Borsuk
 
Test-Driven Development In Action
Test-Driven Development In ActionTest-Driven Development In Action
Test-Driven Development In ActionJon Kruger
 
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
 
Automated Unit Testing and TDD
Automated Unit Testing and TDDAutomated Unit Testing and TDD
Automated Unit Testing and TDDGreg Sohl
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Developmentbhochhi
 
Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven Developmentadrianmitev
 
Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven DevelopmentEffective
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven DevelopmentJohn Blanco
 
Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven DevelopmentEffectiveUI
 
TDD and Unit Testing in Golang
TDD and Unit Testing in GolangTDD and Unit Testing in Golang
TDD and Unit Testing in GolangSofian Hadiwijaya
 
Understanding Unit Testing
Understanding Unit TestingUnderstanding Unit Testing
Understanding Unit Testingikhwanhayat
 

Semelhante a Test driven development (20)

Test driven development(tdd)
Test driven development(tdd)Test driven development(tdd)
Test driven development(tdd)
 
Unit Testing Full@
Unit Testing Full@Unit Testing Full@
Unit Testing Full@
 
Test-Driven Development In Action
Test-Driven Development In ActionTest-Driven Development In Action
Test-Driven Development In Action
 
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
 
Automated Unit Testing and TDD
Automated Unit Testing and TDDAutomated Unit Testing and TDD
Automated Unit Testing and TDD
 
TDD Workshop UTN 2012
TDD Workshop UTN 2012TDD Workshop UTN 2012
TDD Workshop UTN 2012
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven Development
 
Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven Development
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven Development
 
TDD and Unit Testing in Golang
TDD and Unit Testing in GolangTDD and Unit Testing in Golang
TDD and Unit Testing in Golang
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Understanding Unit Testing
Understanding Unit TestingUnderstanding Unit Testing
Understanding Unit Testing
 
Ian Cooper webinar for DDD Iran: Kent beck style tdd seven years after
Ian Cooper webinar for DDD Iran: Kent beck style tdd   seven years afterIan Cooper webinar for DDD Iran: Kent beck style tdd   seven years after
Ian Cooper webinar for DDD Iran: Kent beck style tdd seven years after
 
TestDrivenDeveloment
TestDrivenDevelomentTestDrivenDeveloment
TestDrivenDeveloment
 
Why Unit Testingl
Why Unit TestinglWhy Unit Testingl
Why Unit Testingl
 
Why unit testingl
Why unit testinglWhy unit testingl
Why unit testingl
 
Why Unit Testingl
Why Unit TestinglWhy Unit Testingl
Why Unit Testingl
 
Unit testing - An introduction
Unit testing - An introductionUnit testing - An introduction
Unit testing - An introduction
 

Último

ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfhans926745
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 

Último (20)

ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 

Test driven development

  • 2. Content 1. Definition of Unit Test and benefits of writing unit tests. 2. TDD introduction, methodology and benefits. 3. Effective use of TDD. 4. Applying TDD to our project.
  • 3. What is a “Unit Test”? A "unit" is a piece of code, method or function that has a single responsibility. A "unit test" is a piece of code that invokes another piece of code and checks the correctness of some assumptions. If the assumptions turn out to be wrong, the unit test has failed.
  • 4. Unit Test Benefits compare to Integration Test Integration Test Unit Test • A integration test group 2 or more • A unit test tests only a single unit in dependent units and test to see if they isolation work properly together. • Integration tests are harder to write, • Unit tests are easy to implement, they easy to fail if they have an external works with fake dependencies(Stub and dependency which is unavailable for Mock), run and give results quickly some reason • Integration tests are hard to maintain. • Unit tests are super easy to maintain When a test fail, we have to debug to because they cover a small piece of see what is our problem. functionality. • Integration tests help to document • Unit tests help to design and document main features of our application. our code.
  • 5. TDD Introduction Traditional approach: many people feel that the best time to write unit tests for software is after the software has been written. It sounds reasonable but there is code we can’t write unit test without modifying. There’re many cases we have to modify our code to make it unit-testable, which may lead to other problems, slow down our schedule… Test Driven Development: Write tests first, use unit tests drive the design and document our code. With TDD, we begin with the end in mind.
  • 6. TDD Methodology The general TDD cycle goes as follows: 1. Write a failing test first to prove code or functionality is missing. The test will fail because we haven't implemented that functionality yet. 2. Make the test pass by writing production code that meets the expectations of your test. At that moment, we just need to pass the test, the code should be as simple as possible. 3. Refactor your code when the test passes, make it more readable, remove code duplication... By writing and passing unit tests, we add more functionality, a bit at a time until all requirements are fulfilled.
  • 7. TDD Benefits Writing unit tests actively will make most of our code are covered by unit tests. That will raise the confidence in the code. Help to create quality code, better designs, document our code....Write test first help us really understand the design of code. Instead of writing code to do something, we're starting by outlining all conditions we may have, all expected output. Speed up our development in a long run. We done when we finish the last line of code, we don't need to spend much time debugging, we don't need to manually check if our new features break the old code, existing features.
  • 9. Effective use of TDD TDD brings us a lot of benefits. TDD = Too However, there is no silver bullet, TDD Damn doesn’t ensure project success. On the Difficult other hand, TDD is a double-edges sword and if it is done incorrectly, it will waste our time, lower our code quality. In order to apply TDD effectively, we have to know exactly what to test and how to write a good unit test.
  • 10. What to test? Any classes and components that contain logical code, e.g. if…else, for, switch…case, any calculation or interaction with other components, calls to external methods. Logical code could be found on server side or client size. We have to test them all.
  • 11. Unit testing on server side Repository Layer: verify the persistence and retrieval implementations produce the correct results. Business Logic Layer: focus on verifying the business rules and their interactions with Repository Layer. The tests do not actually store data in the repository but use a fake repository and verify the business services layers uses it correctly. Web Layer: we create unit test for request handlers, verify their interactions with business service, e.g., "Are they called correctly?", "Are the view models returned correctly?"
  • 12. Unit testing on client side The basic rule is to test anything a designer would not change. We would not test that a specific font value was set or a specific background color of an element was used. We test the property values of a view model, whether a calculation is correct, an event is triggered, a DOM element are modified correctly, a AJAX callback function has been called or not...
  • 13. How to write a good unit test? A good unit test must be simple, trustworthy, maintainable and readable. Naming unit test: MethodUnderTest_Scenario_ExpectedBehavior() ex: AuthorizeUser_WrongUserName_ReturnFalse() Avoid logic in tests: having logic will increase chances of having bugs in unit tests. avoid "if, switch, for..." to generate input, try to hard code. Testing only one thing, avoid testing multiple aspects: each unit test should generally take one action and make one assertion. if our tests contain more than a single assert and the first assert fails, it will throw exception and the second assert never run.
  • 14. How to write a good unit test? Avoid duplication: put duplicated code in TestCleanup and TestInitialize method for maintainable reason. Don't test things that obviously work. Enforce test isolation: a test should always run in its little world, isolated from its dependencies. We do not want to think our subject under test is broken because one of its dependencies broke and caused the subject under test to fail. If We test like this, We might spend a lot of time tracking down bugs in the wrong place. Testing how things operate together is the domain of integration testing, not unit testing.
  • 15. Applying TDD to our project Server side: We still have not implemented business services yet, so, most of code reside in action’s Execute methods. In those methods, we don’t care about what business services do or what they return, we only need to test, if we have to, whether they have been called correctly or not.
  • 16. Applying TDD to our project On client side, we have 3 layers that contain logic: View Model: verify all behaviors(CopyUser, LoadUser…), computed properties Repository: verify whether the call to server has been made and callback function has been triggered correctly or not. Presenter: most of our UI logic resides here. We should write unit test to verify circumstances, e.g. whether a button is enabled/disabled, a list is empty, selected item changed, user click Save button.
  • 17. How to write unit-testable code? Just need to remember: “objects do not create other objects on which they rely to do their work. Instead, they get the objects that they need from an outside source”. We have 4 common ways to achieve this: Constructor, Setter and Getter, Interface, Service Locator(factory pattern, local factory method). All necessary techniques can be found in: The Art of Unit Testing – Manning Test-driven Development By Example - Addison Wesley Dependency Injection in .NET - Manning
  • 18. Q&A