Carioca - Suite of Testing libraries for Android

This post celebrates the stable release of Carioca, a suite of testing libraries for Android that I’ve been working on since the last year.

This project is available here: https://github.com/rubensousa/Carioca

It contains 3 main component libraries:

  1. Instrumented test reports
  2. Hilt helper components
  3. Useful JUnit4 rules

In this post, I will briefly mention the set of libraries available. For more information about them, please check the website

Instrumented test reports

Analyzing the results of long-running UI tests can often feel like sifting through a haystack. The standard JUnit reports, while functional, can lack the context and visual aids needed to quickly pinpoint failures.

Remember that time you retried a pipeline just hoping the test was flaky and now this time will be green? Or remember that colleague that suggested ignoring some tests because they’re suddenly not passing, but everything seems fine? Say no more, we’ve all been there.

Carioca contains a thoughtful collection of APIs designed to enhance your testing workflow, with a strong emphasis on flexible and insightful reporting backed by Allure.

The instrumented test report library comes with these features:

  1. Take screenshots easily at any stage of a test
  2. Automatic video recordings of the test execution
  3. Generate an allure report for all tests
  4. Aggregate multi-module test results
  5. Automatic logcat attachment for every test failure

See this page for how to setup these reports.

After you integrate the library, an allure report is generated automatically with all relevant attached files:

This is done simply with a report rule and the allure reporting plugin:

This artifact contains the main library needed to start writing instrumented test reports:

androidTestImplementation("com.rubensousa.carioca:report-android:1.0.0")

And the Allure plugin is also available:

plugins {
    id 'com.rubensousa.carioca.report.allure' version '1.0.0'
}

Hilt helper components

If you’re using Hilt in your application for dependency injection, you might find these useful for writing compose or fragment tests that need Hilt dependencies:

debugImplementation "com.rubensousa.carioca:hilt-manifest:1.0.0"
androidTestImplementation "com.rubensousa.carioca:hilt-fragment:1.0.0"
androidTestImplementation "com.rubensousa.carioca:hilt-compose:1.0.0"
androidTestImplementation "com.rubensousa.carioca:hilt-runner:1.0.0"
  1. HiltFragmentScenario: similar to FragmentScenario but launches a Fragment inside an Activity that contains a Hilt entry point
  2. createHiltComposeRule(): similar to createAndroidComposeRule but launches a composable inside an Activity that contains a Hilt entry point
  3. HiltTestRunner: a custom runner that launches an Application setup with a Hilt graph

Check the docs here for complete examples.

JUnit4 rules

Some of these I kept copying over multiple projects, so I just decided to make them available in a standalone library:

  1. RetryTestRule: retries tests until they pass or a certain limit is exceeded. This isn’t generally needed, unless you have full end-to-end tests that can fail due to unpredictable reasons.
  2. MainDispatcherRule: replaces the main thread for unit tests, as seen here
  3. RepeatTestRule: similar to RetryTestRule but fails the test immediately if a failure is caught. I’ve been using this to detect flakiness in tests, as I can easily execute a test 100s of times

These rules are available through the following:

testImplementation "com.rubensousa.carioca:junit4-rules:1.0.0"
androidTestImplementation "com.rubensousa.carioca:junit4-rules:1.0.0"

Get started

Explore the documentation and examples available in the official docs