Skip to main content

Getting Started

The Litho Testing API is presented through the LithoViewRule 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).

LithoViewRule 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 you don't have to worry about it.

If you would like to read more about the TestRules, refer to the official JUnit documentation.

Adding Dependencies​

In order to use the Litho Testing API, you'll need to add the litho-testing dependency into your 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 your test class you need to initialise it:

@Rule @JvmField val lithoViewRule = LithoViewRule()

Then, within a test, you render your component with the help of the LithoViewRule.render(Component) method and perform assertions or other actions on the LithoView returned by the render call:

val testLithoView = lithoViewRule.render { TestComponent() }

What next?​

The remaining pages in this section describe how to use Assertions and how to interact with your Components.