我可以在一个文件中运行所有测试:

rake test TEST=path/to/test_file.rb

但是,如果我只想在该文件中运行一个测试,我该怎么做呢?

我正在寻找类似的功能:

rspec path/to/test_file.rb -l 25

当前回答

你有没有试过:

ruby path/to/test_file.rb --name test_method_name

其他回答

安装gem minest -focus并使用关键字focus on test/spec,如下所示,只运行特定的测试。

focus
def test
end


focus
it "test" do
end

这将不需要传递任何命令行参数。

你可以用它来运行一个文件:

rake test TEST=test/path/to/file.rb

我也用过

ruby -I"lib:test" test/path/to/file.rb

为了更好的显示。

命令应该是:

% rake test TEST=test/test_foobar.rb TESTOPTS="--name=test_foobar1 -v"

我正在寻找类似的功能: / / test_file rspec路径。Rb -l 25

有一种宝石正是这样做的:最小行。

gem install minitest-line
ruby test/my_file -l 5

从https://github.com/judofyr/minitest-line minitest-line

这是测试中字符串名称定义中困扰我的问题之一。

当你有:

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/