【问题标题】:Ruby Test:Unit, how to know fail/pass status for each test case in a test suite?Ruby Test:Unit,如何知道测试套件中每个测试用例的失败/通过状态?
【发布时间】:2012-02-28 06:36:30
【问题描述】:

这个问题听起来很愚蠢,但我从来没有在网上找到答案。 假设你有一个像这个页面这样的测试套件: http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing 或代码:

require "simpleNumber"
require "test/unit"

class TestSimpleNumber < Test::Unit::TestCase

  def test_simple
    assert_equal(4, SimpleNumber.new(2).add(2) )
    assert_equal(4, SimpleNumber.new(2).multiply(2) )
  end

  def test_typecheck
    assert_raise( RuntimeError ) { SimpleNumber.new('a') }
  end

  def test_failure
    assert_equal(3, SimpleNumber.new(2).add(2), "Adding doesn't work" )
  end

end

运行代码:

>> ruby tc_simpleNumber2.rb
Loaded suite tc_simpleNumber2
Started
F..
Finished in 0.038617 seconds.

  1) Failure:
test_failure(TestSimpleNumber) [tc_simpleNumber2.rb:16]:
Adding doesn't work.
<3> expected but was
<4>.

3 tests, 4 assertions, 1 failures, 0 errors

现在,如何使用变量(什么类型?)来保存测试结果? 例如,像这样的数组:

[{:name => 'test_simple', :status => :pass}, 
    {:name => 'test_typecheck', :status => :pass},
    {:name => 'test_failure', :status => :fail},]

我是测试新手,但很想知道答案...

【问题讨论】:

  • 你想做什么?据我记得,您可以只从命令行运行文件,在这种情况下 test/unit 将报告单个失败的测试。
  • 将结果保存在 Excel 文件中。我的目标是将每个测试用例的通过/失败状态保存到 Excel 文件中。像Started F.. Finished in 0.016001 seconds.3 tests, 3 assertions, 1 failures, 0 errors 这样的结果无法说出细节;它确实告诉行号失败,但格式仍然不好
  • 我有测试套件来包含许多测试用例,单独运行它们是一种方法,但这仍然需要编写一个简单的函数来收集屏幕结果......似乎绕道......

标签: ruby unit-testing testing testunit


【解决方案1】:

你需要执行你的测试脚本文件,就是这样,结果会显示通过或失败。

假设你保存文件 test_unit_to_rspec.rb,然后执行下面的命令

ruby test_unit_to_rspec.rb

【讨论】:

  • 是的,像3 tests, 3 assertions, 1 failures, 0 errors 这样的结果确实报告了结果,但还不够详细...除非我单独拆分每个 test_case,单独运行它们,使用简单的函数来收集这些输出结果...似乎工作量太大,而不是一种优雅/Ruby的做事方式...
  • 您使用的是哪个操作系统?如果你使用 Mac 或 Unix/Linux,我会回复你 ruby​​ 如何使用优雅的方式来显示。
  • 我在 Windows 上使用它,但稍后会在 linux 上部署它。因此,请以优雅的方式前进。欣赏它,阿米特。
  • 这里是你知道 ruby​​ 工作原理的链接ruby.railstutorial.org/chapters/static-pages#sec:first_tests 那些你也可以在 Ruby 中使用的工具(无需使用 rails)
  • 谢谢,阿米特。我以前读过那本书,但对测试部分没有深入。我的项目不是基于 Rails。我只是在测试我当前公司的 Web UI,它有许多 Ajax 驱动的页面。到目前为止,我正在使用 watir 和 test-unit 编写独立的测试程序来完成这项工作......有什么建议吗?
【解决方案2】:

解决了在测试运行程序调用中设置高详细级别的问题。

http://ruby-doc.org/stdlib-1.8.7/libdoc/test/unit/rdoc/Test/Unit/UI/Console/TestRunner.html

require 'test/unit'
require 'test/unit/ui/console/testrunner'

class MySuperSuite < Test::Unit::TestSuite
    def self.suite
        suites = self.new("My Super Test Suite")
        suites << TestSimpleNumber1
        suites << TestSimpleNumber2
        return suites
    end
end

#run the suite
# Pass an io object
#new(suite, output_level=NORMAL, io=STDOUT)
runner = Test::Unit::UI::Console::TestRunner.new(MySuperSuite, 3, io)

结果将以适合每个测试用例的格式保存在 io 流中。

【讨论】:

    【解决方案3】:

    您可以查看另一个Nat's posts 以获取捕获结果的方法。对您的问题的简短回答是没有用于捕获结果的变量。你得到的只是:

    加载套件我的特殊测试

    开始
    ..

    在 1.000935 秒内完成。

    2 次测试,2 次断言,0 次失败,0 次错误

    如果您想向其他人报告发生的事情,这不是很有帮助。 Nat 的另一篇文章展示了如何将 Test::Unit 包装在 rspec 中以获得更好的结果和更大的灵活性。

    【讨论】:

    • 感谢您的建议,确实有点帮助,我需要更多的研究...谢谢!
    【解决方案4】:
    class Test::Unit::TestCase
      def setup
        @id = self.class.to_s()
      end
    
    
      def teardown
        @test_result = "pass"
    
        if(@_result.failure_count > 0 || @_result.error_count > 0)
          @test_result = "fail"
          # making sure no errors/failures exist before the next test case runs.
          i = 0
          while(i < @_result.failures.length) do
            @_result.failures.delete_at(i)
            i = i + 1
          end
          while(i < @_result.errors.length) do
            @_result.errors.delete_at(i)
            i = i + 1
          end
          @test_result = "fail"
        end # if block ended
        puts"#{@id}: #{@test_result}"
      end # teardown definition ended
    end # class Test::Unit::TestCase ended
    

    示例输出:

    test1: Pass
    test2: fail
    so on....
    

    【讨论】:

      猜你喜欢
      • 2022-08-14
      • 2019-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多