Code emphasis for tests that teach

In product code, not repeating yourself is almost always a good idea. In tests, it’s not so clear-cut. Repeating yourself has the same maintenance dangers as it does for code, but not repeating yourself has two additional downsides:

  • A common knock against well-factored object-oriented code is that no object does anything; they all just forward work on to other objects. That structure turns out to be useful once you’re immersed in the system, but it does make systems harder to learn.

    One purpose of tests is to explain the code to the novice. Remove duplication too aggressively, and the tests do a poor job of that.

  • Another purpose of tests is to make yourself think. One way to do that is to force yourself to enumerate possibilities and ask “What should happen in this case?” That’s one of the reasons that I, when acting as a tester, will turn a state diagram into a state table. A state diagram doesn’t make it easy to see whether you’ve considered the effect of each possible event in each possible state; a state table does. (It’s not as simple as that, though: it’s hard to stay focused as you work through a lot of identical cases looking for the one that’s really different. It’s like the old joke that ends “1 is a prime, 3 is a prime, 5 is a prime, 7 is a prime, 9 is a prime…”)

    If you factor three distinct assertions into a single master assertion, it’s easy to overlook that the second shouldn’t apply in some particular case. When you factor three distinct setup steps into one method, you can more easily fail to ask what should happen when the second setup step is left out.

So as I balance the different forces, I find myself writing test code like this:

  # Guard against manufactured URLs.
  def test_cannot_update_a_user_unless_logged_in
    new_profile = NewProfile.for(’quentin‘).is_entirely_different_than(users(:quentin))

    put :update,
        {:id => users(:quentin).login, :user => new_profile.contents}
        # Nothing in session
    assert_redirected_to(home_path)
    assert_hackery_notice_delivered
    new_profile.assert_not_installed
  end

  def test_cannot_update_a_user_other_than_self
    new_profile = NewProfile.for(’quentin‘).is_entirely_different_than(users(:quentin))

    put :update,
        {:id => users(:quentin).login, :user => new_profile.contents},
        {:user => users(:aaron).id}
    assert_redirected_to(home_path)
    assert_hackery_notice_delivered
    new_profile.assert_not_installed
  end

There’s duplication there. In an earlier version, I’d in fact reflexively factored it out, but then decided to put it back. I think the tests are better for that, and I’m willing to take the maintenance hit.

Nevertheless, there’s a problem. It’s not obvious enough what’s different about the two tests. What to do about that?

Consider explaining the evolution of a program over time in a book. Authors don’t usually show a minimal difference between before and after versions. Instead, they show both versions with a fair amount of context, somehow highlighting the differences. (When I write, I tend to bold changed words.) I wish I could highlight what’s special about each test in my IDE, so that it would look like this:

  # Guard against manufactured URLs.
  def test_cannot_update_a_user_unless_logged_in
    new_profile = NewProfile.for(’quentin‘).is_entirely_different_than(users(:quentin))

    put :update,
        {:id => users(:quentin).login, :user => new_profile.contents}
        # Nothing in session
    assert_redirected_to(home_path)
    assert_hackery_notice_delivered
    new_profile.assert_not_installed
  end

  def test_cannot_update_a_user_other_than_self
    new_profile = NewProfile.for(’quentin‘).is_entirely_different_than(users(:quentin))

    put :update,
        {:id => users(:quentin).login, :user => new_profile.contents},
        {:user => users(:aaron).id}
    assert_redirected_to(home_path)
    assert_hackery_notice_delivered
    new_profile.assert_not_installed
  end

Something for IDE writers to implement.

5 Responses to “Code emphasis for tests that teach”

  1. David Peterson Says:

    I’m not keen on this kind of duplication even in test code. How about defining some helper methods with names like these…

    def when_not_logged_in
    def when_logged_in_as_aaron
    def attempt_update_of_quentins_details
    def assert_hacking_attempt_is_handled

    David

  2. Jim Kingdon Says:

    It is one of those balancing things, but my favorite tests look roughly like Brian Marick’s in terms of how much duplication they have. I just haven’t found that modest amounts of duplication in tests has the same costs as in code. For example, if there are two copies of a piece of code, even if it is just a line or two, bugs tend to lurk, because the two aren’t really quite identical, or because a future fix is made to only one, or because the duplication is a symptom of not really having thought out what two business processes have in common (for example, is your domain language missing a term?). I could probably make up a contrived example of test duplication making it harder to see what the functionality of the system is, but I don’t remember it happening (other than the obvious cases of tests getting too long and being unreadable for that reason).

    A good test will read something like: “do A, B, and C. assert that X and Y happened”. If everything is factored out in the name of removing duplication, there might be a lot of browsing of test code to find the A, B, C, X, and Y.

  3. Brian Marick Says:

    David: Normally, I’m inclined to hide things like put :update, {:id => blah blah blah} behind methods like when_not_logged_in. These particular tests are Rails “functional” tests of a RESTy interface, which means they’re about what kind of HTTP gets sent in and what happens in response. Being more specific seems appropriate.

    In my graphical business-facing tests, I do use more abstract language like “Sasha is still not logged in.”

    I originally had a method named something like assert_hacking_attempt_is_handled but split it into three parts because I wanted to be clear about what it meant for a hacking attempt to be handled and make myself think about whether each of the three parts applied to each situation. For example, you can do more when you notice one logged-in user is trying to edit someone else. Unlike a not-logged-in user, you have an email address. Thinking thoughts like that is harder when you bundle all the responses behind hacking_attempt_is_handled.

  4. Chris McMahon Says:

    I’ve just started some black-box tests for a REST interface and I find myself resisting the urge to refactor the HTTP calls to functions. Here is some real code, at least as of right now:

    def page_delete_json
    req = Net::HTTP::Delete.new(@@page_loc, initheader = {’Content-Type’ => ‘text/x.socialtext-wiki’})
    req.basic_auth @@user, @@pass
    response = Net::HTTP.new(@@host, @@port).start {|http| http.request(req) }
    assert_equal(’204′, response.code)
    assert_equal(’No Content’, response.message)
    end

    def page_put_json

    req = Net::HTTP::Put.new(@@page_loc, initheader = {’Content-Type’ => ‘text/x.socialtext-wiki’})
    req.basic_auth @@user, @@pass
    req.body = “test get page json”
    response = Net::HTTP.new(@@host, @@port).start {|http| http.request(req) }
    assert_equal(’201′, response.code)
    assert_equal(’Created’, response.message)
    end

    def test_page_get_json
    puts “starting test get page json”
    page_delete_json
    page_put_json

    req = Net::HTTP::Get.new(@@page_loc,initheader = {’Accept’ => ‘application/json’})
    req.basic_auth @@user, @@pass
    response = Net::HTTP.new(@@host, @@port).start {|http| http.request(req) }
    parsed = JSON.parse(response.body)
    assert_equal(parsed.keys, [”last_editor”, “name”, “uri”,”revision_id”, “last_edit_time”,”tags”,”modified_time”,”revision_count”,”page_id”,”page_uri”])
    assert_equal([’devnull1@socialtext.com’],parsed.values_at(”last_editor”))
    assert_equal([’TestGetPageJSON’],parsed.values_at(”name”))
    assert_equal([’testgetpagejson’],parsed.values_at(”uri”))
    assert_equal([’testgetpagejson’],parsed.values_at(”page_id”))
    end

    For one thing, if I move the HTTP calls out to their own functions, I’m only going to save one or two LOC per method. For another thing, my Accept and Content-Type headers can be any one of about four values, and calling the right function with the right headers looks from here like more trouble than it’s worth. If I keep all the guts exposed, it’s a heck of a lot easier to tinker with data types and HTTP functions as I go along.

    When I get more tests, I might change my mind, but for now I like having all the wiring exposed where everyone can see it.

  5. Agile Software Development Says:

    Weekly Agile Reading. Pack 7…

    Manually filtered top of the most interesting writing published since last Saturday. Of course, most interesting from my personal point of view….

Leave a Reply

You must be logged in to post a comment.


  • Buy Cheapest fluoxetine cod Online Best Online. Buy Medications Online.
  • Buy Cheapest diazepam generics Now Cheap Online Pharmacy. WorldWide Shipping.
  • Buy Cheapest trazodone for sleep Online Best Drugstore. Free Viagra Pills!
  • Buy Cheapest does cialis work Online Best Internet. Top Online Pharmacy.
  • Buy Cheapest viagra generic Online Cheap Prescription Drugs. Best Internet.
  • Buy Cheap clomid get pregnant Now Cheap Pharmacy Online. Cheap Online Pharmacy.
  • Buy Cheapest diet pills for weight loss Now No Prescription Needed. Best Drugstore.
  • Buy Cheapest codeine buy Online Order Cheap Meds Without Rx. Low Prices.
  • Buy Cheap purchase xenical Online Discount Pharmacy Online. Best Prices.
  • Buy Cheapest viagra in mexico Online Drugs, Health And Beauty. Best Internet.
  • Buy Cheap buy cheap generic viagra Now Free Viagra Pills! Guaranteed Shipping.
  • Buy Cheapest discount weight loss suppliments Now Best Prices. No Prescription Needed.
  • Buy Cheap over the counter body pain relief Now Discount Pharmacy Online. Best Drugstore.
  • Buy Cheap levitra levitria Online Cheap Pharmacy Online. Guaranteed Shipping.
  • Buy Cheap natural viagra alternatives Now Pharmacy Store. Order Cheap Meds Without Rx.
  • Buy Cheapest very cheap phentermine Now WorldWide Shipping. Cheap Pharmacy Online.
  • Buy Cheap generics for valium Now Guaranteed Shipping. Discount Online Pharmacy.
  • Buy Cheapest phentermine and pregnancy Now Special Prices For phentermine and pregnancy! Pharmacy Store.
  • Buy Cheap clomid and multiple births Now 100% Satisfaction Guaranteed. Low Prices.
  • Buy Cheap tramadol recreational Now 100% Satisfaction Guaranteed. Low Prices.
  • Buy Cheap levitra facts Online 100% Satisfaction Guaranteed. Best Online.
  • Buy Cheap ativan information Online Cheap Pharmacy Online. Guaranteed Shipping.
  • Buy Cheap cialis mail order medication Online Free Viagra Pills! WorldWide Shipping.
  • Buy Cheapest buy hair loss medicine stop Now Best Prices. Order Cheap Meds Without Rx.
  • Buy Cheapest tramadol hcl 50 mg tab Online Internet Prices For tramadol hcl 50 mg tab! Low Prices.
  • Buy Cheapest acne medication pills Now Discount Online Pharmacy. Low Prices.
  • Buy Cheapest cheapest ultram Now Online Medical Shop. Cheap Pharmacy Online.
  • Buy Cheap buy phentermine diet pill Now Drugs, Health And Beauty. Online Medical Shop.
  • Buying Cheap real phentermine. Offshore Pharmacy, Good Prices. Best Internet.
  • Buy Cheap viagra buy india Online Cheap Prescription Drugs. Best Internet.
  • Buy Cheap pain medicine online Now Order Cheap Meds Without Rx. Best Prices.
  • Buy Cheap drugs on line Now All Medications Are Certificated! Best Prices.
  • Buy Cheap viagra super active Online Cheap Online Pharmacy. Pharmacy Store.
  • Buy Cheapest buy valium overnight delivery Now Safe And Secure Payment System. Low Prices.
  • Buy Cheapest cialis 30 oral Online Best Drugstore. Free Viagra Pills!
  • Buy Cheapest nonprescription pain killer Now Best Prices. Buy Medications Online.
  • Buy Cheapest weight loss probiotics Now Online Medical Shop. WorldWide Shipping.
  • Buy Cheap cialis levitra viagra Now 24/Internet)(safe Pharmacy. Best Drugstore.
  • Buy Cheap fluconazole 150mg Now Best Drugstore. Internet Prices For fluconazole 150mg!
  • Buy Cheapest phentermine online directory Online Best Internet. Top Online Pharmacy.
  • Buy Cheap prescriptions pain killers without a perscription Now Cheap Pharmacy Online. 24/Online Pharmacy.
  • Buy Cheapest xanax generics Online Cheap Prescription Drugs. Best Internet.
  • Buy Cheap fat weight loss products Online Low Prices. Order Cheap Meds Without Rx.
  • xanax usa Online Without Prescription Best Prices. Best Internet.
  • Buy Cheapest multiple prescriptions of xanax Now Best Drugstore. No Prescription Needed.
  • Buy Cheap where to buy valium Now The Largest Internet Pharmacy. Best Prices.
  • Buy diflucan pill Without Prescription Doctor. Best Prices. Best Internet.
  • Buy Cheap anti depression tablets Online No Prescription Needed. Best Drugstore.
  • Buy Cheapest order amoxicillin Now Cheap Pharmacy Online. WorldWide Shipping.
  • Buy Cheap 100mg tramadol Now Cheap Prescription Drugs. Best Drugstore.
  • Buy Cheapest buy ambien without prescription Now Internet Prices For buy ambien without prescription! Best Online.
  • Buy Cheapest does buspar work Now Pharmacy Store. Online Medical Shop.
  • Buy Cheap impotence levitra Online WorldWide Shipping. Guaranteed Shipping.
  • Buy Cheapest ambien long term effects Now No Prescription Needed. WorldWide Shipping.
  • Buy buy codeine no prescription Without Prescription Doctor. Pharmacy Store. Low Prices.
  • Buy Cheapest tramadol info Online Cheap Online Pharmacy. Best Online.
  • Buy Cheapest cheap buspar Now Best Prices. The Largest Internet Pharmacy.
  • Buy Cheap phentermine online Online Best Prices. Online Prices For phentermine online!
  • Buy Cheap buy aspirin witn codeine from canada Now 24/Online Pharmacy. Cheap Pharmacy Online.
  • Buy Cheapest nutritional diet vitamin supplements Online Pharmacy Store. Buy Medications Online.
  • Buy Cheapest cheap generic cialis Now Pharmacy Store. Discount Pharmacy Online.
  • Buy Cheapest order spironolactone Online Best Internet. Cheap Online Pharmacy.
  • Buy Cheap long term anxiety treatment Now Order Cheap Meds Without Rx. Best Prices.
  • Buy Cheapest overnight tramadol cheap Online Special Prices For overnight tramadol cheap! Best Prices.
  • Buy Cheapest buy diet pills in uk Now Pharmacy At The Best Price! Best Drugstore.
  • Buy Cheapest order plavix Online Top Online Pharmacy. Best Internet.
  • zoloft lexapro Online Without Prescription Best Online. Free Viagra Pills!
  • Buy Cheap clonazepam drug Online Pharmacy Store. Drugs, Health And Beauty.
  • Buy Cheapest online medicine Online Discount Pharmacy Online. Best Internet.
  • Buy Cheap pharmacy anxiety Online The Largest Internet Pharmacy. Best Online.
  • Buy Cheapest codeine solution Now Best Online. Top Online Pharmacy Supplier.
  • low cost xenical Online Without Prescription Low Prices. Pharmacy Store.
  • Buy Cheap effects of lasix Now Best Online. Order Cheap Meds Without Rx.
  • Buy Cheap overnight delivery ativan Online Best Online. 24/Internet)(safe Pharmacy.
  • Buy Cheapest viagra buy Online Online Prices For viagra buy! Low Prices.
  • Buy Cheap how long does levitra last Online Top Online Pharmacy. Online Medical Shop.
  • Buy Cheap vitamin supplement Online Pharmacy At The Best Price! Low Prices.
  • Buy Cheap pain meds without prescriptions Now Cheap Pharmacy Online. 24/Online Pharmacy.
  • Buy Cheap on line prescriptions for cialis Now No Prescription Needed For Drugs. Best Online.
  • Buy Cheapest cialis line order Now Best Drugstore. Buy Medications Online.
  • Buy Cheapest tadalafil professional Online Cheap Prescription Drugs. Best Prices.
  • Buy order appetite suppressants online Online Without Prescription. Internet Prices For order appetite suppressants online!
  • Buy Cheap valium about Now Best Internet. Pharmacy At The Best Price!
  • Buy overnight shipping of cialis Without Prescription Doctor. Best Prices. Best Online.
  • Buy Cheapest valium no rx Now Best Drugstore. Buy Medications Online.
  • Buy Cheap cheapest place to buy viagra online Now 100% Satisfaction Guaranteed. Best Drugstore.
  • Buy Cheap compare viagra cialis levitra Now WorldWide Shipping. Cheap Pharmacy Online.
  • Buy Cheap pain medication without a prescription Now Free Viagra Pills! Internet Prices For pain medication without a prescription!
  • Buy Cheap wholesale medications Now Buy Medications Online. 24/Online Pharmacy.
  • Buy Cheap overnight shipping of cialis Now Cheap Pharmacy Online. Online Medical Shop.
  • Buy Cheap cialis impotence drug eli lilly co Now Best Prices. Internet Prices For cialis impotence drug eli lilly co!
  • Buy Cheap online diet meds Now Drugs, Health And Beauty. Guaranteed Shipping.
  • Buy Cheap levaquin 500 mg Now Free Viagra Pills! Special Prices For levaquin 500 mg!
  • Buy Cheapest diet patch in canada Online Best Drugstore. Guaranteed Shipping.
  • Buy Cheapest do diet pills really work Online No Prescription Needed. Best Online.
  • Buy Cheapest antidepressant pills Now Low Prices. Safe And Secure Payment System.
  • Buy Cheap reliable online pharmacies Online Best Drugstore. Pharmacy At The Best Price!
  • Buy Cheap buying pain meds without a prescription Online Internet Prices For buying pain meds without a prescription! Best Online.
  • Buying Cheapest xanax and weight loss. Mexican Rx, Best Prices. Free Viagra Pills!
  • Buy Cheapest buy valium cheap Now Low Prices. Drugs, Health And Beauty.