1 00:00:00,031 --> 00:00:04,875 [MUSIC] 2 00:00:04,875 --> 00:00:07,930 Up until now we've been writing unit tests. 3 00:00:07,930 --> 00:00:10,980 But now, we're going to be writing UI tests, 4 00:00:10,980 --> 00:00:13,960 which can also be called instrumentation tests. 5 00:00:13,960 --> 00:00:18,315 The key difference is that a UI test actually does run on a device. 6 00:00:18,315 --> 00:00:22,574 So, instead of using something like Robolectric to help us pretend there's 7 00:00:22,574 --> 00:00:27,170 a device, we'll be running these tests directly on an actual device or emulator. 8 00:00:28,440 --> 00:00:30,940 The upside is that when we see an error, 9 00:00:30,940 --> 00:00:32,850 we'll know that it happened on an actual device. 10 00:00:33,890 --> 00:00:38,220 On the other hand, we now need a device where we didn't before. 11 00:00:38,220 --> 00:00:41,290 There are pros and cons to each of these methods of testing and 12 00:00:41,290 --> 00:00:45,740 it's up to you, the developer, to decide which is the best way to test your app. 13 00:00:46,930 --> 00:00:50,050 For UI testing, we'll be using Espresso. 14 00:00:50,050 --> 00:00:52,980 Espresso is a testing framework by Google included in 15 00:00:52,980 --> 00:00:56,440 the Android testing support library and it makes it easy for 16 00:00:56,440 --> 00:00:58,800 us to automatically test the functionality of our app. 17 00:01:00,060 --> 00:01:05,470 Let's get started with Espresso by creating a new test class, but instead of 18 00:01:05,470 --> 00:01:10,307 creating it in the test package, we need to create it in the androidTest package. 19 00:01:12,030 --> 00:01:16,930 If a test requires a device to run, then it belongs in the androidTest package, 20 00:01:16,930 --> 00:01:19,990 if it doesn't require a device, then we put it in the test package. 21 00:01:21,250 --> 00:01:28,060 Let's right click on the androidTest package and 22 00:01:28,060 --> 00:01:35,210 create a new Java class named MainActivityUITest. 23 00:01:35,210 --> 00:01:38,890 But before we get started with Espresso, we'll need to add it as a dependency. 24 00:01:39,930 --> 00:01:45,270 Over in our build.gradle file, let's add the necessary dependencies. 25 00:01:45,270 --> 00:01:47,260 You can copy them in from the teacher's notes below. 26 00:01:49,060 --> 00:01:53,220 We also need to add a line to the defaultConfig section. 27 00:01:53,220 --> 00:01:55,980 Let's copy and paste that in from the teacher's notes as well. 28 00:01:58,980 --> 00:02:00,274 Then let's sync the project. 29 00:02:04,844 --> 00:02:05,718 And we're good to go. 30 00:02:05,718 --> 00:02:10,140 Back in MainActivityUITest, 31 00:02:10,140 --> 00:02:15,700 let's tell Android Studio which runner to use by adding a run with annotation 32 00:02:15,700 --> 00:02:20,810 above our class, @RunWith, 33 00:02:20,810 --> 00:02:27,050 and then passing in AndroidJUnit4.class. 34 00:02:27,050 --> 00:02:30,830 Next we need to tell Espresso which activity we'll be testing. 35 00:02:31,860 --> 00:02:35,940 This is done by using a special rule from the Android support library, 36 00:02:35,940 --> 00:02:37,610 called activity test rule. 37 00:02:39,330 --> 00:02:43,840 Inside our class, let's create a new activity test rule field 38 00:02:43,840 --> 00:02:48,730 starting with the rule annotation, @Rule. 39 00:02:48,730 --> 00:02:55,047 Then on the next line, let's add public_ActivityTestRule. 40 00:02:58,308 --> 00:03:04,540 And inside the angle brackets, let's specify our activity, MainActivity. 41 00:03:05,660 --> 00:03:11,512 Then let's name our field activityTestRule and 42 00:03:11,512 --> 00:03:16,228 set it equal to new ActivityTestRule. 43 00:03:16,228 --> 00:03:20,660 Then we just need to pass in MainActivity.class, 44 00:03:20,660 --> 00:03:23,760 add a semicolon, and we're done. 45 00:03:25,080 --> 00:03:29,440 Also, if we hover our cursor over the gray MainActivity, 46 00:03:29,440 --> 00:03:30,868 it looks like it's unnecessary. 47 00:03:30,868 --> 00:03:33,242 Let's use Alt+Enter to get rid of it. 48 00:03:37,226 --> 00:03:41,789 Now that we've got our rules set up, we're ready to start testing with Espresso, but 49 00:03:41,789 --> 00:03:45,917 since this is our third round of testing, instead of writing out everything, 50 00:03:45,917 --> 00:03:50,106 let's skip ahead to the important bits by copying in the three test methods from 51 00:03:50,106 --> 00:03:51,695 the teacher's notes below. 52 00:03:56,100 --> 00:03:58,652 And let's use Alt+Enter to import the test class. 53 00:04:13,139 --> 00:04:17,125 In the next video, will kick off our Espresso testing by testing our edit text.