我可以在一个文件中运行所有测试:
rake test TEST=path/to/test_file.rb
但是,如果我只想在该文件中运行一个测试,我该怎么做呢?
我正在寻找类似的功能:
rspec path/to/test_file.rb -l 25
我可以在一个文件中运行所有测试:
rake test TEST=path/to/test_file.rb
但是,如果我只想在该文件中运行一个测试,我该怎么做呢?
我正在寻找类似的功能:
rspec path/to/test_file.rb -l 25
有两种方法:
“手动”运行测试(参见Andrew Grimm的回答)。 Hack Rake::TestTask目标以使用不同的测试加载器。
Rake::TestTask(来自Rake 0.8.7)理论上能够通过“TESTOPTS=blah-blah”命令行选项将额外的选项传递给MiniTest::Unit,例如:
% rake test TEST=test/test_foobar.rb TESTOPTS="--name test_foobar1 -v"
在实践中,选项——name(测试名称的过滤器)将不起作用,因为rake的内部结构。为了解决这个问题,你需要在Rakefile中写一个小猴子补丁:
# overriding the default rake tests loader
class Rake::TestTask
def rake_loader
'test/my-minitest-loader.rb'
end
end
# our usual test terget
Rake::TestTask.new {|i|
i.test_files = FileList['test/test_*.rb']
i.verbose = true
}
这个补丁需要你创建一个文件test/my-minitest-loader.rb:
ARGV.each { |f|
break if f =~ /^-/
load f
}
要打印Minitest的所有可能选项,请键入
% ruby -r minitest/autorun -e '' -- --help
这是测试中字符串名称定义中困扰我的问题之一。
当你有:
def test_my_test
end
你总是知道你的测试是如何命名的,所以你可以像这样执行它:
ruby my_test -n test_my_test
但当你有这样的东西:
it "my test" do
end
你永远不知道这个测试在内部是如何命名的,所以你不能直接使用-n选项。
要了解这个测试在内部是如何命名的,您只有一个选择:执行整个文件以尝试在日志中查找。
我的解决方法是(暂时地)在测试名中添加一些非常独特的东西,比如:
it "my test xxx" do
end
然后使用'-n'形参的RegEx版本,例如:
ruby my_test.rb -n /xxx/
如果你在minitest中使用Turn gem,请确保使用Turn.config.pattern选项,因为Turn minitest运行器不尊重arg中的——name选项。
不需要宝石: ruby - test test/lib/testRb——name /some_test/
来源:http://blog.arvidandersson.se/2012/03/28/minimalicous-testing-in-ruby-1-9
你可以用它来运行一个文件:
rake test TEST=test/path/to/file.rb
我也用过
ruby -I"lib:test" test/path/to/file.rb
为了更好的显示。
你可以通过——name来运行一个测试,通过它的名字或者它名字中的一个数字:
-n, --name PATTERN Filter run on /regexp/ or string.
例如:
$ ruby spec/stories/foo_spec.rb --name 3
FAIL (0:00:00.022) test_0003_has foo
Expected: "foo"
Actual: nil
这个标志被记录在Minitest的README中。
如果你正在使用MiniTest和Rails 5+,最好的方法是在一个文件中运行所有测试:
bin/rails test path/to/test_file.rb
对于单个测试(例如在第25行):
bin/rails test path/to/test_file.rb:25
看到http://guides.rubyonrails.org/testing.html the-rails-test-runner
我正在寻找类似的功能: / / test_file rspec路径。Rb -l 25
有一种宝石正是这样做的:最小行。
gem install minitest-line
ruby test/my_file -l 5
从https://github.com/judofyr/minitest-line minitest-line
我使用ruby /path/to/test -n /distinguishable word/
编辑: -n是——name的缩写。可区分的单词可以是你放在测试描述中的任何字符串,我通常使用一些随机的单词,我知道这些单词不会出现在其他测试的描述中。
以下将会起作用
def test_abc
end
test "hello world"
end
这可以通过
bundle exec ruby -I test path/to/test -n test_abc
bundle exec ruby -I test path/to/test -n test_hello_word
我在Rails版本4.2.11.3和Ruby版本2.4.7p357
下面一个对我有用。
ruby -Itest <relative_minitest_file_path> --name /<test_name>/
安装gem minest -focus并使用关键字focus on test/spec,如下所示,只运行特定的测试。
focus
def test
end
focus
it "test" do
end
这将不需要传递任何命令行参数。