This course will be retired on July 14, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Testing in Android!
You have completed Testing in Android!
Preview
In this video we'll introduce UI (aka Integration) testing with Espresso - a UI testing Framework by Google!
Gradle Dependencies
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
androidTestCompile 'com.android.support:support-annotations:23.3.0'
Gradle 'default config' Part
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
Related Links
Test Methods for Copying
@Test
public void editTextUpdatesTextView() throws Exception {
// Arrange
String givenString = "test123";
// Act
// Assert
}
@Test
public void spinnerUpdatesBackgroundColor() throws Exception {
// Arrange
// Act
// Assert
}
@Test
public void buttonLaunchesOtherActivity() throws Exception {
// Arrange
// Act
// Assert
}
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
[MUSIC]
0:00
Up until now we've been
writing unit tests.
0:04
But now,
we're going to be writing UI tests,
0:07
which can also be called
instrumentation tests.
0:10
The key difference is that a UI
test actually does run on a device.
0:13
So, instead of using something like
Robolectric to help us pretend there's
0:18
a device, we'll be running these tests
directly on an actual device or emulator.
0:22
The upside is that when we see an error,
0:28
we'll know that it happened
on an actual device.
0:30
On the other hand, we now need
a device where we didn't before.
0:33
There are pros and cons to each
of these methods of testing and
0:38
it's up to you, the developer, to decide
which is the best way to test your app.
0:41
For UI testing, we'll be using Espresso.
0:46
Espresso is a testing framework
by Google included in
0:50
the Android testing support library and
it makes it easy for
0:52
us to automatically test
the functionality of our app.
0:56
Let's get started with Espresso by
creating a new test class, but instead of
1:00
creating it in the test package, we need
to create it in the androidTest package.
1:05
If a test requires a device to run, then
it belongs in the androidTest package,
1:12
if it doesn't require a device,
then we put it in the test package.
1:16
Let's right click on
the androidTest package and
1:21
create a new Java class
named MainActivityUITest.
1:28
But before we get started with Espresso,
we'll need to add it as a dependency.
1:35
Over in our build.gradle file,
let's add the necessary dependencies.
1:39
You can copy them in from
the teacher's notes below.
1:45
We also need to add a line to
the defaultConfig section.
1:49
Let's copy and paste that in from
the teacher's notes as well.
1:53
Then let's sync the project.
1:58
And we're good to go.
2:04
Back in MainActivityUITest,
2:05
let's tell Android Studio which runner
to use by adding a run with annotation
2:10
above our class, @RunWith,
2:15
and then passing in AndroidJUnit4.class.
2:20
Next we need to tell Espresso
which activity we'll be testing.
2:27
This is done by using a special rule
from the Android support library,
2:31
called activity test rule.
2:35
Inside our class, let's create
a new activity test rule field
2:39
starting with the rule annotation, @Rule.
2:43
Then on the next line,
let's add public_ActivityTestRule.
2:48
And inside the angle brackets,
let's specify our activity, MainActivity.
2:58
Then let's name our field
activityTestRule and
3:05
set it equal to new ActivityTestRule.
3:11
Then we just need to pass
in MainActivity.class,
3:16
add a semicolon, and we're done.
3:20
Also, if we hover our cursor
over the gray MainActivity,
3:25
it looks like it's unnecessary.
3:29
Let's use Alt+Enter to get rid of it.
3:30
Now that we've got our rules set up, we're
ready to start testing with Espresso, but
3:37
since this is our third round of testing,
instead of writing out everything,
3:41
let's skip ahead to the important bits by
copying in the three test methods from
3:45
the teacher's notes below.
3:50
And let's use Alt+Enter
to import the test class.
3:56
In the next video, will kick off our
Espresso testing by testing our edit text.
4:13
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up