Whats my Coverage? (C0 C1 C2 C3 + Path)

100% coverage sounds great, but is it c0, c1, c2, c3 or path coverage ? If you do not know, here is the answer:

  • C0 = every instruction
  • C1 = every branch if(i==1) a; even if there is no actual instruction in the i!=1 path it needs 2 tests
    1. i == 1
    2. i != 1
  • C2 + C3 ~= is every condition inside an ‘if’ is once true and once false (personally, i could not care less…)
  • C4: Path-coverage = every possible path was taken, if(a) x else b; if(c) y requires 4 tests
    1. a true c true
    2. a true c false
    3. a false c true
    4. a false c false

Try to aim for ~95% C0 and ~70% C1. C2 and C3 in my opinion add no value(except that they cover more paths) and C4 is often not possible, since a loop can have infinite paths (go through 1,2,3,4… times).

Testing Validation the DRY Way

UPDATE: Please have a look at valid_attributes

We all know the discussion: should we test simple validation or not ? Here is a great argument for testing even the most simple validation logic.

assert_invalid_attributes User, :login=>[”,nil,’admin’], :email=>[”,nil,’aa’]

Can it get any simpler ?

It goes through all of them and and tests them one by one, giving hepful error messages like:

<User.login> expected to be invalid when set to <admin>

All you need to do is define a @valid_attributes = {:username=>’foo’…} hash in your test setup to test things like ‘password is not username’ and never worry about validations again!

This goes to test/test_helper.rb or spec/spec_helper.rb

  #create a set of invalid attributes
def invalid_attributes search='', replace=''
  @valid_attributes ||= {}
  @valid_attributes[search]=replace unless search.blank?
  @valid_attributes
end

#idea: http://www.railsforum.com/viewtopic.php?id=741
#example: User, :login=>['',nil,'admin'], :email=>['',nil,'aa','@','a@','@a']
def assert_invalid_attributes(model_class, attributes)
  attributes.each_pair do |attribute, value|
    assert_invalid_value model_class, attribute, value
  end
end

#idea: http://www.railsforum.com/viewtopic.php?id=741
#example: User, :login, ['',nil,'admin']
def assert_invalid_value(model_class, attribute, value)
  if value.kind_of? Array
     value.each { |v| assert_invalid_value model_class, attribute, v }
  else
    attributes = invalid_attributes(attribute,value)
    record = model_class.new(attributes)
    assert_block "<#{model_class}.#{attribute}> expected to be invalid when set to <#{value}>" do
      record.valid? # Must be called to generate the errors
      record.errors.invalid? attribute
    end
  end
end

Why WordPress sucks

  • & sometimes get converted to & and then i have to recheck the code block
  • &nbsp; gets removed when switching to visual mode
  • no feedburner support
  • Images just get replaced by others, i had a RCov screenshot and one from jqUnit, now on the Frontpage of ajaxian you can see this: (hope i can fix it later…)

Ajaxian

  • the thumbnail still looks fine…

Thumbnail

  • they cant even get the date right, it is not that old 🙂
  • i cannot replace the file, and i could overwrite it, as long as i am in the same month (delete, then upload an image with the same name), but when i do that, the old image reappears…
  • using inline style and code with square brackets results in normalquotes beeing rendered as slanted/italic quotes, rendering the code un-pasteable

Seems to me, they are missing a lot of automated testing….

If someone can recommend me another hasslefree Blog provider, with images and statistics and maybe some nice code highlighting, drop a comment 😉

Convert Test::Unit to RSpec by Script

Living with Test and Spec at the same time is annoying, so here is a small Howto for conversion using the Test::Unit to RSpec converter.

  1. Change spec/spec_helper.rg ‘config.fixture_path =‘ to test/fixtures OR copy all fixtures from test to spec (svn cp test/fixtures spec/fixtures)
  2. copy old code from test_helper to spec_helper (leave includes outside of Spec::Runner.configure do…)
  3. sudo gem install spec_converter
  4. convert all tests ‘spec_converter
  5. correct syntax errors
  6. search and replace ‘test/xxx’ with ‘spec/xxx’ where neccessary
  7. run ‘rake spec
  8. dance if result == :success

Works very nice for me, just one syntax error to correct, and then everything runs. Not all old asserts will be replaced, but this is not problem, since RSpec can work with Test::Unit assertions.

Instant Bug to Testcase

Today i found a small new plugin, that offers a simple way to convert bugs to a testcase.

script/plugin install http://svn.extendviget.com/lab/laziness/trunk

Now every failing request will print a small test case to repeat this request, it is not much, but it is a starting point and can be helpful if you have a lot of parameters being passed.

Output:
Laziness
def test_get_rating_edit_should_not_raise_activerecord_recordnotfound_exception
  assert_nothing_raised(ActiveRecord::RecordNotFound) do
    get :edit, {"id"=>"1"}, {:user_id=>nil, :return_to=>"/orders/1"}, {}, {"_session_id"=>["…"]}
  end
end
Patch:

Atm you need to apply this patch to make it work without exception_notification.

#vendor/plugins/laziness/lib/laziness.rb
module Laziness
  begin
    module ExceptionNotifier
      ::ExceptionNotifier.sections << 'laziness'
    end
  rescue
  end