如果测试在 scalatet 中的每个 FunSpec 中失败,如何确定 [英] how to dermine if a test failed in afterEach of a FunSpec in scalatest

查看:33
本文介绍了如果测试在 scalatet 中的每个 FunSpec 中失败,如何确定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用 FunSpec 的 scalatest 中,我有一些在 afterEach 中触发的代码.我想仅在测试失败时执行一些代码来获取屏幕截图.几乎所有我看过的东西都试图通过将断言放在看起来很糟糕的 try 块中来解决这个问题.TestNG 中是否有类似 onTestFailure 的东西,或者我可以像 RSpec 这样的上下文来确定测试是否失败?查看 scala 文档,我看到一个采用 configMap 的实现,但是当我运行测试时它是空的.任何帮助将不胜感激.

In scalatest using FunSpec I have some code that fires in the afterEach. I would like to execute some code to get a screenshot only when the test fails. Just about everything I have looked at tries to solve this by putting asserts in try blocks which seems awful. Is there anything like onTestFailure in TestNG or a context I can get like RSpec to determine if the test failed? Looking at the scala doc I see a implementation that takes a configMap, but that is empty when I run the test. Any help would be appreciated.

推荐答案

很确定我想通了.来自 TestNG 背景,不得不弄乱装置来完成它似乎很奇怪.我怀疑其他像我这样背景的人也可能会在所有错误的地方看,所以把这个留在这里帮助其他人:

pretty sure I figured it out. coming from a TestNG background it seemed weird to have to mess with fixtures to accomplish it. I suspect others with a background like mine may also look in all the wrong places as well, so going to leave this here to help others:

override def withFixture(test: NoArgTest) = { // after
   val outcome = super.withFixture(test)
   outcome match {
     case Failed(ex) =>
     // log ex (the exception) and a screenshot
   }
   outcome
}

这篇关于如果测试在 scalatet 中的每个 FunSpec 中失败,如何确定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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