使用Ruby minitest创建测试套件 [英] Creating a test suite using Ruby minitest

查看:127
本文介绍了使用Ruby minitest创建测试套件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将测试移至新的ruby minitest库,并且正在寻找与旧的Test :: Unit :: TestSuite类相对应的类.我在网上找到的所有示例都显示单个测试用例,但我知道:

I'm moving my tests to the new ruby minitest library, and I am looking for the class that corresponds to the old Test::Unit::TestSuite class. All the examples I've found online show single test cases, but I've got:

require 'minitest/unit/testsuite'
require 'minitest/unit/ui/console/testrunner'

require 'tests/fs_session_test'
require 'tests/resource_test'
require 'tests/rest_session_test'
require 'tests/server_test'

class AllTests
  def self.suite
    suite = Test::Unit::TestSuite.new
    suite << FSSessionTest.suite
    suite << ResourceTest.suite
    suite << RESTSessionTest.suite
    suite << ServerTest.suite
  end
end

Test::Unit::UI::Console::TestRunner.run(AllTests)

并且我不断收到测试套件要求中的LoadError.

and I keep getting a LoadError on the testsuite require.

推荐答案

minitest中没有Test::Unit::TestSuite.假设您的测试看起来像这样,您有几种选择:

There is no Test::Unit::TestSuite in minitest. You have several options, assuming your tests look something like this:

require 'minitest/unit'
require 'minitest/autorun'

class FSSessionTest < MiniTest::Unit::TestCase
  def test_the_truth
    assert true
  end
end

这里最重要的部分是require 'minitest/autorun',它在封闭脚本退出之前使用at_exit运行它可以找到的所有测试.我发现这是运行测试套件的最简单方法.

The vital part here is require 'minitest/autorun' which uses at_exit to run all tests it can find, just before the enclosing script exits. I find this to be the easiest way for running my test suites.

例如,您可以使用 Rake::TestTask 创建一个Rakefile,它可以运行所有测试在您的test/目录中:

For example, you can create a Rakefile using Rake::TestTask which runs all the tests in your test/ directory:

require 'rake'
require 'rake/testtask'

Rake::TestTask.new do |t|
  t.pattern = 'tests/**/*_test.rb'
end

运行测试

$ rake test

需要在Ruby文件中进行测试

如果您经常只需要某些测试,则还可以编写测试脚本,例如

Require tests in a Ruby file

If you frequently only need certain tests, you can also write a test script, something like

require './tests/fs_session_test'
require './tests/resource_test'
require './tests/rest_session_test'
require './tests/server_test'

您也可以在该文件的顶部包括require 'minitest/autorun'以确保测试已运行,但是无论如何,我都会在每个测试文件的顶部进行此操作.使用

You could also include require 'minitest/autorun' at the top of this file to ensure, the tests are run, but i do this at the top of every test file, anyway. Run the suite with

$ ruby test.rb

结果

这两种方法都可以提供相同的输出,例如类似

Result

Both methods give you the same output, for example something like

Run options: --seed 5559

# Running tests:

....

Finished tests in 0.001909s, 2095.3379 tests/s, 2095.3379 assertions/s.

4 tests, 4 assertions, 0 failures, 0 errors, 0 skips

由于miniest充分利用了at_exit,因此实际上不需要在运行测试之前对其进行分组.您永远不会只有一个测试的输出.除非,当然,除非您自己进行测试,例如使用

Because mintiest makes use of at_exit, there is really no need to group the tests before you run them. You never get the output of only one test. Unless, of course you run a test on its own, for example with

$ ruby tests/fs_session_test.rb 
Run options: --seed 43007

# Running tests:

.

Finished tests in 0.000672s, 1488.0952 tests/s, 1488.0952 assertions/s.

1 tests, 1 assertions, 0 failures, 0 errors, 0 skips

这篇关于使用Ruby minitest创建测试套件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆