I've been trying to follow the instructions on getting Spoon 1.1.14 to take screenshots for failing Espresso tests.
What's the best way to configure this with a custom Espresso FailureHandler?
I've been trying to follow the instructions on getting Spoon 1.1.14 to take screenshots for failing Espresso tests.
What's the best way to configure this with a custom Espresso FailureHandler?
Replacing the default FailureHandler of Espresso with a custom one allows for additional error handling e.g. taking a screenshot:
Also, you'll need to throw the custom exception in the test setup and teardown:
You can use this in your Espresso test like:
Please look at the Android official CustomFailure example:
Click here for the official example
Click here for another example
Based on @Eric's approach above, and with ActivityTestRule we can obtain the current test method name and test class name from
description
object whenapply()
function is called. By overriding the apply function like thisI was able to take screenshot with correct test method and test class so it can be correctly integrated into final Spoon test report. And remember to use the JUnit4 runner by adding
to your test class.
Here's how I am doing this at the moment:
My base Espresso test class sets up the custom FailureHandler (I like using a base class to hold lots of other common code):
...and here is the slightly modified screenshot code from the Gist posted by Square:
I'd love to find a way to avoid calling
setUpFailureHandler()
in every test - please let me know if you have a good idea on how to avoid this!You could try setting this up in your subclass of
ActivityRule
. Something likeI'm not sure that
testClassName
andtestMethodName
will always be correct. The way I'm fetching those seems super-fragile, but I couldn't figure out a better way.