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

发布于 2024-10-21 08:04:35 字数 199 浏览 3 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(17

写给空气的情书 2024-10-28 08:04:35

命令应该是:

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

The command should be:

% rake test TEST=test/test_foobar.rb TESTOPTS="--name=test_foobar1 -v"
年少掌心 2024-10-28 08:04:35

你有没有尝试过:

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

Have you tried:

ruby path/to/test_file.rb --name test_method_name
又爬满兰若 2024-10-28 08:04:35

不需要宝石:
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

执着的年纪 2024-10-28 08:04:35

这是测试中的字符串名称定义让我烦恼的事情之一。

当你有:

def test_my_test
end

你总是知道你的测试是如何命名的,所以你可以像这样执行它:

ruby my_test -n test_my_test

但是当你有这样的事情:

it "my test" do
end

你永远不确定这个测试在内部是如何真正命名的,所以你不能使用 -n< /code> 直接选项即可。

要了解此测试在内部的命名方式,您只有一个选择:执行整个文件以尝试在日志中查找。

我的解决方法是(暂时)在测试名称中添加一些非常独特的内容,例如:

it "my test xxx" do
end

然后使用“-n”参数的 RegEx 版本,例如:

ruby my_test.rb -n /xxx/

This is one of the things that bother me about the string name definition in tests.

When you have:

def test_my_test
end

you always know how your test is named so you can execute it like this:

ruby my_test -n test_my_test

But when you have something like:

it "my test" do
end

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:

it "my test xxx" do
end

and then use the RegEx version of the '-n' parameter like:

ruby my_test.rb -n /xxx/
も让我眼熟你 2024-10-28 08:04:35

我正在寻找与 rspec path/to/file.rb -l 25 类似的功能

With Nick Quaranto 的“m” gem 类似的功能,你可以说:

m spec/my_spec.rb:25

I'm looking for similar functionality to rspec path/to/file.rb -l 25

With Nick Quaranto's "m" gem, you can say:

m spec/my_spec.rb:25
海拔太高太耀眼 2024-10-28 08:04:35

如果您将 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

If you are using MiniTest with Rails 5+ the best way to run all tests in a single file is:

bin/rails test path/to/test_file.rb

And for a single test (e.g. on line 25):

bin/rails test path/to/test_file.rb:25

See http://guides.rubyonrails.org/testing.html#the-rails-test-runner

摇划花蜜的午后 2024-10-28 08:04:35

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

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

我也使用它

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

来更好地显示。

You can use this to run a single file:

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

I also used

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

for better display.

晒暮凉 2024-10-28 08:04:35

您可以传递 --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 的自述文件

You can pass --name to run a test by its name or a number within its name:

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

e.g.:

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

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

This flag is documented in Minitest’s README.

一刻暧昧 2024-10-28 08:04:35

有两种方法可以做到这一点:

  1. “手动”运行测试(参见 Andrew Grimm 的回答)。
  2. Hack Rake::TestTask 目标以使用不同的测试加载器。

Rake::TestTask(从 rake 0.8.7 开始)理论上可以通过 "TESTOPTS=blah-blah"< 将附加选项传递给 MiniTest::Unit /code> 命令行选项,例如:

% 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

There are 2 ways to do it:

  1. Run tests 'manually' (see Andrew Grimm's answer).
  2. Hack Rake::TestTask target to use a different tests loader.

Rake::TestTask (from rake 0.8.7) theoretically is able to pass additional options to MiniTest::Unit with a "TESTOPTS=blah-blah" command line option, for example:

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

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:

# 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 
}

This patch requires you to create a file test/my-minitest-loader.rb:

ARGV.each { |f|
  break if f =~ /^-/
  load f
}

To print all possible options for Minitest, type

% ruby -r minitest/autorun -e '' -- --help
秉烛思 2024-10-28 08:04:35

我使用的是 Rails 版本 4.2.11.3 和 Ruby 版本 2.4.7p357

下面一个对我有用。

ruby -Itest <relative_minitest_file_path> --name /<test_name>/

I am in Rails Version 4.2.11.3 and Ruby Version 2.4.7p357

Below one worked for me.

ruby -Itest <relative_minitest_file_path> --name /<test_name>/
莫相离 2024-10-28 08:04:35

Rails 7.1 引入用于测试的特定行的新语法

现在您不仅可以运行特定测试按单行,

bin/rails test test/models/user_test.rb:10 # by line 10

也可按行范围

bin/rails test test/models/user_test.rb:10-20 # by lines 10..20

,甚至按几个范围

bin/rails test test/models/user_test.rb:10-20:25-30 # by lines 10..20 and 25..30

Rails 7.1 introduce new syntax for specific lines for tests

Now you can run not only specific tests by single line

bin/rails test test/models/user_test.rb:10 # by line 10

but also by lines range

bin/rails test test/models/user_test.rb:10-20 # by lines 10..20

or even by few ranges

bin/rails test test/models/user_test.rb:10-20:25-30 # by lines 10..20 and 25..30
唔猫 2024-10-28 08:04:35

我使用 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.

云朵有点甜 2024-10-28 08:04:35

如果您将 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.

剑心龙吟 2024-10-28 08:04:35

我正在寻找类似的功能:

rspec 路径/to/test_file.rb -l 25

有一个 gem 可以做到这一点:minitest-line

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

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

I'm looking for similar functionality to:

rspec path/to/test_file.rb -l 25

There is a gem that does exactly that: minitest-line.

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

from https://github.com/judofyr/minitest-line#minitest-line

折戟 2024-10-28 08:04:35

以下将起作用

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

Following will work

def test_abc
end

test "hello world"
end

This can run by

bundle exec ruby -I test path/to/test -n test_abc

bundle exec ruby -I test path/to/test -n test_hello_word
没企图 2024-10-28 08:04:35

安装 gem minitest-focus 并使用关键字 focus on test/spec ,如下所示仅运行具体测试。

focus
def test
end


focus
it "test" do
end

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

Install gem minitest-focus and use the keyword focus on test/spec like below to run only the specific test.

focus
def test
end


focus
it "test" do
end

This would not need any command line argument to be passed.

z祗昰~ 2024-10-28 08:04:35

如果您使用 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 the N environment variable:

rake test N=test_some_test_name

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文