Ruby 1.9 Rspec 风格 Unit Test MiniTest::Spec介绍
发布日期:2021-09-29 20:09:23 浏览次数:6 分类:技术文章

本文共 3835 字,大约阅读时间需要 12 分钟。

MiniTest是1.9后加到ruby标准库里的。其中,有几个部分当在1.9中写
require 'test/unit'
时,会兼容的把MiniTest:Unit载入,然而MiniTest::Unit只是增加了assertions
比较显著的部分是MiniTest::Spec和MiniTest::Mock,[url=http://hlee.iteye.com]夜猪[/url]对此比较感冒,当然效果是比不了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
值得注意的是,这个[url=https://github.com/seattlerb/minitest/blob/master/test/test_minitest_spec.rb]minitest/spec[/url]的支持只有短短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好读了,[url=https://github.com/seattlerb/minitest/blob/master/lib/minitest/spec.rb]对应关系参考[/url]
最后,想要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

转载地址:https://blog.csdn.net/horace_lee/article/details/84085772 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:MiniTest::Mock Ruby1.9 标准库支持
下一篇:Module方法调用好不好

发表评论

最新留言

感谢大佬
[***.8.128.20]2024年02月28日 17时26分00秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

8k分辨率需要多大带宽_超乎想象!用RTX3080显卡连索尼8K电视玩游戏感受如何?... 2019-04-21
win10怎么开启aptx_Win10未来的黑科技?微软SurfaceFleet大曝光 2019-04-21
creo视图管理器使用方法_学以致用之中望3D—浅谈使用中望3D的初步感受 2019-04-21
周育如的音标口诀大全_花鸟画口诀大全,实用! 2019-04-21
心电图计算心率公式_医学常用的计算公式口诀(内外妇儿),赶快收藏! 2019-04-21
select 移动端 第一个无法选中_Python爬虫微博(移动端)评论 2019-04-21
华为云welink成像是反的_华为发布智能办公神器WeLink,可连接会议室开会,还可一键遥控报销和智能翻译... 2019-04-21
唱好铁血丹心谐音正规_趙贤典:打好“感情牌” 唱好“大合唱” 2019-04-21
aix系统vi修改命令_Linux基础知识必备:利用vi编辑器创建和编辑正文文件 2019-04-21
天涯明月刀开发_玩家被天涯明月刀手游“冷落”?六大门派角色竟不带正眼看人... 2019-04-21
this指向undefined uiapp_一个this都没有,真好 2019-04-21
add p4 多个文件_2-3【微信小程序全栈开发课程】index页面完善--vue文件代码解析... 2019-04-21
5w2h原则指的是什么_什么是5W2H分析法?一首小诗带入进入大门。 2019-04-21
技校毕业是什么学历_中等职业学校是什么_中等职业学校毕业是什么学历 2019-04-21
2压缩备份数据库_MySQL数据备份与恢复(二) xtrabackup工具 2019-04-21
英特尔cpu发布时间表_被嘲讽的英特尔核显,强大能力其实超乎你的想象 2019-04-21
chi2inv函数 matlab_MATLAB概率和统计(2) 2019-04-21
lisp修改上一个图素_在Windows上安装Haskell 2019-04-21
ad19 导出step 没有pcb_几款主流PCB软件哪个最好用,你用过几款? 2019-04-21
json mysql 字段 默认值_Newtonsoft.Json 六个超简单又实用的特性,值得一试 【上篇】... 2019-04-21