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

Forcelandia16 - Mythbusters: Test Driven Develo...

Forcelandia16 - Mythbusters: Test Driven Development on Force.com projects

Testing is the key to successful long-term development and is a critical component of the development process. Salesforce's Best Practices strongly recommend that you use a test-driven development process, that is, test development that occurs at the same time as code development. But, how real is this in our projects? what are the long-term benefits? how to encourage developers to think test-first? is this feasible? Join us to discover myths and truths of TDD.

Aldo Fernandez

July 28, 2016

Transcript

31 slides
  1. Agenda • What is TDD? • The TDD process •

    An Apex TDD example • Benefits • Challenges
  2. What is TDD? (I) • It refers to a style

    of programming where three activities are tightly interwoven: • Coding • Testing (in the form of unit tests) • Design (in the form of refactoring)
  3. What is TDD? (II) “TDD is a software development approach

    where tests are written before the functional code” “TDD is a technique of using automated unit tests to drive the design of software and force decoupling of dependencies”
  4. Unit Testing: "Classic" vs "Mockist" styles • Verify that a

    relatively small piece of code is doing what is intended to do • Narrow in scope • Easy to write and execute • Useful for developers • No dependencies on external components • External components are mocked • Test internal consistency • Verify that different pieces of the system works correctly together • Covers multiple components • Require more resource to complete • Useful for business • Dependencies are required because a holistic approach • Test consistency between components
  5. Refactoring • “Refactoring is a controlled technique for improving the

    design of an existing code without changing its external behavior” —Martin Fowler • Needed when “code smells” • Improves non functional attributes • improve readability • reduce complexity • Ideal codebase • Easy to maintain • Easy to extend
  6. An Apex TDD example (I) • Aldo’s Algebra (don’t google,

    it’s just a specification example!) • Given 2 integer numbers (a,b) the sum operation will be a+b • If one of the numbers is null, treat it as zero • Examples • AA.sum(2,3) = 5 • AA.sum(2, null) = 2 • AA.sum(null, null) = 0
  7. Benefits • Forces a good architecture design • Reduce time

    of bug hunting • Write shorter and less complex code • Fewer System.debug() statements ;-) • Instant feedback on broken code • Code coverage on steroids! (go for 100%!) • Enforce KISS and Minium Viable Product concepts • Tests run faster • Mocking around SOQL and DML to avoid declarative changes failures (yes Admin, I’m looking at you!)
  8. Challenges • Learning curve • Cost of TDD vs Project

    Budget • ROI with TDD • Advanced concepts • Dependency Injection • Mocking • Cultural issues • Handling often business requirements changes
  9. Resources + Kudos • Test Driven Development Method in Software

    Development Process. Denis Duka, Lovre Hribar 2010 • Test Driven Development in Apex by Chris Aldridge (@caldrig3) • Unit Testing, Apex Enterprise Patterns and ApexMock, Andrew Fawcett (@andyinthecloud) • Writing true unit tests using dependency injection mocking apex, Alex Tennant (@adtennant) • ApexMocks: How Does It Work? Jesse Altman (@jessealtman) • Inversion of Control Containers and the Dependency Injection Pattern. Martin Fowler, 2004 • Demo Code repo: https://github.com/aldoforce/uysdug_tdd_demo
  10. Q&A

  11. Thank you! Mythbusters: Test Driven Development on Force.com projects Aldo

    Fernandez Technical Architect Litify.com @aldoforce #Forcelandia16 #tdd #dependencyInjection #apex #testing

More Decks by Aldo Fernandez

Other Decks in Programming