Ruby测试套件的创建

时间:2019-01-11 05:53:45

标签: ruby

在通过Ruby中的测试/单元创建/执行测试套件方面需要帮助。 我有两个在类中定义的测试用例,并且该类具有单个方法。该类继承自Test :: Unit :: TestCase。我想要实现的是运行测试套件时,应调用该类中的方法并打印结果,但是运行测试套件时,将打印以下内容

Test::Unit::TestSuite::STARTED
Test::Unit::TestSuite::STARTED::OBJECT
Test::Unit::TestSuite::STARTED
Test::Unit::TestSuite::STARTED::OBJECT
Test::Unit::TestSuite::FINISHED

Test::Unit::TestSuite::FINISHED::OBJECT
Test::Unit::TestSuite::STARTED
Test::Unit::TestSuite::STARTED::OBJECT
Test::Unit::TestSuite::FINISHED
Test::Unit::TestSuite::FINISHED::OBJECT
Test::Unit::TestSuite::FINISHED
Test::Unit::TestSuite::FINISHED::OBJECT

尝试搜索,但找不到任何解决方案。

require 'test/unit'
require 'test/unit/testcase'
require 'test/unit/ui/console/testrunner'
require 'test/unit/testsuite'
require 'test/unit/testresult'

class TC_HelloWorld < Test::Unit::TestCase
  def hello
    puts "Hello world!"
  end
end    
class TC_HelloWorld1 < Test::Unit::TestCase
  def hello
    puts "Hello1"
  end
end
class TS_HelloWorld
  def self.suite()
    suite = Test::Unit::TestSuite::new
    suite << TC_HelloWorld.suite
    suite << TC_HelloWorld1.suite

    return suite
  end
end

tr = Test::Unit::TestResult::new

TS_HelloWorld.suite.run(tr) do|result|
  puts(result)
end

1 个答案:

答案 0 :(得分:0)

您需要在测试方法前加上字符串test_。例如:

class TC_HelloWorld < Test::Unit::TestCase
  def test_hello                          # method name updated here
    puts "Hello world!"
  end
end 

这反映在documentation中:“向您的班级添加以“ test”开头的方法。”