Minitest和Rspec [英] Minitest and Rspec
问题描述
我刚刚观看了Minitest的 Railscast .
I have just watched a Railscast for Minitest.
使用RSpec和Minitest来测试Rails应用程序的优缺点是什么?从RSpec转换为Minitest会失去哪些功能?
What are the pros and cons for using RSpec vs Minitest for testing a rails app? Which features will I lose converting from RSpec to Minitest?
推荐答案
我是RSpec开发人员之一,并且从未使用过minitest,因此在阅读此答案时请考虑我的偏见.
I'm one of the RSpec developers, and have never used minitest, so take my biases into account when reading this answer.
总的来说,RSpec的强大之处在于它将许多测试概念转化为一流的对象.在Test :: Unit和Minitest使用简单方法进行断言的情况下,RSpec使用支持否定,自我描述等功能的一流匹配器对象. RSpec的示例是支持丰富元数据的一流对象. minitest/spec将it
编译成简单的方法,不支持相同类型的丰富元数据. RSpec支持使用接受参数的一流构造(共享示例组)来指定共享行为.使用minitest,您可以使用继承或混合来重复使用测试,但是它没有同等的一流支持. RSpec有一个显式的格式化程序API(并且有许多使用它的第三方格式化程序);我不知道minitest具有相同类型的一流格式化程序API.
By and large, RSpec's power comes from the fact that it reifies so many testing concepts into first class objects. Where Test::Unit and Minitest use simple methods for making assertions, RSpec uses first-class matcher objects that support negation, self-description and more. RSpec's examples are first-class objects that support rich metadata; minitest/spec compiles it
blocks down into simple methods, which don't support the same sort of rich metadata. RSpec supports specifying shared behaviors using a first-class construct (shared example groups) that accepts arguments; w/ minitest you can use inheritance or a mixin to re-use tests, but it doesn't have the same sort of first-class support. RSpec has an explicit formatter API (and there are many third party formatters that use it); I'm not aware of minitest having the same sort of first-class formatter API.
作为一整天都在不断进行测试和练习TDD的人,我发现RSpec赋予我的力量非常有用.但是,许多人发现它过于矫kill过正,而额外的抽象又增加了认知成本.
As somebody who is constantly running tests and practicing TDD all day long, I find the power RSpec gives me to be very useful. Many people find it to be overkill, though, and there is an added cognitive cost to the extra abstractions.
我认为minispec缺少RSpec的一些特定功能:
Here are some specific features RSpec has that I believe minitest lacks:
-
before(:all)
挂钩(请注意,这是RSpec的高级用户功能,应该很少使用;在使用RSpec的许多年中,我仅在少数情况下使用过此功能) -
around(:each)
挂钩 - 共享的示例组
- 共享上下文
- 丰富的元数据支持,可用于控制运行哪些示例,共享上下文中包含哪些示例组,哪些示例模块中混入了更多示例.
- 对带有rspec-mocks的各种模拟功能的集成支持; Minitest :: Mock相比起来简单得多,而且局限性很大.
- RSpec具有 rspec-fire ,真棒.
before(:all)
hooks (note this is a power user feature of RSpec that should rarely be used; I've only used it on a few occasions in many years of using RSpec)around(:each)
hooks- Shared example groups
- Shared contexts
- Rich metadata support that can be used to control which examples get run, which example groups shared contexts get included in, which example groups modules get mixed into and more.
- Integrated support for a wide range of mocking features w/ rspec-mocks; Minitest::Mock is far simpler and more limited in comparison.
- RSpec has rspec-fire, which is awesome.
使用Minitest的好处:
Benefits of using Minitest:
- 它内置在标准库中,因此您不需要安装任何额外的东西.
- 它可以用于
def test_blah
或it 'blah'
样式. - 代码库非常小而简单. RSpec年龄较大且具有附加功能,因此相比而言更大.
- Minitest的加载速度比RSpec快(与RSpec相比,大约有4个代码文件,而RSpec的文件分布在3个gem上),但是请注意,RSpec绝不慢.如今,在我大多数项目中,我在一秒钟之内(通常不到500毫秒)就收到了RSpec的测试反馈.
- It's built into the standard library so you don't need to install anything extra.
- It can either be used in
def test_blah
orit 'blah'
styles. - The code base is very small and simple. RSpec, by virtue of it's older age and additional features, is larger in comparison.
- Minitest loads faster than RSpec (it's about 4 files of code compared to RSpec having many files spread across 3 gems)--but note that RSpec is by no means slow; in most of my projects these days, I get test feedback from RSpec in under a second (and often in under 500 ms).
总体而言,它有点像Sinatra与Rails,我认为Minitest和RSpec都是根据您的需求的不错选择.
Overall, it's a bit like Sinatra vs. Rails, and I think Minitest and RSpec are both fine choices depending on your needs.
最后一件事:如果您更喜欢Minitest的某些特定方面,但是您更喜欢RSpec的其他方面,则可以轻松地将它们混在一起.我为此写了一篇博客文章,如果您有兴趣.
One last thing: if there are specific aspects of Minitest you like better, but other things you like better about RSpec, they can easily be mixed and matched. I wrote a blog post about this, if you're interested.
这篇关于Minitest和Rspec的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!