Creating a Test Class

The most convenient way to write unit tests is within a testing framework. The framework makes it easier to write and run a suite of tests together and see their output in Android Studio.

JUnit is almost universally used as a testing framework on Android and has convenient integrations into Android Studio. Your first job is to create a class for your JUnit tests to live in. To do this, open up SoundViewModel.kt and key in Command-Shift-T (Ctrl-Shift-T). Android Studio attempts to navigate to a test class associated with the class you are looking at. If there is no test class (as is the case here), you are given the option to create a new test class (Figure 20.1).

Figure 20.1  Trying to open a test class

Trying to open a test class

Choose Create New Test... to create the new test class. Select JUnit4 for your testing library and check the box marked setUp/@Before. Leave all the other fields as they are (Figure 20.2).

Figure 20.2  Creating a new test class

Creating a new test class

Click OK to continue to the next dialog.

The last step is to choose what kind of test class you will create. Tests in the androidTest folder are called instrumentation tests. Instrumentation tests run on an Android device or emulator. The advantage of this is that you can test your app against the same system frameworks and APIs that your APK will be running against once the app is released. The downside is that instrumentation tests take longer to set up and run, because they are running against a full version of the Android OS.

Tests in the test folder are often referred to as unit tests. These tests run on a Java Virtual Machine, or JVM, without any of the Android runtime. Leaving out that baggage makes them quick.

The term unit test on Android is overloaded. It is sometimes used to describe a type of test that verifies a single class or unit of functionality in isolation. At other times, it is used to describe any test residing in the test directory. Unfortunately, a test in that directory might verify a single class or unit – but it might be an integration test, which tests a section of an app with many pieces working together. You will learn more about integration tests in the section called For the More Curious: Integration Testing near the end of this chapter.

For the remainder of this chapter, we will use the term JVM test for a test of either type that lives in the test folder and runs on the JVM. We will reserve unit test for only those tests that verify a single class or unit.

Unit tests are the smallest kind of test you can write: a test of one component by itself. They should not need your entire app or a device to run, and they should run quickly enough for you to run them repeatedly as you work. Because of this, they are rarely run as instrumentation tests. With this in mind, choose the test folder for your test class (Figure 20.3) and click OK.

Figure 20.3  Selecting a destination directory

Selecting a destination directory
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.238.254.78