是否可以捕获 ruby 中抛出的整个 class 中的异常

Is it possible to catch exceptions in ruby thrown over an entire class

我有测试 classes 继承了 class,继承了 Test::Unit::TestCase。我重写了断言,以便将输出输出到用于 bamboo 和 jenkins 的 junit 样式测试报告。这很好用。

现在我想捕获在整个测试过程中出现的异常 classes 从设置、多个测试用例到拆卸。除了在所有方法中放入 begin/resucue/end 块之外,还有更简单的方法吗?

有,但您需要挖掘(并理解其中的一些)Test::Unit 内部结构。 您想要实现的目标可能最容易通过 patching/replacing Test::Unit::TestCase#run 方法用您自己的方法完成。请注意,这可能会在版本之间中断,因此请务必进行修补 "smart".