(gentle music)
- [Jose] In this screencast,
I'll show you how to create your first Compose tests.
The content of this video
applies to the first beta version of Compose.
So if you're using a later version,
make sure to check out our official documentation first.
The first thing to note about Compose tests
is that they are very similar to other UI tests
that you might run with Espresso, for example.
In fact, you can use Espresso in Compose tests
if you need to interact with a view.
Compose tests are, at least for now, instrumentation tests.
This means that they require an emulator
or a device to run.
Let's now take a look at this test.
There's something called a composeTestRule,
which we'll talk about later;
and finders, actions, and assertions,
like onNodeWithTag, assertIsDisplayed, onNodeWithText,
WithContentDescription, performClick, et cetera.
So just by reading the code,
you can have a clear understanding
of how this is going to work.
But let's start from the beginning.
I'm going to create a new project
with an empty Compose activity.
And by default, the new activity shows a simple text.
So let's change that and add some interaction.
We'll start with some internal mutable state,
a column that is going to have a button with some padding,
and this is going to increment the counter onClick.
Inside, we'll add a text with a string
that should come from string resources,
so let's do that now.
Also, we'll create another resource called Clicks
that takes an argument,
and we'll place it here, passing the counter.
Now I can build and use the preview
to check that everything looks good.
And it does, so let's do some cleanup first.
And for something so simple,
we can use the preview window in interactive mode
to check that it works by clicking the button.
Great.
Now let's start creating a test for this component.
Because Compose tests are instrumented tests,
I'll open the one in the Android test source set.
Let's rename it first and call it CounterTest
and remove everything else.
Before we continue,
we need to add the testing dependencies to our project.
You do that in the module's build.gradle file,
and you have the artifact name in the documentation,
so I'll copy it over and press the Sync button.
Okay, back to our test.
And the first thing you need in a Compose test
is the composeTestRule.
This is the way to launch components or activities,
and it provides the interface to use the finders,
actions, assertions, et cetera.
Now, there are different factory methods
depending on what you want to test:
A pure composeTestRule or an Android composeTestRule.
Depending on how much you want to interact
with the framework in your test,
you'll use one or the other.
The rule of thumb is that, if you can,
you should be using the composeTestRule.
In our case, we will be interacting
with the resources system
so it's easier to use an Android composeTestRule.
I'll call the first test, counter_initially_zero.
In most tests, you'll access everything through the rule.
To find an element, you use one of the onNode finders.
Let's cheat for now and hard-code the string.
Then, we add our assertion,
and we can run and see if it passes.
Great.
Now we can create the interesting one.
clickButton_incrementsCounter,
which will start by finding the button, clicking on it,
and finally asserting that the text has changed.
Now we can run it, and it passes.
Now, I know what you're thinking:
I'm hard-coding and duplicating strings all over the place,
so let's fix that.
We can access the launching activity
with the activity property in the composeTestRule,
so we just have to call getString on it.
We pass the string for the button
and the one for the text,
and we leave the number of clicks as zero
so that we can see the test failing first.
And now we fix it,
and now it passes.
This was just a very simple example.
The new testing APIs in Compose
offer very interesting features,
like testing in isolation or time manipulation,
and they come with a huge list
of finders, actions, and assertions.
Check out the official documentation
to learn everything about them.
(gentle music)