Ruby 1.9 Rspec 风格 Unit Test MiniTest::Spec介绍

MiniTest是1.9后加到ruby标准库里的。其中,有几个部分当在1.9中写
require 'test/unit'
时,会兼容的把MiniTest:Unit载入,然而MiniTest::Unit只是增加了assertions

比较显著的部分是MiniTest::Spec和MiniTest::Mock, 夜猪对此比较感冒,当然效果是比不了Rspec啦,比如before do还是不能支持before :all do这样的回调。对于ruby标准库来说还是不错的开始。简介如下:

例如之前的unittest可能会这么写
require 'test/unit'

class TestArray < Test::Unit::TestCase
  def test_array_can_be_created_with_no_arguments
    assert_instance_of Array, Array.new
  end

  def test_array_of_specific_length_can_be_created
    assert_equal 10, Array.new(10).size
  end
end

之后,可以这样写
require 'minitest/spec'
require 'minitest/autorun'

describe Array do
  it "can be created with no arguments" do
    Array.new.must_be_instance_of Array
  end

  it "can be created with a specific size" do
    Array.new(10).size.must_equal 10
  end
end


再来一个之前的setup和teardown

  require 'minitest/autorun'

  class TestMeme < MiniTest::Unit::TestCase
    def setup
      @meme = Meme.new
    end

    def test_that_kitty_can_eat
      assert_equal "OHAI!", @meme.i_can_has_cheezburger?
    end

    def test_that_it_will_not_blend
      refute_match /^no/i, @meme.will_it_blend?
    end
  end


变成如下:

  require 'minitest/autorun'

  describe Meme do
    before do
      @meme = Meme.new
    end

    describe "when asked about cheeseburgers" do
      it "must respond positively" do
        @meme.i_can_has_cheezburger?.must_equal "OHAI!"
      end
    end

    describe "when asked about blending possibilities" do
      it "won't say no" do
        @meme.will_it_blend?.wont_match /^no/i
      end
    end
  end
#这里是我说的那个,还是不支持所有测试before :all


下一篇准备说说MiniTest的Mock
这里也支持skip方法就是cucumber里的@wip

值得注意的是,这个 minitest/spec的支持只有短短300多行代码,有条件的推荐读。

然后写unit test一定想知道assertions都有啥吧

obj.must_be(operator, expected) # for example, 10.must_be :< , 11
obj.must_be_close_to # the equivalent of assert_in_delta
obj.must_be_empty - Fails unless obj.empty?
obj.must_be_instance_of(klass) # Fails unless obj.class == klass
obj.must_be_kind_of(klass) # Fails unless obj is of class klass or klass is one of its superclasses.
obj.must_be_nil
obj.must_be_same_as $ tests for true object equality
lambda {}.must_be_silent
obj.must_be_within_delta
obj.must_be_within_epsilon
obj.must_equal(other) # Does a ==/eql? comparison between two objects.
obj.must_include(other)
obj.must_match(regex) # A regular expression match, e.g. "hello".must_match /w+/
lambda {}.must_output(stdout, [stderr..]) # The block should have certain output on stdout or stderr. Set stdout to nil just to check stderr.
lambda {}.must_raise(exception)
obj.must_respond_to(message)
obj.must_throw(sym)
wont_be
wont_be_empty
wont_be_instance_of
wont_be_kind_of
wont_be_nil
wont_be_same_as
wont_equal
wont_include
wont_match
wont_respond_to

当然,也比原版的unit test的assertion好读了, 对应关系参考


最后,想要rake跑test目录下所有测试,添加如下Rakefile文件

require 'rake/testtask'

Rake::TestTask.new do |t|
  t.libs.push "lib"
  t.test_files = FileList['test/*_test.rb']
  t.verbose = true
end



关于benchmark放最后

  ## optionally run benchmarks, good for CI-only work!
  require 'minitest/benchmark' if ENV["BENCH"]

  class TestMeme < MiniTest::Unit::TestCase
    # Override self.bench_range or default range is [1, 10, 100, 1_000, 10_000]
    def bench_my_algorithm
      assert_performance_linear 0.9999 do |n| # n is a range value
        @obj.my_algorithm(n)
      end
    end
  end

#或者放到spec里
  describe Meme do
    if ENV["BENCH"] then
      bench_performance_linear "my_algorithm", 0.9999 do |n|
        100.times do
          @obj.my_algorithm(n)
        end
      end
    end
  end


输出如下:

  TestBlah	100	1000	10000
  bench_my_algorithm	 0.006167	 0.079279	 0.786993
  bench_other_algorithm	 0.061679	 0.792797	 7.869932


你可能感兴趣的:(test,unit,Ruby,rspec,minitest)