Android Test Reports¶
Setup¶
Start by including the library:
Then create your own report rule that extends InstrumentedReportRule
:
Note
The goal of extending InstrumentedReportRule is to apply consistent report rules throughout your test suite. It contains settings regarding screen recording and screenshot that you can customize.
Now apply the rule in your tests:
Note
Use different orders for your rules in case you have multiple of them and assign the lowest value to TestReportRule
.
This ensures that it starts before all other rules you have in your test suite.
Example: @get:Rule(order = 0)
for the report rule and order = 1
for the next rule
This basic setup will achieve this out of the box:
- Automatic screen recordings for every test
- Automatic screenshot when the test fails
- Automatic dumps of the view hierarchy if the test fails
Visualize reports¶
The test reports are generated automatically after running any task like connectedDebugAndroidTest
and can be found in build/outputs/connected_android_test_additional_output/**/carioca-report
.
By default, those reports are in json format and are not really easily readable. To visualize them properly, this library ships with an Allure plugin that can be used to generate test reports based on the metadata collected through each test execution. Check it out in this page.
Test structure¶
Test body¶
You can decorate your tests with individual reports for every execution step:
Optionally, Given
, When
, Then
statements from BDD are also available to describe your tests:
Before and after¶
If you have re-usable logic in @Before
or @After
that you want to include in your reports,
just use the following APIs:
Scenario¶
The library includes an InstrumentedScenario
which allows you to re-use a set of steps across multiple tests:
Then, in your tests, can use it like so:
Extra metadata¶
Using @TestReport
allows you to describe your tests in more detail:
Recording options¶
To override the recording options for individual tests, use @TestRecording
:
Or also:
This configuration will replace the RecordingOptions
from InstrumentedReportRule
Screenshot options¶
To override the screenshot options for individual tests, use @TestScreenshot
:
This configuration will replace the ScreenshotOptions
from InstrumentedReportRule
Compose¶
com.rubensousa.carioca:report-android-compose
contains a DumpComposeHierarchyInterceptor
that can be used to inspect the compose hierarchy when tests fail.
To use it, create your rule and pass it in the list of interceptors:
The output of the hierarchy looks similar to this:
Node #1 at (left=0.0, top=0.0, right=1920.0, bottom=1080.0)px
|-Node #2 at (left=0.0, top=0.0, right=1920.0, bottom=1080.0)px
IsTraversalGroup = 'true'
|-Node #14 at (left=835.0, top=500.0, right=1085.0, bottom=580.0)px
| Focused = 'false'
| Role = 'Button'
| Actions = [OnClick, RequestFocus]
| MergeDescendants = 'true'
| |-Node #16 at (left=883.0, top=520.0, right=1037.0, bottom=560.0)px
| Text = '[Main button]'
| Actions = [SetTextSubstitution, ShowTextSubstitution, ClearTextSubstitution, GetTextLayoutResult]
|-Node #7 at (left=1728.0, top=936.0, right=1888.0, bottom=1048.0)px
Focused = 'false'
Role = 'Button'
Actions = [OnClick, RequestFocus]
MergeDescendants = 'true'
|-Node #10 at (left=1782.0, top=972.0, right=1834.0, bottom=1012.0)px
Text = '[FAB]'
Actions = [SetTextSubstitution, ShowTextSubstitution, ClearTextSubstitution, GetTextLayoutResult]
Tracking ignored tests¶
To include ignored tests in the reports, you need to attach the library's instrumentation listener:
android {
defaultConfig {
testInstrumentationRunnerArguments listener: 'com.rubensousa.carioca.report.android.CariocaInstrumentedListener'
}
}
Or in kotlin DSL:
android {
defaultConfig {
testInstrumentationRunnerArguments["listener"] =
"com.rubensousa.carioca.report.android.CariocaInstrumentedListener"
}
}
Note
If you use other listeners, just separate them with a comma in the same string