Skip to main content

Getting Started

The Litho Testing API is presented through the LithoTestRule class, it 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, the litho-testing dependency needs to be added into the relevant BUCK file:

BUCK
deps = [
"//fbandroid/libraries/components/litho-testing/src/main/java/com/facebook/litho/testing:testing"
],

Basics​

In order to use the @Rule inside of a test class, it needs to be initialised:

@Rule @JvmField val mLithoTestRule = 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 = mLithoTestRule.render { TestComponent() }

What next?​

The remaining pages in this section describe how to use assertions and how to interact with components.