how to dermine if a test failed in afterEach of a

2019-02-20 15:23发布

问题:

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.

回答1:

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
}


标签: scalatest