Getting Started
The Litho Testing API lets you write unit tests for Litho components. It is presented through the LithoTestRule class, which enables the following:
- Make assertions against the Component hierarchy.
- Mark assertions against the View hierarchy.
- Provides access to utility functions to interact with the View hierarchy (such as by clicking).
LithoTestRule uses the Junit 'TestRule', which provides a flexible mechanism to execute code before and after a test method. As a result, Litho is able to prepare the environment then clean it up after testing has taken place, so there's no need to be concerned about it.
For more information about the TestRules, refer to the official JUnit documentation.
Adding Dependencies
In order to use the Litho Testing API, add the litho-testing dependency to your module's build.gradle:
dependencies {testImplementation 'com.facebook.litho:litho-testing:0.50.2'}
Basics
In order to use the @Rule inside of a test class, it needs to be initialised:
@Rule @JvmField val lithoTestRule = LithoTestRule()
Then, within a test, render the component with the help of the LithoTestRule.render(Component) method and perform assertions or other actions on the LithoView returned by the render call:
val testLithoView = lithoTestRule.render { TestComponent() }
What next?
The remaining pages in this section describe how to use assertions and how to interact with components.