Minitest 和 Rspec [英] Minitest and Rspec

查看:27
本文介绍了Minitest 和 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 支持使用接受参数的一流构造(共享示例组)指定共享行为;w/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 给我的强大功能非常有用.然而,许多人发现它太过分了,而且额外的抽象会增加认知成本.

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.

以下是我认为 minitest 缺乏的 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_blahit 'blah' 样式.
  • 代码库非常小且简单.相比之下,RSpec 凭借其较旧的年龄和附加功能而更大.
  • Minitest 的加载速度比 RSpec 快(与 RSpec 的许多文件分布在 3 个 gem 中相比,它大约有 4 个代码文件)——但请注意,RSpec 绝不慢;在我最近的大多数项目中,我会在不到一秒(通常不到 500 毫秒)内从 RSpec 获得测试反馈.

总的来说,有点像 Sinatra vs. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆