6194 comments
2357 subscribers
Follow me on Twitter (@sachac)
Subscribe! Feed reader E-mail

Quantified Awesome: Development-driven behaviour and integrated tests for life

2011-12-14: Updated step code

In terms of testing code, behaviour-driven development is fantastic. You can write your tests in pretty much plain English using a testing tool like Cucumber for Rails, which makes it easier to communicate with other people (including clients!). There’s a certain satisfaction in getting your tests to pass, and when they break, you know something needs fixing.

I’ve been thinking about what automated tests might look like in life. It turned out to be easy to prototype, thanks to the data I’m already collecting. It’s almost like development-driven behavior: can I apply the tools I use in software development to help me change my behaviours in life?

Here are some results from my very first integration test of real life:

Feature: Development-driven behaviour

  Scenario: Check for overdue books                # features/life.feature:2
    When I check our library items                 # features/step_definitions/life.rb:3
    Then there should be no items that are overdue # features/step_definitions/life.rb:7

  Scenario: Check my work load                       # features/life.feature:5
    When I look at my time use for the past 7 days   # features/step_definitions/life.rb:11
    Then I should have time data                     # features/step_definitions/life.rb:19
    And I should have worked between 40 and 44 hours # features/step_definitions/life.rb:24
      <46.5166666666667> expected to be
      <=
      <44.0>. (Test::Unit::AssertionFailedError)
      ./features/step_definitions/life.rb:26:in `/^I should have worked between (\d+) and (\d+) hours$/'
      features/life.feature:8:in `And I should have worked between 40 and 44 hours'

  Scenario: Check if I'm sleeping                        # features/life.feature:9
    When I look at my time use for the past 7 days       # features/step_definitions/life.rb:11
    Then I should have slept between 8 and 9 hours a day # features/step_definitions/life.rb:29

Failing Scenarios:
cucumber features/life.feature:5 # Scenario: Check my work load

3 scenarios (1 failed, 2 passed)
7 steps (1 failed, 6 passed)
0m0.833s

Cucumber highlights failing tests in red and it lists the failures as well.

Here’s the steps.rb that I’ve started fleshing out:


When /^I look at my time use for the past (\d+) days?$/ do |arg1|
  @start_time = (Date.today - arg1.to_i.days).midnight.in_time_zone
  @end_time = Date.today.midnight.in_time_zone
  @log = TimeTrackerLog.new(User.first)
  @entries = @log.entries(@start_time, @end_time)
  @summary = @log.summarize(@start_time, @end_time)
end

Then /^I should have time data$/ do
  assert @entries != nil
  assert @entries.size > 0
end

Then /^I should have worked between (\d+) and (\d+) hours$/ do |min, max|
  assert_operator @summary['A - Work'] / 1.hour, :>=, min.to_f
  assert_operator @summary['A - Work'] / 1.hour, :< =, max.to_f
end

Then /^I should have slept between (\d+) and (\d+) hours a day$/ do |min, max|
  average = @summary['A - Sleep'] * 1.0 / (1.hour * ((@end_time - @start_time) / 1.day))
  assert_operator average, :>=, min.to_f
  assert_operator average, :< =, max.to_f
end

# LIBRARY

When /^I check our library items$/ do
  nil # Actually stored in database, so we don't need anything here. This is more for semantics
end

Then /^there should be no items that are overdue$/ do
  assert_equal 0, LibraryItem.where('status = ? AND due < ?', 'due', Date.today).size
end

I am pleasantly boggled that this is possible, and will probably write all sorts of odd tests now. Because Cucumber can fill in web forms, click on stuff, and so on, I might even be able to use it to check information on other sites. (When I check my mail, then all the messages in my inbox should be less than a week old?)

Oh, the possibilities…

Short URL: http://sachachua.com/blog/p/22632
  • http://www.trajano.net/ Archimedes Trajano

    All I can say is “LOL” :D
    The hardest part for me to do this is the discipline providing the input if I ever have to do this.

On This Day...

  • 2012: Weekly review: Week ending November 3, 2012 — Second week of conference sprint. Toronto was buffeted by Hurricane Sandy, but skipped the worst of it. I stayed home [...]
  • 2011: Weekly review: Week ending November 4, 2011 — I worked 46 hours this week. Most of the overtime was from a 12-hour sprint on Tuesday, getting all our [...]
  • 2010: Where do you find topics to write about? How to have tons of topics — People often ask me how they can blog more. The easiest blog post to write is the answer. It is [...]
  • 2009: My Inkscape settings — Here are some things I change right away in my Inkscape settings: Double-click on the pencil tool, and choose “Last used [...]
  • 2006: Real life — I went to Michael McGuffin’s birthday party. I enjoy spending time with him, his wife Alice, and their kids. There’s just [...]
  • 2006: Combimouse and completely split keyboards — I keep daydreaming about a totally split keyboard. Something like the Combimouse, I guess. I hope they commercialize it soon, ’cause I [...]
  • 2006: Feeling much better — I was in bed all day yesterday thanks to a very annoying fever and the beginning of a sore throat. It [...]
  • 2005: A couple of quotes — A crucible is a severe test of patience and belief. Whether the crucible experience is an apprenticeship, an ordeal, or [...]
  • 2004: Pfft. Geek bias. — Hmm. 51.67653% – Super Geek. Oh well. Slightly disappointing. ;) Then again, I’m not part of the usual(?!) geek culture. I [...]
  • 2004: Nifty! The quick start’s sorta readable! — Clair has started playing around with Emacs and Planner. She found the quick start useful. Yaaaaaaaaaaaaaaaaaaaaaaaaaay! Clair: A basic Planner should automatically pick up [...]
  • 2004: Collected links for Faye story — Some people e-mailed me for information, so I figured I’d see what I could come up with using my searching-fu. Google [...]
  • 2004: Will move to a hotel — New trainees are coming to Tokyo Kenshu Center next week, so Aris and I have to move to a hotel temporarily. [...]
  • 2004: Update on Faye — Followup stories. Posting because people who read this blog might be curious. - Faye being used in political attack on gov’t? (2004.11.04) - [...]
  • 2003: chatting with Diane — quiz every day 5 points not all objective chapter a day not pressured to memorize objective things helpful reviews do what we feel like doing – [...]
  • 2003: Freemind extensions — Top-down chart mode?