Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Test Driven Development: Workshop

Test Driven Development: Workshop

An introduction to TDD. covering the principles of TDD with tasks available in

PHP: https://github.com/braddle/tdd-workshop-php
Go: https://github.com/braddle/tdd-workshop-go
Java: https://github.com/braddle/tdd-workshop-java

Mark Bradley

October 02, 2017
Tweet

More Decks by Mark Bradley

Other Decks in Technology

Transcript

  1. @braddle Principal Software Engineer @ Sainsbury’s sporadic blogger @ mark-bradley.net

    Husband to Miranda Father to Florence, Matilda & Rowan Collector ofCDs and Cook Books Mark Bradley
  2. @braddle What will we cover? • What is Test Driven

    Development and why use it • The different types of tests • Writing our first tests • Refactoring • Test Doubles (Mocks, Stubs, Fakes, Spies, Dummies) • Test Quality
  3. @braddle What is Test Driven Development? • Writing tests before

    you write code • Using tests to design how an application should work • Not just unit tests
  4. @braddle Good Practices • Object Oriented Programming (OOP) ◦ Encapsulation

    ◦ Inheritance ◦ Polymorphism ◦ Interfaces • SOLID Principles ◦ Single Responsibility ◦ Open/Closed ◦ Liskov Substitution ◦ Interface Segregation ◦ Dependency Inversion • Hexagonal Architecture
  5. @braddle Testing Pyramid End 2 End Integration Unit Test a

    few parts of application together Uses mocked external dependencies Test a single function Use mocking for dependencies Only a few / possible only one test Run through the core journey Make use of actual external dependencies
  6. @braddle End to End Tests • Flows through you application

    from start to finish • Uses all really services ◦ Database ◦ Email ◦ … • Only made up of one or two tests ◦ Core user journeys • May interact with many different parts of you app in a single test ◦ Customer applications ◦ Admin applications • May require some seeding of external services • Could be behavioural tests
  7. @braddle Integration Tests • Tests a small number of units

    together • Ensure the different units work together as expected • Mocks external dependencies ◦ Database ◦ Email ◦ …. • Could be behavioural Tests
  8. @braddle Unit Tests • Tests a single unit/function (Targeted) •

    Mocks any dependencies of the class and function (Isolated) • Unit test should be able to run over and over in any order without the result changing (Repeatable & predictable)
  9. @braddle Other Types of Tests • API Contract Testing •

    Security • Load (Volume and Performance)
  10. @braddle The Test Driven Development Cycle 1. Write a test

    2. Run tests (Latest test should fail || error) 3. Write code (Just enough code for the failing test to pass or error to go away) 4. Run test a. Test passed -> Continue b. Test Fails || New Error -> return to 3 5. Refactor (if required) REPEAT
  11. @braddle Why use Test Driven Development • Fewer defects in

    your code • Only implement what is required • Increased code quality
  12. @braddle Bug Report When I have an Integer and I

    divide By 0. The universe implodes.
  13. @braddle Test Doubles • Imitating the functionality of dependencies •

    Allowing for isolation of unit and integration tests • Do not necessarily require a mocking framework • Interfaces start becoming very important • Lots of different types of test double
  14. @braddle Mutation Testing Testing the your test by making changes

    to the logic within the code you are testing. PHP: Humbug (https://github.com/humbug/humbug) Java: PIT (http://pitest.org/) Go: Mutesting (https://github.com/zimmski/go-mutesting)
  15. @braddle Requirement #1 Feature: Customer Satisfation Index As A Store

    Manager I want my customers to be able to give simple feedback on their shopping experience So that I can gage how changes I make to the store affect customer satisfaction Scenario: When a customer gives a positive feedback Then the customer should be thanked for their feedback And the positive feedback count should have increased by 1 Scenario: When a customer gives a negative feedback Then the customer should be thanked for their feedback And the negative feedback count should have increased by 1 Scenario: When a customer gives a neutral feedback Then the customer should be thanked for their feedback
  16. @braddle Resources • 30 Days of TDD by James Bender

    (@jamesbender) ◦ http://www.telerik.com/blogs/30-days-tdd-day-one-what-is-tdd • Code Coverage: Testing Private Functions (Me) ◦ http://mark-bradley.net/2017/03/11/code-coverage-testing-private-functions/ • SOLID Principles (Billie Thompson @PurpleBooth) ◦ https://purplebooth.co.uk/blog/2015/02/23/s-is-for-single-responsibility-principle/ • Speaker notes in these slides ◦ More in depth note about the slide ◦ Links to related articles