是否可以在 MiniTest 中运行单个测试?
我可以在一个文件中运行所有测试:
rake test TEST=path/to/test_file.rb
但是,如果我只想在该文件中运行一个测试,我该怎么做?
我正在寻找类似的功能:
rspec path/to/test_file.rb -l 25
I can run all tests in a single file with:
rake test TEST=path/to/test_file.rb
However, if I want to run just one test in that file, how would I do it?
I'm looking for similar functionality to:
rspec path/to/test_file.rb -l 25
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
命令应该是:
The command should be:
你有没有尝试过:
Have you tried:
不需要宝石:
ruby -Itest test/lib/test.rb --name /some_test/
来源:http://blog.arvidandersson.se/2012/03/28/minimalicous-testing-in-ruby-1-9
No gem required:
ruby -Itest test/lib/test.rb --name /some_test/
Source: http://blog.arvidandersson.se/2012/03/28/minimalicous-testing-in-ruby-1-9
这是测试中的
字符串名称定义
让我烦恼的事情之一。当你有:
你总是知道你的测试是如何命名的,所以你可以像这样执行它:
但是当你有这样的事情:
你永远不确定这个测试在内部是如何真正命名的,所以你不能使用
-n< /code> 直接选项即可。
要了解此测试在内部的命名方式,您只有一个选择:执行整个文件以尝试在日志中查找。
我的解决方法是(暂时)在测试名称中添加一些非常独特的内容,例如:
然后使用“-n”参数的 RegEx 版本,例如:
This is one of the things that bother me about the
string name definition
in tests.When you have:
you always know how your test is named so you can execute it like this:
But when you have something like:
you are never sure how this test is really named internally so you can not use the
-n
option just directly.To know how this test is named internally you only have an option: execute the whole file to try to figure out looking in the logs.
My workaround is (temporally) add something to the test name very unique like:
and then use the RegEx version of the '-n' parameter like:
With Nick Quaranto 的“m” gem 类似的功能,你可以说:
With Nick Quaranto's "m" gem, you can say:
如果您将 MiniTest 与 Rails 5+ 结合使用,在单个文件中运行所有测试的最佳方法是:
对于单个测试(例如第 25 行):
请参阅 http://guides.rubyonrails.org/testing.html#the-rails-test-runner
If you are using MiniTest with Rails 5+ the best way to run all tests in a single file is:
And for a single test (e.g. on line 25):
See http://guides.rubyonrails.org/testing.html#the-rails-test-runner
您可以使用它来运行单个文件:
我也使用它
来更好地显示。
You can use this to run a single file:
I also used
for better display.
您可以传递
--name
通过名称或名称中的数字来运行测试:例如:
此标志已记录 Minitest 的自述文件。
You can pass
--name
to run a test by its name or a number within its name:e.g.:
This flag is documented in Minitest’s README.
有两种方法可以做到这一点:
Rake::TestTask
(从 rake 0.8.7 开始)理论上可以通过"TESTOPTS=blah-blah"< 将附加选项传递给
MiniTest::Unit
/code> 命令行选项,例如:实际上,由于 rake 内部的原因,选项
--name
(测试名称的过滤器)将不起作用。要解决这个问题,您需要在 Rakefile 中编写一个小猴子补丁:此补丁要求您创建一个文件
test/my-minitest-loader.rb
:要打印 Minitest 的所有可能选项,类型
There are 2 ways to do it:
Rake::TestTask
target to use a different tests loader.Rake::TestTask
(from rake 0.8.7) theoretically is able to pass additional options toMiniTest::Unit
with a"TESTOPTS=blah-blah"
command line option, for example:In practice, the option
--name
(a filter for test names) won't work, due to rake internals. To fix that you'll need to write a small monkey patch in your Rakefile:This patch requires you to create a file
test/my-minitest-loader.rb
:To print all possible options for Minitest, type
我使用的是 Rails 版本 4.2.11.3 和 Ruby 版本 2.4.7p357
下面一个对我有用。
I am in Rails Version 4.2.11.3 and Ruby Version 2.4.7p357
Below one worked for me.
Rails 7.1 引入用于测试的特定行的新语法
现在您不仅可以运行特定测试按单行,
也可按行范围
,甚至按几个范围
Rails 7.1 introduce new syntax for specific lines for tests
Now you can run not only specific tests by single line
but also by lines range
or even by few ranges
我使用 ruby /path/to/test -n /distinguishable word/
编辑:
-n
是--name
的简写。可区分单词
可以是您在测试描述中放入的任何字符串,我通常使用一些我知道不会出现在其他测试描述中的随机单词。I use
ruby /path/to/test -n /distinguishable word/
Edit:
-n
is a shorthand for--name
.distinguishable word
can be any string you put in the test description, I usually use some random word that I know won't be present in other tests' descriptions.如果您将 Turn gem 与 minitest 一起使用,请确保使用
Turn.config.pattern
选项,因为 Turn Minitest 运行程序不考虑 ARG 中的 --name 选项。If you are using Turn gem with minitest, just make sure to use
Turn.config.pattern
option since Turn Minitest runner doesn't respect --name option in ARGs.有一个 gem 可以做到这一点:
minitest-line
。来自 https://github.com/judofyr/minitest-line#minitest-line
There is a gem that does exactly that:
minitest-line
.from https://github.com/judofyr/minitest-line#minitest-line
以下将起作用
这可以通过以下方式运行
Following will work
This can run by
安装 gem minitest-focus 并使用关键字 focus on test/spec ,如下所示仅运行具体测试。
这不需要传递任何命令行参数。
Install gem minitest-focus and use the keyword focus on test/spec like below to run only the specific test.
This would not need any command line argument to be passed.
如果您使用
Minitest::TestTask
并且没有 Rails,您还可以使用N
环境变量传递测试的名称:rake test N=test_some_test_name
If you're using
Minitest::TestTask
and no Rails, you can also pass the name of a test using theN
environment variable:rake test N=test_some_test_name