Ruby 单元测试:在每次测试失败后运行一些代码 [英] Ruby unit tests: run some code after each failed test

查看:31
本文介绍了Ruby 单元测试:在每次测试失败后运行一些代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Test::Unit 中的 ruby​​ 单元测试中断言失败后,在 teardown 被执行之前,是否有一些干净优雅的方式来执行我的代码?>

我正在做一些自动化的 GUI 测试,想在出现问题后立即截取屏幕截图.

解决方案

如果您使用的是 1.9,请不要使用 Test::Unit::TestCase 作为您的基类.将其子类化并覆盖 #run_test 以进行救援,截取屏幕截图并重新提出:

class MyAbstractTestCase <测试::单元::测试用例def run_test( *args )超级(*参数)救援快照()增加结尾结尾

或者,我认为这实际上是最简洁的方式,您可以使用 before_teardown 钩子:

class MyTestCase <测试::单元::测试用例add_teardown_hook do |tc|屏幕截图()除非 tc.passed?结尾结尾

这不适用于 1.8 的测试/单元,但适用于 1.9 中的 minitest.

Is there some clean and elegant way to execute my code right after a failed assert in ruby unit tests in Test::Unit, before teardown gets executed?

I am doing some automated GUI testing and would like to take a screenshot right after something failed.

解决方案

If you're on 1.9, don't use Test::Unit::TestCase as your base class. Subclass it and override #run_test to rescue, take the screenshot and reraise:

class MyAbstractTestCase < Test::Unit::TestCase
  def run_test( *args )
    super(*args)
  rescue
    snapshot()
    raise
  end
end

Alternatively, and I think this is actually the most terse way in, you can use a before_teardown hook:

class MyTestCase < Test::Unit::TestCase
  add_teardown_hook do |tc|
    screenshot() unless tc.passed?   
  end
end

This won't work on 1.8's test/unit, but will with the minitest in 1.9.

这篇关于Ruby 单元测试:在每次测试失败后运行一些代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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