失败时执行专门的功能

失败时执行专门的功能

本文介绍了在 Scalatest 失败时执行专门的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 selenium 在 Scala Web 应用程序上执行集成测试.每当测试失败时,我想屏幕截图或将页面的 html 打印到控制台中.我目前的设置是使用 Selenium 2.0 和 Spec 的 Scalatest.

I am using selenium to perform integration tests on a Scala web app. I would like to either screenshot or print the html of a page into the console whenever a test fails. My current set up is Scalatest using Selenium 2.0, with Spec.

无论如何都可以从 AfterEach 覆盖方法中拦截失败或确定测试的状态吗?

Is there anyway to intercept a failure or determine the state of a test from a AfterEach override method?

推荐答案

为此,您需要覆盖 withFixture 而不是使用 BeforeAndAfterEach.BeforeAndAfterEach 的 beforeEach 方法发生在测试之前,而 afterEach 发生在测试之后.而 withFixture 可以在测试开始和/或结束时做一些事情.所以当 withFixture 被调用时,测试已经开始了.测试函数返回withFixture后,测试还没有完成.因此,您可以在 withFixture 中捕获异常以确定测试是否失败,如果失败,则创建屏幕截图.

To do that you'll want to override withFixture instead of using BeforeAndAfterEach. BeforeAndAfterEach's beforeEach method happens before the test, and afterEach happens after the test. Whereas withFixture can do something at the start and/or end of the test. So when withFixture is invoked, the test has already started. After the test function returns to withFixture, the test has not yet completed. So you can catch an exception in withFixture to determine if the test has failed, and if so, create a screen shot.

这篇关于在 Scalatest 失败时执行专门的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 00:01