Minitest-具有方法级别粒度的测试套件 [英] Minitest - A test suite with method-level granularity

查看:101
本文介绍了Minitest-具有方法级别粒度的测试套件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

升级后,我发现相同的几种测试方法都失败了,所以我想自动测试那些方法,而不是自动测试所有类中的所有方法.我想列出每个类方法对(例如TestBlogPosts.test_publishTestUsers.test_signup),并将它们作为测试套件一起运行.无论是在文件中还是在命令行中,我都不在乎.

After an upgrade, I'm finding the same several test methods failing, so I'd like to automate testing just those instead of all methods in all classes. I want to list each class-method pair (e.g. TestBlogPosts.test_publish, TestUsers.test_signup) and have them run together as a test suite. Either in a file or on the command-line, I don't really care.

我知道这些用于运行多个完整类的技术,但我在这里寻找更细的粒度. (与-n/pattern/在命令行上执行的操作类似-运行测试方法的子集-但跨多个类.)

I'm aware of these techniques to run several entire classes, but I'm looking for finer granularity here. (Similar to what -n /pattern/ does on the command-line - to run a subset of test methods - but across multiple classes.)

推荐答案

您可以放弃minitest/autorun并使用自己定义的测试选择呼叫Minitest.run.

You could renounce minitest/autorun and call Minitest.run with your self defined test selection.

一个例子:

gem 'minitest'
require 'minitest'
#~ require 'minitest/autorun' ##No!

#Define Test cases. 
#The `puts`-statements are kind of logging which tests are executed.
class MyTest1 < MiniTest::Test
  def test_add
    puts "call %s.%s" % [self.class, __method__]
    assert_equal(2, 1+1)
  end
  def test_subtract
    puts "call %s.%s" % [self.class, __method__]
    assert_equal(0, 1-1)
  end
end
class MyTest2 < MiniTest::Test
  def test_add
    puts "call %s.%s" % [self.class, __method__]
    assert_equal(2, 1+1)
  end
  def test_subtract
    puts "call %s.%s" % [self.class, __method__]
    assert_equal(1, 1-1)  #will fail
  end
end

#Run two suites with defined test methods.
Minitest.run(%w{-n /MyTest1.test_subtract|MyTest2.test_add/})  #select two specific test method

结果:

Run options: -n "/MyTest1.test_subtract|MyTest2.test_add/" --seed 57971

# Running:

call MyTest2.test_add
.call MyTest1.test_subtract
.

Finished in 0.002313s, 864.6753 runs/s, 864.6753 assertions/s.

2 runs, 2 assertions, 0 failures, 0 errors, 0 skips

调用以下测试时:

Minitest.run(%w{-n /MyTest1.test_subtract/})  #select onespecific test method
puts '=================='
Minitest.run(%w{-n /MyTest2.test_add/})  #select one specific test method

那你就得到

Run options: -n /MyTest1.test_subtract/ --seed 18834

# Running:

call MyTest1.test_subtract
.

Finished in 0.001959s, 510.4812 runs/s, 510.4812 assertions/s.

1 runs, 1 assertions, 0 failures, 0 errors, 0 skips
==================
Run options: -n /MyTest2.test_add/ --seed 52720

# Running:

call MyTest2.test_add
.

Finished in 0.000886s, 1128.0825 runs/s, 1128.0825 assertions/s.

1 runs, 1 assertions, 0 failures, 0 errors, 0 skips

Minitest.run的参数与您在命令行中使用的参数相同.因此,您可以在选择中使用-n选项,例如/MyTest1.test_subtract|MyTest2.test_add/.

The Minitest.run takes the same parameters you use from the command line. So you can use the -n option with your selection, e.g. /MyTest1.test_subtract|MyTest2.test_add/.

您可以定义具有不同Minitest.run定义的不同任务或方法来定义测试套件.

You could define different tasks or methods with different Minitest.run-definition to define your test suites.

注意: 您加载的任何测试文件都不能包含require 'minitest/autorun'.

Attention: No test file you load may contain a require 'minitest/autorun'.

这篇关于Minitest-具有方法级别粒度的测试套件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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