Rethinking Testing
 towards your needs




            pablo@cuboxsa.com
eXtreme Programming




    Rspec
          Testing
     TestUnit

                    jUnit


Test Driven Development
                            Crash Test
“Our highest priority is to satisfy
 the customer through the early
 and continuous DELIVERY of
 VALUABLE software.”




                         The agile manifesto
The Customer

       Working
        Code




 On              Low
Time             Cost
The Needs of Customer
Working Code   Acceptance Testing
Public Domain Wikimedia Commons
Design by Contract
• by B. Meyer
• Code enforces a contract
• Language provides constraints
  – Before function call (Preconditions)
  – After function call (Post Conditions)
  – During all the function call (Invariants)
User.create! params[:user]
Pre conditions:
   params[:user] is not empty.
Post conditions:
  New instance returned is successful.
  Raise exception w/errors otherwise.
Invariants:
   params[:user] is not modifiable.
  Database connection available.
Design by Contract
• Few implementations:
  – Native in Eiffel
  – As add-on like:
     • Java “assert”
     • .Net code contracts

• In Ruby
  – A.Hunt created DBC.rb (~2000)
  – Lost in translation
Code that verifies Code

• Focus on the valuable outputs

• Repeatable

• Regression
jUnit

• by Gamma & Beck since 1997

• by the time Java was “cool”

• Isolated Components

• TestCases & TestSuites
Test Driven Development (TDD)

• by Beck.

• Test First Design and then program.

• Small requirements.

• Verify functionality w/automated tests.
“the programmer
  should let
  correctness proof
  and program
  grow hand in hand”
               E.Dijsktra 1972
The TDD Cycle

           Failing
            test




                     Passing
Refactor
                       test
No Refactor

             Failing
              test
Technical Debt
  Overdose


  Fail to              Passing
 Refactor                test
Running Suite takes too long

               Failing
                test




                         Passing
    Refactor
                           test
“correctness concerns
 turn out to be a
 very effective
 heuristic guidance”


                E.Dijsktra 1972
Testing correctness - scales
          Acceptance    As the user would do



          Integration      The Ensamble.




             Unit       The single component
By Jewgienij Bal, Wikimedia Commons
Plenty of Tools
Focus on Ruby     Focus on Javascript
• Test-Unit       • QUnit
• RSpec           • Jasmine
• Cucumber        • Gerbil
Plenty of Tools
Focus on Ruby        Focus on Javascript
• Test-Unit          • QUnit
• RSpec              • Jasmine
• Cucumber           • Gerbil
RSpec

• Rails ready out-of-the-box

• Acceptance testing:
  – Selenium driver

  – Capybara

  – spec/integration
What do we test?
• Rails app:
  – Models
  – Controllers
  – Views

• Additionally:
  – Services
  – Components
Typical setup on Rails
       Acceptance




       Integration

            t
            i
            n
            U
.rspec
--colour
--format Fuubar
--profile

Additional gems:
  Autotest
  Specjour / Spork
Average runtime for test-suite for a small project
                         (294 examples)
25




20




15




10
         seconds




5




                                                         # example

0
     0             50   100   150    200    250    300               350
Two different sets of tests

                                                          rspec profile
10




                               y = 0.0033x2 - 1.6196x + 199.53
                                    Selenium, APIs
     seconds




                    y = 3E-05x2 - 0.0012x + 1.2525




                                            # example
 1
       0       50        100          150               200       250     300   350
Models && Persistence
• Object State & Transitions

• Custom methods and edge conditions

• Associations w/other objects

• Additional Validations
  – Expected fields.

  – Factories.
Controllers && Views
• Controllers:
  – Don’t test them, do acceptance tests.
  – Logic and state reside at Model level.
• Controllers that are APIs:
  – Don´t test them, do integration tests.
• Views
  – Do Acceptance testing for all actions
    available (not just CRUD).
Components

• Black Box Testing:
  – Test parameters and results.

  – Test correct invoking:
    • Send Email: ActionMailer parameters.

    • Draw a chart: HighChart parameters.
Services

• Test connectivity to services.

• Mocked for Integration Tests.
  – Save responses in fixtures.

  – Assume correct/wrong responses.
Javascript: Selenium driver
• Does a lot of things:
  – Creates an instance of browser.
  – Establishes connections.
  – Executes requests, renders responses.
  – Executes Javascript.
• Emulates the user experience
  – Browser “bugs”
Slow test suite
• Use a CI Server
  – Postpone long tests.
• Avoid Rails boot time
  – Decouple logic from Rails into POROs.
  – Use a no_rails_spec_helper.
• Avoid API calls lag
  – Mock external services.
Rspec




        By Nicolas M. Perrault , Wikimedia Commons
Questions?
спасибi

¡MUCHAS GRACIAS!
References
•   The Rspec Book, D. Chelimsky et al.
•   TDD by Example, K. Beck
•   Continuous Delivery, J.Humble
•   The Pragmatic Programmer, A. Hunt
•   The Humble Programmer, E. Dijkstra
    http://www.cs.utexas.edu/~EWD/transcription
    s/EWD03xx/EWD340.html
Is Cucumber a need?
• Rspec is good enough.
• “Simplicity –
     the art of maximizing the
     amount of work not done –
  is essential.”
• Specs are easy to program than
  specs + steps.
• Customer Interaction vs tools.
Stress && Performace
• Staging environment.
• Monitor execution, APM:
  – New Relic
  – Air Brake
“…program testing can
 be a very effective
 way to show the
 presence of bugs,
 but [it] is hopelessly
 inadequate for showing
 their absence.”
                 E.Dijsktra 1972

Rethinking Testing

  • 1.
    Rethinking Testing towardsyour needs pablo@cuboxsa.com
  • 4.
    eXtreme Programming Rspec Testing TestUnit jUnit Test Driven Development Crash Test
  • 5.
    “Our highest priorityis to satisfy the customer through the early and continuous DELIVERY of VALUABLE software.” The agile manifesto
  • 6.
    The Customer Working Code On Low Time Cost
  • 7.
    The Needs ofCustomer Working Code Acceptance Testing
  • 8.
  • 9.
    Design by Contract •by B. Meyer • Code enforces a contract • Language provides constraints – Before function call (Preconditions) – After function call (Post Conditions) – During all the function call (Invariants)
  • 10.
    User.create! params[:user] Pre conditions:  params[:user] is not empty. Post conditions: New instance returned is successful. Raise exception w/errors otherwise. Invariants:  params[:user] is not modifiable. Database connection available.
  • 11.
    Design by Contract •Few implementations: – Native in Eiffel – As add-on like: • Java “assert” • .Net code contracts • In Ruby – A.Hunt created DBC.rb (~2000) – Lost in translation
  • 12.
    Code that verifiesCode • Focus on the valuable outputs • Repeatable • Regression
  • 13.
    jUnit • by Gamma& Beck since 1997 • by the time Java was “cool” • Isolated Components • TestCases & TestSuites
  • 14.
    Test Driven Development(TDD) • by Beck. • Test First Design and then program. • Small requirements. • Verify functionality w/automated tests.
  • 15.
    “the programmer should let correctness proof and program grow hand in hand” E.Dijsktra 1972
  • 16.
    The TDD Cycle Failing test Passing Refactor test
  • 17.
    No Refactor Failing test Technical Debt Overdose Fail to Passing Refactor test
  • 18.
    Running Suite takestoo long Failing test Passing Refactor test
  • 19.
    “correctness concerns turnout to be a very effective heuristic guidance” E.Dijsktra 1972
  • 20.
    Testing correctness -scales Acceptance As the user would do Integration The Ensamble. Unit The single component
  • 21.
    By Jewgienij Bal,Wikimedia Commons
  • 22.
    Plenty of Tools Focuson Ruby Focus on Javascript • Test-Unit • QUnit • RSpec • Jasmine • Cucumber • Gerbil
  • 23.
    Plenty of Tools Focuson Ruby Focus on Javascript • Test-Unit • QUnit • RSpec • Jasmine • Cucumber • Gerbil
  • 24.
    RSpec • Rails readyout-of-the-box • Acceptance testing: – Selenium driver – Capybara – spec/integration
  • 25.
    What do wetest? • Rails app: – Models – Controllers – Views • Additionally: – Services – Components
  • 26.
    Typical setup onRails Acceptance Integration t i n U
  • 27.
  • 28.
    Average runtime fortest-suite for a small project (294 examples) 25 20 15 10 seconds 5 # example 0 0 50 100 150 200 250 300 350
  • 29.
    Two different setsof tests rspec profile 10 y = 0.0033x2 - 1.6196x + 199.53 Selenium, APIs seconds y = 3E-05x2 - 0.0012x + 1.2525 # example 1 0 50 100 150 200 250 300 350
  • 30.
    Models && Persistence •Object State & Transitions • Custom methods and edge conditions • Associations w/other objects • Additional Validations – Expected fields. – Factories.
  • 31.
    Controllers && Views •Controllers: – Don’t test them, do acceptance tests. – Logic and state reside at Model level. • Controllers that are APIs: – Don´t test them, do integration tests. • Views – Do Acceptance testing for all actions available (not just CRUD).
  • 32.
    Components • Black BoxTesting: – Test parameters and results. – Test correct invoking: • Send Email: ActionMailer parameters. • Draw a chart: HighChart parameters.
  • 33.
    Services • Test connectivityto services. • Mocked for Integration Tests. – Save responses in fixtures. – Assume correct/wrong responses.
  • 34.
    Javascript: Selenium driver •Does a lot of things: – Creates an instance of browser. – Establishes connections. – Executes requests, renders responses. – Executes Javascript. • Emulates the user experience – Browser “bugs”
  • 35.
    Slow test suite •Use a CI Server – Postpone long tests. • Avoid Rails boot time – Decouple logic from Rails into POROs. – Use a no_rails_spec_helper. • Avoid API calls lag – Mock external services.
  • 36.
    Rspec By Nicolas M. Perrault , Wikimedia Commons
  • 38.
  • 39.
  • 40.
    References • The Rspec Book, D. Chelimsky et al. • TDD by Example, K. Beck • Continuous Delivery, J.Humble • The Pragmatic Programmer, A. Hunt • The Humble Programmer, E. Dijkstra http://www.cs.utexas.edu/~EWD/transcription s/EWD03xx/EWD340.html
  • 41.
    Is Cucumber aneed? • Rspec is good enough. • “Simplicity – the art of maximizing the amount of work not done – is essential.” • Specs are easy to program than specs + steps. • Customer Interaction vs tools.
  • 42.
    Stress && Performace •Staging environment. • Monitor execution, APM: – New Relic – Air Brake
  • 43.
    “…program testing can be a very effective way to show the presence of bugs, but [it] is hopelessly inadequate for showing their absence.” E.Dijsktra 1972