Rspec-git

Playing with TDD would be better if I could save the stable points of my software and use the description of the new testcase as git commit message too. I needn’t make any git commit, since when I say “User should have name” in my testcase specification, it contains all the information about the changes in the code. So my idea is to use the ruby spec files to generate git commits.

I created a proof of concept and released it on rubygems.org. You can install it with gem:

1
gem install rspec-git
gem install rspec-git

Here I just want to show you in a really simple example how it works. Using my previous example, I extend my User object with name feature.

specs/unit/user.rb

1
2
3
4
5
describe User do
  it "should have name" do
    User.new.should respond_to(:name)
  end
end
describe User do
  it "should have name" do
    User.new.should respond_to(:name)
  end
end

Now my test fails, so I implement the feature to be my example stable again.

lib/user.rb

1
2
3
class User
  attr_accessor :name
end
class User
  attr_accessor :name
end

Now the test is green, no refactoring is required, so we can save this point as a stable point. Calling rspec-git will do all the things!

1
rspec-git
rspec-git

The new commit message looks like below:

1
2
3
4
5
6
$ git log
commit 2fad8841019ac484fe2f298b983219e0b13aea43
Author: Laszlo Papp <nucc_at_bteam_hu>
Date:   Sat Oct 27 21:36:34 2012 +0100
 
    Added user should have name
$ git log
commit 2fad8841019ac484fe2f298b983219e0b13aea43
Author: Laszlo Papp <nucc_at_bteam_hu>
Date:   Sat Oct 27 21:36:34 2012 +0100

    Added user should have name

If you remove a testcase, the commit message will contain Removed instead of Added. So if you removed the previously added testcase, the commit message would look like this:

1
2
3
4
5
6
$ git log
commit 2fad8841019ac484fe2f298b983219e0b13aea43
Author: Laszlo Papp <nucc_at_bteam_hu>
Date:   Sat Oct 27 21:36:34 2012 +0100
 
    Removed user should have name
$ git log
commit 2fad8841019ac484fe2f298b983219e0b13aea43
Author: Laszlo Papp <nucc_at_bteam_hu>
Date:   Sat Oct 27 21:36:34 2012 +0100

    Removed user should have name

Plans

My next plan is to make association between test code coverage and git diff, because a testcase has been refactored if there is at least one line in its code coverage report that has changed till the last commit. I guess that would be really cool if I know from a commit message what testcases were affected by the change! ;)