如何使用 minitest 运行所有测试?

发布于 2024-10-13 10:35:18 字数 148 浏览 2 评论 0 原文

我下载了一个项目的源代码,发现了一个错误,并修复了它。

现在我想运行测试来看看我是否破坏了任何东西。

测试是在 minitest DSL 中进行的。

我如何同时运行它们?

我搜索了适用的 rake 任务等,但没有找到。

I downloaded source code for a project, found a bug, and fixed it.

Now I want to run tests to find out if I have broken anything.

The Tests are in minitest DSL.

How do I run them all at once?

I searched for applicable rake tasks etc, but I didn't find any.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(11

满天都是小星星 2024-10-20 10:35:18

这是 Rake::TestTask 的链接。

页面中有一个示例可以帮助您入门。
我将发布另一个我现在正在使用的 gem:

require 'rake/testtask'

Rake::TestTask.new do |t|
  t.pattern = "spec/*_spec.rb"
end

如您所见,我假设我的文件全部位于 /lib 中,并且我的规格位于 /spec 中 并命名为 whatever_spec.rb

Here's a link to Rake::TestTask.

There is an example in the page to get you started.
I'll post another one that I'm using right now for a gem:

require 'rake/testtask'

Rake::TestTask.new do |t|
  t.pattern = "spec/*_spec.rb"
end

As you can see, I assume that my files are all in /lib and that my specs are in /spec and are named whatever_spec.rb

禾厶谷欠 2024-10-20 10:35:18

locks 的答案更好,但我还想指出,您也可以直接从命令运行 minitest,就像使用 ruby​​ 命令一样。要运行spec/calculator_spec.rb文件中的测试,

$ ruby spec/calculator_spec.rb 

请记住在calculator_spec.rb文件中包含以下代码:

require 'minitest/spec'
require 'minitest/autorun'

要运行spec/目录中的所有测试,请使用以下命令(有关更多详细信息,请参阅这篇文章通配符不适用于 Minitest - 只有一个文件已运行)

$ for file in spec/*.rb; do ruby $file; done 

locks' answer is better, but I also wanted to point out that you can also run minitest directly from the command like with the ruby command. To run the tests in the spec/calculator_spec.rb file run:

$ ruby spec/calculator_spec.rb 

Remember to include the following code in the calculator_spec.rb file:

require 'minitest/spec'
require 'minitest/autorun'

To run all tests in the spec/ directory, use the following command (see this post for more details Globbing doesn't work with Minitest - Only one file is run)

$ for file in spec/*.rb; do ruby $file; done 
落花随流水 2024-10-20 10:35:18

这就是 Rake::TestTask< /a> 或多或少在幕后工作:

ruby -Ilib -e 'ARGV.each { |f| require f }' ./test/test*.rb

注意:lib & test/test*.rb (上面)是默认值,但 test & test/*_test.rb 分别是更典型的。

来源:rake/testtask.rb位于c34d9e0第169行

如果您使用 JRuby 并且希望避免支付两次启动成本(一次用于 Rake,另一次用于 Rake 启动的子进程),只需使用该命令即可。

This is what Rake::TestTask does under the hood, more or less:

ruby -Ilib -e 'ARGV.each { |f| require f }' ./test/test*.rb

Note: lib & test/test*.rb (above) are the defaults but test & test/*_test.rb, respectively, are more typical.

Source: rake/testtask.rb at c34d9e0 line 169

If you're using JRuby and want to avoid paying the startup cost twice (once for Rake and then once for the subprocess that Rake starts), just use that command.

雾里花 2024-10-20 10:35:18

这是我的整个 rakefile,我将其放在顶级目录中:

task :default => :test
task :test do
  Dir.glob('./test/*_test.rb').each { |file| require file}
end

要立即运行所有测试文件,我只需输入 rake。就是这样!

确保每个 Minitest 文件的顶部都有 require 'minitest/autorun'。 Dir.glob 绝对可以与 Minitest 一起使用。

为了获得漂亮的彩色 Minitest 输出以及所有测试方法的名称,我在 /test 目录中有文件 minitest_helper.rb 。 (必须安装 gem minitest-reporters):

require 'minitest/reporters'
Minitest::Reporters.use!(Minitest::Reporters::SpecReporter.new)
require 'minitest/autorun'

我只需在每个测试文件的顶部添加 require_relative './minitest_helper' 即可。

Here is my entire rakefile, which I put in my top directory:

task :default => :test
task :test do
  Dir.glob('./test/*_test.rb').each { |file| require file}
end

To run all my test files at once, I just type rake. That's it!

Make sure to have require 'minitest/autorun' at the top of each of your Minitest files. Dir.glob definitely DOES work with Minitest.

To get pretty, colored Minitest output, with names of all my test methods, I have the file minitest_helper.rb in my /test directory. (Had to install the gem minitest-reporters):

require 'minitest/reporters'
Minitest::Reporters.use!(Minitest::Reporters::SpecReporter.new)
require 'minitest/autorun'

I just had to require_relative './minitest_helper' at the top of each of my test files.

待"谢繁草 2024-10-20 10:35:18

这也可以通过 Makefile 来完成。

default:
  echo "Dir.glob('./test/*_test.rb').each { |file| require file}" | ruby

运行 make 将运行所有测试。

This can also be done via a Makefile.

default:
  echo "Dir.glob('./test/*_test.rb').each { |file| require file}" | ruby

running make will run all your tests.

只有影子陪我不离不弃 2024-10-20 10:35:18

$ rake test 超出了 Rails 5.0 的范围。

$ rake test ran out of the rails 5.0 box.

我的黑色迷你裙 2024-10-20 10:35:18

仅使用 Ruby 标准库执行此操作的另一种方法是使用 <代码>Dir.glob。在 ruby​​ 文件中,这看起来像这样:

require "minitest/autorun"

Dir.glob("**/*Test.rb") { |f| require_relative(f) }

或者从命令行,您可以使用此命令:

ruby -I . -e "require 'minitest/autorun'; Dir.glob('**/*Test.rb') { |f| require(f) }"

Dir.glob('**/*Test.rb') 递归地搜索当前目录任何与 *Test.rb 匹配的文件,因此我们只需获取所有这些文件并 requirerequire_relative 它们。从命令行, require_relative 失败,因此我们使用 require 但首先通过 -I .< 将当前目录添加到 $LOAD_PATH 中/代码>

Another way to do this using only Ruby's standard library is with Dir.glob. From within a ruby file, this would look like this:

require "minitest/autorun"

Dir.glob("**/*Test.rb") { |f| require_relative(f) }

Or from the commandline, you can use this command:

ruby -I . -e "require 'minitest/autorun'; Dir.glob('**/*Test.rb') { |f| require(f) }"

Dir.glob('**/*Test.rb') recursively searches the current directory for any file which matches *Test.rb, so we simply take all those files and require or require_relative them. From the commandline, require_relative fails, so we use require but first add the current directory to the $LOAD_PATH through -I .

心舞飞扬 2024-10-20 10:35:18

我意识到这是一个非常老的问题,但是 rake test 在 Rails 4.2 中对我有用,包括 test/ 以及 test/integration/、test/unit/ 等下的文件。

I realize this is a very old question, but rake test works for me in Rails 4.2, including files under test/ as well as test/integration/, test/unit/, etc.

等数载,海棠开 2024-10-20 10:35:18

在 Rails7 上,您有以下任务:

rake test
rake test:all

第一个任务跳过系统测试,第二个完成所有任务

On rails7 you have the following tasks:

rake test
rake test:all

First one skips system tests, second one does them all

用心笑 2024-10-20 10:35:18

2024 年 3 月更新:

minitest gem 有一个内置任务。将以下代码添加到您的 Rakefile.rb 文件中

require 'minitest/test_task'

Minitest::TestTask.create do |t|
  t.framework = %(require "test/test_helper.rb")
  t.libs = %w[test .]
  t.test_globs = ['test/**/*_test.rb']
end

并运行 rake test。调整 .create需要时默认

March 2024 update:

minitest gem has a built-in task for that. Add the following code to your Rakefile.rb file

require 'minitest/test_task'

Minitest::TestTask.create do |t|
  t.framework = %(require "test/test_helper.rb")
  t.libs = %w[test .]
  t.test_globs = ['test/**/*_test.rb']
end

and run rake test. Tune .create defaults when needed.

风吹过旳痕迹 2024-10-20 10:35:18

如果您没有 rake,请尝试以下操作:

http://blog.gingergriffis.com/post/85871430778/ruby-how-to-run-all-tests-in-a-directory

# run_all_tests.rb

require 'find'
require 'optparse'

options = {
  :exclude => [],
}

OptionParser.new do |opts|
  opts.on('--exclude Comma Separated String',
          'Test scripts to exclude') do |css|
    options[:exclude] = css.split(',')
  end
end.parse!

commands = []

Find.find(File.dirname(__FILE__)) do |path|
  Find.prune if path =~ /#{__FILE__}$/
    if !File.directory?(path) && (path =~ /(.*)\.rb$/)
      if options[:exclude].none? {|e| path.include?(e)}
        commands << "ruby #{path}"
      end
    end
end
command_string = commands.join(" && ")
exec(command_string)

If you don't have rake, try this:

http://blog.gingergriffis.com/post/85871430778/ruby-how-to-run-all-tests-in-a-directory

# run_all_tests.rb

require 'find'
require 'optparse'

options = {
  :exclude => [],
}

OptionParser.new do |opts|
  opts.on('--exclude Comma Separated String',
          'Test scripts to exclude') do |css|
    options[:exclude] = css.split(',')
  end
end.parse!

commands = []

Find.find(File.dirname(__FILE__)) do |path|
  Find.prune if path =~ /#{__FILE__}$/
    if !File.directory?(path) && (path =~ /(.*)\.rb$/)
      if options[:exclude].none? {|e| path.include?(e)}
        commands << "ruby #{path}"
      end
    end
end
command_string = commands.join(" && ")
exec(command_string)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文