image

  • フォト Amazonギフト券
    ※この時計の時刻は、閲覧しているパソコンのものであり、必ずしも正確な時間とは限りません

検索

最近のトラックバック

無料ブログはココログ

« ピックアップ: NeoOffice 2.2.2 is released, 基礎体力より基礎代謝, etc... | トップページ | ピックアップ: Gmail保存容量大幅拡大のお知らせ, Webサイトの質を高める為には低機能パソコンが必要, etc... »

2007-10-14

rspec + rcov でテストカバレージをとることを試した

rspec + rcov でテストカバレージをとることを試した。

0. ファイル構成
1. 対象 ruby クラス
2. spec ファイル
3. Rakefile
4. 実行結果

0. ファイル構成
==============
  |-- Rakefile
  |-- spec
  |   `-- foo_spec.rb
  `-- src
      `-- foo.rb

1. 対象 ruby クラス
==================

$ cat foo.rb
class Foo
  def add x, y
    x + y
  end

  def sub x, y
    x - y
  end

  def div x, y # not use
    x / y
  end
end

2. spec ファイル
===============

$ cat foo_spec.rb
require 'foo'

describe Foo, "add " do
  it "should return sum." do
    f = Foo.new
    f.add(1,2).should == 3
  end
end

describe Foo, "sub " do
  it "should return sub." do
    f = Foo.new
    f.sub(1,2).should == -1
  end
end

3. Rakefile
============

$ cat Rakefile
require 'rake'
require 'spec/rake/spectask'

desc "Run all examples with Rcov"
Spec::Rake::SpecTask.new('rspec_with_rcov') do |t|
  t.spec_files = FileList['spec/**/*.rb']
  t.libs << "src"
#  t.warning = true
  t.rcov = true
  t.spec_opts = ['--format', 'specdoc', '--color']
  t.rcov_opts = ['--exclude', 'spec']
end

desc "Run rspec"
Spec::Rake::SpecTask.new('rspec') do |t|
  t.spec_files = FileList['spec/**/*.rb']
  t.libs << "src"
  t.spec_opts = ['--format', 'specdoc', '--color']
end

task :clean =>[:clobber_rspec_with_rcov] do
sh "rm -f *~"
sh "rm -f */*~"
end

4. 実行結果
============
$ rake rspec_with_rcov
(in /Users/kato/work/src/ruby/rspec/rcov)

Foo add
- should return sum.

Foo sub
- should return sub.

Finished in 0.077517 seconds

2 examples, 0 failures

coverge/*.html が生成される。(rake clean で削除できる)
# わざと、spec から漏れているメソッドをつくり、coverage でそれが報告されていることを確かめている。
生成されたレポートのスクリーンショット

Rcov01

Rcov02

現状は jruby で gem install rcov できない。
java + jruby で、 java/ruby のどちらのコードも区別なくカバレージ計測できようになるべきだ。


« ピックアップ: NeoOffice 2.2.2 is released, 基礎体力より基礎代謝, etc... | トップページ | ピックアップ: Gmail保存容量大幅拡大のお知らせ, Webサイトの質を高める為には低機能パソコンが必要, etc... »

コメント

この記事へのコメントは終了しました。

トラックバック


この記事へのトラックバック一覧です: rspec + rcov でテストカバレージをとることを試した:

« ピックアップ: NeoOffice 2.2.2 is released, 基礎体力より基礎代謝, etc... | トップページ | ピックアップ: Gmail保存容量大幅拡大のお知らせ, Webサイトの質を高める為には低機能パソコンが必要, etc... »

mokuji

2013年12月
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        

google

  • twitter
  • __
  • _
    Googleボットチェッカー

合わせて読む

  • 合わせて読む
    フィードメーター - katoy: cocolog あわせて読みたい

リンク