It is important to test your app. It is a key factor in helping you provide an excellent user experience with your app. So, what if you wanted to get started with writing tests in Android? This post will help you understand the basic to get you up and running.
Package Structure
In an Android project, there are two folders that hold your tests. They are the “test” and “androidTest” folder. They are kept distinct from each other for a good reason. Although, both are for testing they are used for different type of testing.
Type of Tests in Android
There are two types of test in Android, they are “Test” and “AndroidTest” (I know the directory names gave it away already). In the test directory is where pure java unit tests are placed and run. These tests run on the Java Virtual Machine (JVM) and don’t require an Android device or emulator. This type of tests has no access to any android framework specific components such as Context.
In the androidTest directory is where all Instrumentation (Espresso) tests are located. These tests do require a physical Android device or emulator to run. You may have guessed it already, but this type of tests does have access to components of the Android framework.
What Architecture Model to Use
When you are developing your app with testing in mind, your project architecture should accommodate it. There are multiple architecture patterns that easily supports testing and they are Model-View-Presenter (MVP) and Model-View-Viewmodel (MVVM).
Mocking
When you are writing tests, there will be times when you want to test something and it turns out to depend on some other object. In a situation like such, is where mocking comes in. You can create a mock of dependency object instead of creating an actual instance of that dependency. Usually, you will use a mock object for Unit Testing.
There are many options out there that you can use for mocking. However, if you are new to testing, Mockito is probably the one with a less steep learning curve.
UI Testing
For UI (User Interface) testing, Espresso is probably the testing framework to use. It is by Google and it provides APIs for UI testing in a single application.
UI testing is important because it ensures that user doesn’t have poor interaction or encounter unexpected behaviors. In addition, the UI for your application will determine whether the user would want to keep using your app.
I hope this post was helpful to you. If you found this post helpful, share it with others so they can benefit too.
What are your experiences with testing Android applications?
To get in touch, you can follow me on Twitter, leave a comment, or send me an email at steven@brightdevelopers.com.
Pingback: Testing and Android - Part 3: Unit Testing and UI Testing for Model View Controller Architecture - bright developers