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.
3. Safe Harbor
Safe harbor statement under the Private Securities Litigation Reform Act of 1995: This presentation may contain forward-looking statements that involve
risks, uncertainties, and assumptions. If any such uncertainties materialize or if any of the assumptions proves incorrect, the results of salesforce.com,
inc. could differ materially from the results expressed or implied by the forward-looking statements we make. All statements other than statements of
historical fact could be deemed forward-looking, including any projections of subscriber growth, earnings, revenues, or other financial items and any
statements regarding strategies or plans of management for future operations, statements of belief, any statements concerning new, planned, or
upgraded services or technology developments and customer contracts or use of our services.
The risks and uncertainties referred to above include – but are not limited to – risks associated with developing and delivering new functionality for our
service, our new business model, our past operating losses, possible fluctuations in our operating results and rate of growth, interruptions or delays in
our Web hosting, breach of our security measures, risks associated with possible mergers and acquisitions, the immature market in which we operate,
our relatively limited operating history, our ability to expand, retain, and motivate our employees and manage our growth, new releases of our service and
successful customer deployment, our limited history reselling non-salesforce.com products, and utilization and selling to larger enterprise customers.
Further information on potential factors that could affect the financial results of salesforce.com, inc. is included in our annual report on Form 10-K for the
most recent fiscal quarter ended July 31, 2011. This document and others are available on the SEC Filings section of the Investor Information section of
our Web site.
Any unreleased services or features referenced in this or other press releases or public statements are not currently available and may not be delivered
on time or at all. Customers who purchase our services should make the purchase decisions based upon features that are currently available.
Salesforce.com, inc. assumes no obligation and does not intend to update these forward-looking statements.
4. Agenda
▪ What is TDD?
▪ TDD Process
▪ An Apex TDD Example
▪ Benefits
▪ Challenges
5. What is TDD? (I)
▪ …but first, what IS NOT TDD:
▪ TDD is not BDD (Behavior Driven Development)
▪ BDD: specs define tests, and then test defines code implementation
▪ BDD in Apex: Specs -> Test class -> class
▪ Cucumber (cucumber.io) based projects (Pickle, Pepino, …) which runs
acceptance tests defined in Gherkin
▪ …so, what IS TDD?
6. What is TDD? (II)
▪ 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)
7. What is TDD? (III)
“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”
8. The TDD process
1. Write a test
2. Run all tests (the new should fail)
3. Write some code
4. Run tests
5. If tests passes, refactor
6. Repeat step 1
9. Unit Tests vs Integration Tests
▪ Unit Test
▪ Verify that a relatively small piece of
code is doing what is intended to do
▪ Narrow in scope
▪ Easy to write and execute
▪ Test useful for the developer
▪ No dependencies on external
components
▪ External components are mocked or
stubbed out
▪ Test internal consistency
▪ Integration Tests
▪ Verify that different pieces of the
system works correctly together.
▪ Covers whole business
requirements/features
▪ Require more resources to complete
▪ Test useful for business
▪ Dependencies are required because
the holistic approch.
▪ Test consistency between
components
10. An Apex TDD example (I)
▪ Aldo’s Algebra (don’t google… it’s just a trivial 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
20. Benefits
▪ Forces good architecture design
▪ Reduce time of bug hunting
▪ Creates a detailed specification
▪ Write shorter and less complex code
▪ Fewer system.debug() statements ;-)
▪ Instant feedback on broken code
▪ Code coverage on steroids (goodbye less than 75%)
▪ Enforce KISS and YAGNI
▪ Tests run faster
▪ Mocking around SOQL and DML to avoid declarative changes failures o/
21. Challenges
▪ Learning curve
▪ Cost of TDD vs Project Budget
▪ ROI with TDD
▪ Advanced concepts
▪ Dependency Injection
▪ Mocking
▪ Cultural issues
▪ Handling business requirements changes
22. Resources + Kudos
▪ Test Driven Development Method in Software Development Process. Denis Duka, Lovre
Hribar 2010
▪ Test Driven Development in Apex by Chris Aldridge (@caldrig3)
▪ Cucumber (cucumber.io)
▪ Pickle, a lightweight Cucumber implementation for Salesforce.com, github.com/lpoulain/pickle
▪ Pepino, BDD framework for Apex. github.com/pbattisson/pepino
▪ 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
23. Thank You!
Test Driven Development on
Force.com projects
Myths and Truths
Uruguay Salesforce Developer Group
March 10, 2016
#UYSDUG
Aldo Fernandez
Technical Architect
@aldoforce
#uysdug
#tdd
#dependencyInjection
#apex
#testing