aninda kundu
spacefugitive       @aninda42
2008

sidu ponnappa
kaiwren        @ponnappa
2006
any_instance
any_instance

that’s right - it’s an antipattern
wrest
wrest

'http://google.com'.to_uri(:follow_redirects => false).get
started two companies
started two companies

    over five years

{ context }
pair programming
stand-ups
TDD
{ facts }
O SHIT!
feedback!
changing code is tough

              +
feedback loops should be short
{ tests! }
no, not those
yeah, those
choices!
watir?
            TDD?
                               functional?
  cucumber?
                                    rspec?
BDD?
                                     minitest?
  shoulda?

       integration?            regression?
                      unit?
full-to confusion for beginners
still a little confusing for the experienced
a good place to start?
a good place to start?

      unit tests
this talk is about unit testing

cukes, capybara, selenium et. al. are out of scope
we also thought...
lets analyse the situation by looking at the villains
after all, a good villain is a sexy thang!
after all, a good villain is a sexy thang!

   makes you want to be the hero
like this guy
so this talk is about unit testing anti-patterns
{ two types }
two types

just for this talk, yeah? not formal, like.
implementation / workflow
implementation / workflow

what’s in the test / how it was written
{ implementation anti-patterns }
The long test



The test with too many assertions
The long test file
The long test file
The long test file



Refactor: extract multiple classes
The test that has logic
who watches the watchmen?
The implementation bound test
the test with arcane terminology
the test with arcane terminology
name/descriptor should say it all
the test with arcane terminology
  name/descriptor should say it all
treat these as developer documentation
the test with arcane terminology
    name/descriptor should say it all
treat these as developer documentation
people rolling onto a project <3 you for it
the test with arcane terminology
    name/descriptor should say it all
treat these as developer documentation
people rolling onto a project <3 you for it
      comment = well named test
The slow test
the slow test

too much data ? (again, fixtures or heavy setup)
The slow test

too much data ? (again, fixtures or heavy setup)
                   up next..
The slow test

too much data ? (again, fixtures or heavy setup)
                   up next..

   Too many objects (running into MRI GC?)
The slow test

Too much data ? (again, fixtures or heavy setup)
                   up next..

   Too many objects (running into MRI GC?)
                 profile and fix
The slow test

Too much data ? (again, fixtures or heavy setup)
                   up next..

   Too many objects (running into MRI GC?)
                 profile and fix

              Testing externals?
The slow test

Too much data ? (again, fixtures or heavy setup)
                   up next..

   Too many objects (running into MRI GC?)
                 profile and fix

              Testing externals?
               Stop it! Stub it!
The slow test

Too much data ? (again, fixtures or heavy setup)
                   up next..

   Too many objects (running into MRI GC?)
                 profile and fix

              Testing externals?
               Stop it! Stub it!

          Rails startup time !@#$?
The slow test

Too much data ? (again, fixtures or heavy setup)
                   up next..

   Too many objects (running into MRI GC?)
                 profile and fix

              Testing externals?
               Stop it! Stub it!

          Rails startup time !@#$?
                Spork, Guard
The test that has complex setup
The test that has complex setup

Fixtures are for static data only
The test that has complex setup

           Fixtures are for static data only

Large factory setups are better than invisible fixtures
The test that has complex setup

           Fixtures are for static data only

Large factory setups are better than invisible fixtures

             Stub non essential factories
All tests should execute independantly as well as a suite
All tests should execute independantly as well as a suite




              tests must not share state
Avoid redundant / replicated tests
Avoid redundant / replicated tests



  Push tests down the order as much as possible

 e.g. Controller tests that can exist as model tests

Rule of thumb : Do this as long as you can keep the
          name of the test almost the same
The test that shows high churn
The test that cannot fail



Red-green refactor. Make sure the test fails first.
{ workflow anti-patterns }
TEST LAST
#1
refactor
#1.1
b692246bad
refactor


b692246bad
refactor


b692246bad         0df4e19f2a
TESTING ONLY FOR
   REGRESSION
usually test last
TDD helps encapsulate
TESTING ELSWHERE
separate teams of testers writing unit tests
ONLY RUN RAKE
means you’re never working on just one unit
NEVER RUN RAKE
without CI you’re in a world of pain
ONLY RUN TEST(S) FROM CLI
slower, less productive
QA TEAM FOR BUSINESS LOGIC
       VERIFICATION
your tests should be enough
NO CI
test frequently (every commit!)
feedback for distributed/large teams
CI servers are collaboration tools
MANUAL DEPLOY TO STAGING
don’t trust your commits
{ reading
between the
    lines }
text
subtext
we do BDD, not TDD
we do BDD, not TDD

 we only write Cukes
we have 100% code coverage
we have 100% code coverage

that’s from all those Cukes we just told you about
we love our tests

we run them at least once every day
we keep our build green
we keep our build green

we comment out failing tests if that’s what it takes
tests are an integral part of the
         way we work
tests are an integral part of the
              way we work
we write them for every client that wants them
Attributions
•Agile Pills: http://briannoyle.wordpress.com/2009/05/12/getting-yer-agile-on-at-a-discount-upcoming-course
•TDD index card: http://www.testically.org/2011/01/12/the-ideal-use-case-to-give-tdd-a-try/
•Pair programming: http://thoughtworker.com/agile-our-way-life
•Stand-up: http://www.flickr.com/photos/karthikc/333796551/
•C# : http://www.jameswiseman.com/blog/tag/c-sharp/
•Robotic arm : http://profmason.com/?p=562
•Wrecking ball: http://marilebetterdays.wordpress.com/2012/02/28/wrecking-ball-lyrics-the-title-track/
•Cat eat finger: http://www.123rf.com/photo_2005824_cat-eating-human-finger.html
•Tiger attack: http://tvmywifewatches.blogspot.com/2011/07/rhw-of-nj-pointcounterpoint-should-kim.html
•Lex Luthor: http://maxnaylor.ca/?attachment_id=835
•The Joker: http://www.fanpop.com/spots/the-joker/images/9028188/title/joker
42

@ponnappa        @aninda42




                        

Testing smells

Editor's Notes

  • #2 \n
  • #3 \n
  • #4 \n
  • #5 \n
  • #6 \n
  • #7 \n
  • #8 \n
  • #9 \n
  • #10 blah blah contributed to rspec blah blah\n
  • #11 \n
  • #12 contributed an anti pattern to rspec\n
  • #13 \n
  • #14 \n
  • #15 \n
  • #16 \n
  • #17 Ask them what&amp;#x2019;s the difference.\n
  • #18 Ask them what&amp;#x2019;s the difference.\n
  • #19 Ask them what&amp;#x2019;s the difference.\n
  • #20 \n
  • #21 Ask them what&amp;#x2019;s the difference.\n
  • #22 Lets start with a few well understood facts about our community which help set some context - we&amp;#x2019;ll circle back to them later\n
  • #23 we&amp;#x2019;re all agilists. I haven&amp;#x2019;t come across a single waterfall Ruby shop - or not one that claims this publicly. Agile is all about short feedback loops allowing you to deliver high quality software while dealing with change.\n
  • #24 Pairing is much less common - but accepted.\n
  • #25 Pairing is much less common - but accepted.\n
  • #26 Everyone writes tests now. Nobody will publicly state that they explicitly consider testing and/or TDD a low value activity or worse, a complete waste of time.\n
  • #27 Lets start with a few well understood facts about building software\n
  • #28 new codebases are awsome. so beautiful...\n
  • #29 then it&amp;#x2019;s jenga\n
  • #30 and anyone working on it winds up like the good prince\n
  • #31 And because these things are true, we try to solve the problem with tests\n
  • #32 Short feedback cycles are important. It&amp;#x2019;s like a friendly kitty giving you feedback about code that needs petting.\n
  • #33 Lengthen your feedback cycles and what you learn when the feedback hits you will be like having a royal bengal tiger jump on you while you&amp;#x2019;re in a pool. Seriously.\n
  • #34 And because these things are true, we try to solve the problem with tests\n
  • #35 And because these things are true, we try to solve the problem with tests\n
  • #36 And because these things are true, we try to solve the problem with tests\n
  • #37 And because these things are true, we try to solve the problem with tests\n
  • #38 And because these things are true, we try to solve the problem with tests\n
  • #39 \n
  • #40 \n
  • #41 \n
  • #42 \n
  • #43 \n
  • #44 \n
  • #45 \n
  • #46 \n
  • #47 \n
  • #48 \n
  • #49 \n
  • #50 \n
  • #51 \n
  • #52 \n
  • #53 \n
  • #54 \n
  • #55 \n
  • #56 \n
  • #57 A Test should have only one reason to fail and therefore ideally make just one assertion.\n
  • #58 \n
  • #59 \n
  • #60 \n
  • #61 \n
  • #62 Tests are supposed to be simple, easy to read and understand.\nAre you going to write a test that tests your test? Then avoid logic in tests.\n\n
  • #63 \n
  • #64 \n
  • #65 \n\n
  • #66 \n\n
  • #67 \n\n
  • #68 If your code does not have behaviour, dont test it. Its totally fine to skip it\n
  • #69 \n\n
  • #70 \n\n
  • #71 \n\n
  • #72 \n\n
  • #73 \n\n
  • #74 Testing is used for quick feedback to the developer. \n\nSlow test suites are run less often. \n\nSkipping test run can cause a dev to spend more time in retracing their steps to figure out what broke the tests since they had them passing last.\n\nIts worth finding out which your slowest tests are and optimizing them.\n\n
  • #75 \n\n
  • #76 \n\n
  • #77 \n\n
  • #78 \n\n
  • #79 \n\n
  • #80 \n\n
  • #81 \n\n
  • #82 \n\n
  • #83 Testing is used for quick feedback to the developer. \n\nSlow test suites are run less often. \n\nSkipping test run can cause a dev to spend more time in retracing their steps to figure out what broke the tests since they had them passing last.\n\nIts worth finding out which your slowest tests are and optimizing them.\n\n
  • #84 Testing is used for quick feedback to the developer. \n\nSlow test suites are run less often. \n\nSkipping test run can cause a dev to spend more time in retracing their steps to figure out what broke the tests since they had them passing last.\n\nIts worth finding out which your slowest tests are and optimizing them.\n\n
  • #85 Testing is used for quick feedback to the developer. \n\nSlow test suites are run less often. \n\nSkipping test run can cause a dev to spend more time in retracing their steps to figure out what broke the tests since they had them passing last.\n\nIts worth finding out which your slowest tests are and optimizing them.\n\n
  • #86 Testing is used for quick feedback to the developer. \n\nSlow test suites are run less often. \n\nSkipping test run can cause a dev to spend more time in retracing their steps to figure out what broke the tests since they had them passing last.\n\nIts worth finding out which your slowest tests are and optimizing them.\n\n
  • #87 \n
  • #88 \n
  • #89 Testing is used for quick feedback to the developer. \n\nSlow test suites are run less often. \n\nSkipping test run can cause a dev to spend more time in retracing their steps to figure out what broke the tests since they had them passing last.\n\nIts worth finding out which your slowest tests are and optimizing them.\n\n
  • #90 Testing is used for quick feedback to the developer. \n\nSlow test suites are run less often. \n\nSkipping test run can cause a dev to spend more time in retracing their steps to figure out what broke the tests since they had them passing last.\n\nIts worth finding out which your slowest tests are and optimizing them.\n\n
  • #91 Testing is used for quick feedback to the developer. \n\nSlow test suites are run less often. \n\nSkipping test run can cause a dev to spend more time in retracing their steps to figure out what broke the tests since they had them passing last.\n\nIts worth finding out which your slowest tests are and optimizing them.\n\n
  • #92 Testing is used for quick feedback to the developer. \n\nSlow test suites are run less often. \n\nSkipping test run can cause a dev to spend more time in retracing their steps to figure out what broke the tests since they had them passing last.\n\nIts worth finding out which your slowest tests are and optimizing them.\n\n
  • #93 Testing is used for quick feedback to the developer. \n\nSlow test suites are run less often. \n\nSkipping test run can cause a dev to spend more time in retracing their steps to figure out what broke the tests since they had them passing last.\n\nIts worth finding out which your slowest tests are and optimizing them.\n\n
  • #94 Testing is used for quick feedback to the developer. \n\nSlow test suites are run less often. \n\nSkipping test run can cause a dev to spend more time in retracing their steps to figure out what broke the tests since they had them passing last.\n\nIts worth finding out which your slowest tests are and optimizing them.\n\n
  • #95 \n
  • #96 \n
  • #97 This is rule number 1\n
  • #98 raaad\n
  • #99 gureeen\n
  • #100 reefactor\n
  • #101 better yet\n
  • #102 \n
  • #103 \n
  • #104 \n
  • #105 \n
  • #106 red-green-commit-refactor-commit\n
  • #107 \n
  • #108 \n
  • #109 \n
  • #110 \n
  • #111 \n
  • #112 \n
  • #113 \n
  • #114 \n
  • #115 \n
  • #116 \n
  • #117 \n
  • #118 \n
  • #119 \n
  • #120 \n
  • #121 \n
  • #122 \n
  • #123 \n
  • #124 \n
  • #125 \n
  • #126 So - back to what you hear about testing. Here are some of the common things bandied about in the industry, with some subtext about what it usually means.\n
  • #127 then there&amp;#x2019;s the subtext - which I&amp;#x2019;ll mention where appropriate\n
  • #128 \n
  • #129 Ask them what&amp;#x2019;s the difference.\n
  • #130 Ask them what&amp;#x2019;s the difference.\n
  • #131 Ask them what&amp;#x2019;s the difference.\n
  • #132 Ask them what&amp;#x2019;s the difference.\n
  • #133 Ask them what&amp;#x2019;s the difference.\n
  • #134 \n
  • #135 \n
  • #136 \n
  • #137 \n
  • #138 \n
  • #139 \n