如何设置MiniTest?
我是一个相当新手的测试人员,但一直在努力提高 Rails 中的 TDD。
RSpec 工作得很好,但我的测试非常慢。我听说 MiniTest 速度要快得多,而且 MiniTest/Spec DSL 看起来与我习惯使用 RSpec 的方式非常相似,所以我想尝试一下。
但是,我无法在网络上找到任何提供如何设置和运行 Minitest 的演练的内容。我从 RSpec 书中学习了如何测试,但我不知道 Test::Unit 或 MiniTest 应该如何工作。我的 gem 文件中有 gem,我编写了一些简单的测试,但我不知道将它们放在哪里或如何运行它们。我认为这是显而易见的事情之一,没有人费心把它写下来......
任何人都可以向我解释如何设置一些 Minitest/spec 文件并让它们运行,以便我可以将性能与 Rspec 进行比较吗?
编辑
具体来说,这些是我最需要知道的基础知识:
- 您是否需要 test_helper 文件(如 spec_helper),如果需要,如何创建它?
- 如何运行 minitest? 似乎没有与
rspec spec
或rspec path/to/file_spec.rb
等效的东西,什么我失踪了吗?
谢谢!
I'm a fairly novice tester, but have been trying to get better at TDD in Rails.
RSpec works great, but my tests are pretty slow. I've heard that MiniTest is a lot faster, and the MiniTest/Spec DSL looks pretty similar to how I'm used to working with RSpec, so I thought I'd give it a try.
However, I have not been able to find anything on the web that provides a walkthrough of how to setup and run Minitest. I learned how to test from the RSpec book, and I have no idea how Test::Unit or MiniTest are supposed to work. I have the gem in my gemfile, I've written a few simple tests, but I have no idea where to put them or how to run them. I figure this is one of those things that's so obvious nobody has bothered to write it down...
Can anyone explain to me how to setup some some Minitest/spec files and get them running so I can compare the performance against Rspec?
EDIT
Specifically these are the basics I most need to know:
- Do you need a test_helper file (like spec_helper) and if so how do you create it?
- How do you run minitest? There doesn't seem to be an equivalent to
rspec spec
orrspec path/to/file_spec.rb
, what am I missing?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这个问题类似于 如何使用 minitest 运行所有测试?
使用 Ruby 1.9.3 和 Rake 0.9.2.2,给定如下所示的目录布局:
所示:
alpha_spec.rb
可能如下Rakefile
您可以运行
rake 测试
ruby -Ilib spec/alpha_spec。 rb
我不知道将
spec_helper.rb
与 minitest 一起使用是否常见。似乎没有一种方便的方法来加载它。将其添加到 Rakefile:然后
spec/spec_helper.rb
可以包含各种冗余的内容:并且
spec/alpha_spec.rb
将冗余部分替换为:rake 测试
ruby -Ilib -Ispec spec/alpha_spec.rb
This question is similar to How to run all tests with minitest?
Using Ruby 1.9.3 and Rake 0.9.2.2, given a directory layout like this:
Here is what
alpha_spec.rb
might look like:And here's
Rakefile
You can run
rake test
ruby -Ilib spec/alpha_spec.rb
I don't know if using a
spec_helper.rb
with minitest is common or not. There does not appear to be a convenience method for loading one. Add this to the Rakefile:Then
spec/spec_helper.rb
can contain various redundant things:And
spec/alpha_spec.rb
replaces the redundant parts with:rake test
ruby -Ilib -Ispec spec/alpha_spec.rb
请注意,watchr 或 spork 不是运行测试的要求。它们为进行自动测试提供了便利。但运行一组 MiniTest 测试的最基本方法是使用 ruby 本身:
Note that watchr or spork are not requirements for running tests. They're a convenience for doing autotesting. But the most basic way you can run a set of MiniTest tests is with ruby itself:
您使用什么版本的 Ruby?
据我了解,1.9 引入了
MiniTest
,它完全取代了Test::Unit
,并具有向后兼容的 API。因此,如果您使用的是 1.9 并且您的 Rails 应用程序引用了Test::Unit
,那么它实际上已经在使用MiniTest
。查看您的 Ruby 源代码 - 1.9.2 的
test/unit.rb
具有以下要求:并且
test/unit/assertions
包括MiniTest::Assertions
。让你的测试跑得更快完全是另一门黑暗艺术。看看 spork 和 观察者。前者使您的应用程序保持初始化,重新加载修改的文件,并在每次运行测试套件之前进行分叉。后者监视存储库中的文件是否发生更改,并自动运行其关联的测试用例。
它们一起为您提供了一个非常简单的 TDD 设置。您为新模型编写一个测试用例,它会自动运行并失败。然后,每当您保存关联的模型时,该测试用例都会很快重新运行。您几乎可以立即获得有关您是红色/绿色的反馈。
他们都有点难以设置并表现得很好,所以如果你遇到困难,请再检查一些问题。
祝你好运!
What version of Ruby are you using?
In my understanding, 1.9 introduced
MiniTest
, which replacesTest::Unit
entirely, with a backwards compatible API. So if you're on 1.9 and your Rails app referencesTest::Unit
, it's actually already usingMiniTest
.Take a look at your Ruby source -
test/unit.rb
for 1.9.2 has the following requires:And
test/unit/assertions
includesMiniTest::Assertions
.Getting your tests to run faster is a whole other dark art. Take a look at spork and watchr. The former keeps your app initialized, reloads modified files, and forks before each run of your test suite. The latter watches files in your repo for changes, and automatically runs their associated test cases.
Together they give you a pretty mean TDD setup. You write a test case for your new model, and it automatically runs and fails. Then whenever you save the associated model, that testcase re-runs, very quickly. You get near instant feedback on whether you're red/green.
They're both a little tricky to get set up and behaving nicely together, so check back with some more questions if you get stuck.
Best of luck!
观看入门视频了解 minitest-rails。它逐步介绍了为 Rails 应用程序设置 Minitest 的过程。
http://www.youtube.com/watch?v=xA2f2zBNvsc
该演练展示了如何创建帮助程序文件以及如何使用 rake 任务运行测试。如果您想运行特定测试,可以执行以下操作:
Check out this getting started video for minitest-rails. It walks through the process for setting up Minitest for a Rails app.
http://www.youtube.com/watch?v=xA2f2zBNvsc
The walkthrough shows how to create a helper file as well as how to run the tests using a rake task. If you want to run a specific test you can do so with the following:
RSpec 的较新版本(自 2012 年 1 月发布 2.8 起)的速度有了显着提高。 这里是对性能差异以及与 MiniTest 的比较。
另外,我发现 Ryan Bates 的这个截屏视频是对 MiniTest 的精彩介绍。请注意,这是专业视频之一,您需要订阅才能观看。
Newer versions of RSpec (since 2.8 released in Jan 2012) have a dramatic speed increase. Here is an exploration of performance differences and a comparison with MiniTest.
Also, I found this screencast by Ryan Bates an excellent introduction to MiniTest. Note that this is one of the pro videos and you will need to subscribe to watch it.
这是我的整个
rakefile
,我将其放在顶级目录中:要立即运行所有 Minitest 文件,我只需输入
rake
。就是这样!确保每个 Minitest 文件的顶部都有
require 'minitest/autorun'
。 Dir.glob 绝对可以与 Minitest 一起使用。为了获得漂亮的彩色 Minitest 输出以及所有测试方法的名称,我在 /test 目录中有文件
minitest_helper.rb
。 (必须安装 gem minitest-reporters):我只需在每个测试文件的顶部添加
require_relative './minitest_helper'
即可。Here is my entire
rakefile
, which I put in my top directory:To run all my Minitest files at once, I just type
rake
. That's it!Make sure to have
require 'minitest/autorun'
at the top of each of your Minitest files. Dir.glob definitely DOES work with Minitest.To get pretty, colored Minitest output, with names of all my test methods, I have the file
minitest_helper.rb
in my /test directory. (Had to install the gem minitest-reporters):I just had to
require_relative './minitest_helper'
at the top of each of my test files.帕特里克写了一个非常好的解释和例子。对于未来的读者,请注意全局期望现在已经贬值,并将在 Minitest 6 中删除。它需要在调用
expect
中包装您正在测试的内容时进行几乎微不足道的更改。Patrick wrote a really good explanation and example. For future readers just be aware that global expectations are now depreciated, and will be removed in Minitest 6. It requires an almost trivial change in wrapping the thing you are testing in a call to
expect
.