Convenience class for extractors that match and return Throwables based on a type and Boolean condition.
Class Catcher was motivated by the need to catch
and handle exceptions based on more than just the exception's type as a strategy for dealing with
"flickering" tests—tests that usually pass, but occasionally fail. The best strategy for dealing with
flickers is to fix the test such that they stop flickering, but sometimes that is not practical. In
such cases allowing the test to continue flickering can distract the team by requiring them to
spend time inspecting failures to determine whether or not they are flickers or real failures that need
attention. Worse, with enough flickers, team members can stop checking all failures and not notice real ones.
One strategy for dealing with flickers you can't practically fix is to catch exceptions that are causing individual flickers
and cancel the test when you detect them. Often this means you will need to insert a catch clause in a particular spot, or a pattern
match if in a withFixture, looking for a particular exception with a particular message or other identifying attribute. If
the same problem is causing flickers in many places,
it is handy to create an extractor to detect the problem. This Catcher class provides
a factory method that takes a partial function from Throwable to Boolean and produces such an extractor.
Here's an example:
valInternalServerError =
Catcher { case e: DBAccessException =>
e.getMessage == "500:Internal Server Error"
}
Using this Catcher in a ScalaTest withFixture method would look like:
overridedef withFixture(test: NoArgTest) = {
super.withFixture(test) match {
caseFailed(InternalServerError(ex)) =>
Canceled("Canceled because likely a flicker caused by intermittently flaky DB", ex)
case other => other
}
}
Convenience class for extractors that match and return
Throwable
s based on a type andBoolean
condition.Class
Catcher
was motivated by the need to catch and handle exceptions based on more than just the exception's type as a strategy for dealing with "flickering" tests—tests that usually pass, but occasionally fail. The best strategy for dealing with flickers is to fix the test such that they stop flickering, but sometimes that is not practical. In such cases allowing the test to continue flickering can distract the team by requiring them to spend time inspecting failures to determine whether or not they are flickers or real failures that need attention. Worse, with enough flickers, team members can stop checking all failures and not notice real ones.One strategy for dealing with flickers you can't practically fix is to catch exceptions that are causing individual flickers and cancel the test when you detect them. Often this means you will need to insert a catch clause in a particular spot, or a pattern match if in a
withFixture
, looking for a particular exception with a particular message or other identifying attribute. If the same problem is causing flickers in many places, it is handy to create an extractor to detect the problem. ThisCatcher
class provides a factory method that takes a partial function fromThrowable
toBoolean
and produces such an extractor. Here's an example:Using this
Catcher
in a ScalaTestwithFixture
method would look like: