是否可以在MiniTest中运行单个测试?

时间:2011-03-12 21:45:32

标签: ruby ruby-on-rails-3 minitest

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

rake test TEST=path/to/test_file.rb

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

我正在寻找类似的功能:

rspec path/to/test_file.rb -l 25

14 个答案:

答案 0 :(得分:191)

命令应为:

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

答案 1 :(得分:142)

你试过了吗?

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

答案 2 :(得分:47)

这是让我对测试中的string name definition感到困扰的事情之一。

当你有:

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/

答案 3 :(得分:47)

不需要宝石: ruby -Itest test/lib/test.rb --name /some_test/

来源:http://blog.arvidandersson.se/2012/03/28/minimalicous-testing-in-ruby-1-9

答案 4 :(得分:41)

  

我正在寻找与rspec path / to / file.rb -l 25

类似的功能

烨!使用Nick Quaranto's "m" gem。有了它,你可以说:

m spec/my_spec.rb:25

答案 5 :(得分:24)

如果您使用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

答案 6 :(得分:20)

您可以使用它来运行单个文件:

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

我也用过

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

以便更好地展示。

答案 7 :(得分:11)

有两种方法可以做到:

  1. 手动运行测试(参见Andrew Grimm的回答)。
  2. Hack Rake::TestTask目标使用不同的测试加载程序。
  3. Rake::TestTask(来自rake 0.8.7)理论上可以使用MiniTest::Unit命令行选项将其他选项传递给"TESTOPTS=blah-blah",例如:

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

    实际上,由于rake内部,选项--name(测试名称的过滤器)将不起作用。要解决这个问题,你需要在你的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

答案 8 :(得分:7)

您实际上只能运行名称甚至数字:

-n, --name PATTERN               Filter run on /pattern/ or string.

例如:

ruby spec/stories/foo_spec.rb --name 3

FAIL (0:00:00.022) test_0003_has foo
Expected: "foo"
Actual: nil

请参阅:https://github.com/seattlerb/minitest

答案 9 :(得分:1)

如果您使用Minitest的Turn gem,请确保使用Turn.config.pattern选项,因为Turn Minitest runner不尊重ARG中的--name选项。

答案 10 :(得分:0)

我使用this.bag[i].cartTotal

编辑: ruby /path/to/test -n /distinguishable word/-n的简写。 --name可以是您在测试说明中输入的任何字符串,我通常使用一些随机的单词,我知道这些单词不会出现在其他测试的说明中。

答案 11 :(得分:0)

以下将起作用

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

答案 12 :(得分:-1)

  

我正在寻找类似的功能:

     

rspec path / to / test_file.rb -l 25

有一颗宝石正是这样做的:minitest-line

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

来自https://github.com/judofyr/minitest-line#minitest-line

答案 13 :(得分:-1)

rails test path/to/test_file.rb:25会做到