{"id":561,"date":"2019-11-02T12:13:58","date_gmt":"2019-11-02T02:13:58","guid":{"rendered":"https:\/\/learntodroid.com\/?p=561"},"modified":"2020-03-26T18:59:08","modified_gmt":"2020-03-26T08:59:08","slug":"how-to-test-my-android-app-before-publishing","status":"publish","type":"post","link":"http:\/\/10.0.0.14:32769\/how-to-test-my-android-app-before-publishing\/","title":{"rendered":"How to test my Android app before publishing?"},"content":{"rendered":"\n

The Google Play Store is a competitive place today for Android apps. When you are ready to release your app you want to make a great first impression and to get positive reviews from your users. Any bugs experienced by your users with your app will likely result in bad reviews and uninstalls which will have a negative impact on your app’s ranking in the Play Store.<\/p>\n\n\n\n

In this article I will share with you how you can build quality into your apps by using various Android app testing frameworks to minimise the risk of users finding bugs in your app.<\/p>\n\n\n\n

You can test your Android app before publishing to the Google Play Store using the following methods:<\/strong><\/p>\n\n\n\n

  1. Local Unit Tests using JUnit<\/strong><\/li>
  2. Instrumented Unit Tests using JUnit<\/strong><\/li>
  3. User Interface Tests using Espresso<\/strong><\/li>
  4. Distributing your app for Open, Closed or Internal Testing on the Google Play Store<\/strong><\/li><\/ol>\n\n\n\n

    At minimum you would want to use at least local unit tests, instrumented unit tests and user interface tests. I will explain in more detail below what each of these testing methods are, why they are important and how to use them to improve the user experience of your app.<\/p>\n\n\n\n

    Unit Testing in Android<\/h2>\n\n\n\n

    Setting up for Unit Testing with JUnit 4<\/h3>\n\n\n\n

    Before you write your tests you will need to make sure your app’s build.grade file contains the following dependencies in order to use JUnit 4 as well as some optional dependencies for Robolectric and Mockito.<\/p>\n\n\n\n

    dependencies {\n    \/\/ Required -- JUnit 4 framework\n    testImplementation 'junit:junit:4.12'\n    \/\/ Optional -- Robolectric environment\n    testImplementation 'androidx.test:core:1.0.0'\n    \/\/ Optional -- Mockito framework\n    testImplementation 'org.mockito:mockito-core:1.10.19'\n}<\/pre>\n\n\n\n

    When you create a new project in Android Studio, in the project view you will see androidTest, test and main directories inside the app\/src directory.<\/p>\n\n\n\n

    • The app\/src\/main directory contains your application code<\/li>
    • The app\/src\/test directory contains your local unit tests<\/li>
    • The app\/src\/androidTest directory contains your instrumented and end to end tests<\/li><\/ul>\n\n\n\n
      \"\"
      Android Studio Project View androidTest, test and main directories<\/figcaption><\/figure>\n\n\n\n

      To run your instrumented tests, right click the package inside the androidTest directory, then select “Run Tests in <package name>”. These tests will then be executed inside the emulator.<\/p>\n\n\n\n

      \"\"
      Running Instrumented Tests in Android Studio<\/figcaption><\/figure>\n\n\n\n

      The test results for the instrumented tests will be visible within a tab in the Run results.<\/p>\n\n\n\n

      \"\"<\/figure>\n\n\n\n

      To run your local tests, right click the package inside the test directory, then select “Run Tests in <package name>”. These tests will then be executed within the IDE without requiring an emulator or real device.<\/p>\n\n\n\n

      \"\"
      Running Local Unit Tests in Android Studio with Test Results<\/figcaption><\/figure>\n\n\n\n

      Local Unit Tests using JUnit 4<\/h3>\n\n\n\n

      Local unit tests are run on your local machine within the Java Virtual Machine. They are run inside the Android Studio IDE and they do not require an emulator, virtual or real device to run. These tests run very fast and they are limited to testing logic within your code. It will not be able to test Android specific functionality such as Activities, Fragments, etc.<\/p>\n\n\n\n

      JUnit 4 and Mockito are testing frameworks that can be used to write local unit tests. For the scope of this tutorial we will be using JUnit 4, before you write these local unit tests make sure JUnit 4 is added as a dependency in your app’s build.gradle file as covered in the Setting up for Unit Testing with JUnit 4<\/em><\/strong> section.<\/p>\n\n\n\n

      To generate a test class where you will write your local unit tests in Android Studio:<\/p>\n\n\n\n

      1. Highlight the method name within the class you want to make a Local Unit tests for<\/li>
      2. Right click the method name<\/li>
      3. Select “Generate”<\/li>
      4. Select “Test…”<\/li>
      5. (Optional) Update the test class name<\/li>
      6. Check the set of methods you want to create tests for in the new test class<\/li>
      7. Select the “OK” button<\/li>
      8. Select the test directory<\/li>
      9. Select “OK”<\/li><\/ol>\n\n\n\n
        \"\"
        Generating Local Unit Tests in Android Studio<\/figcaption><\/figure>\n\n\n\n

        The test class generated will a test method for each of the methods you selected in the “Create Test” dialog.<\/p>\n\n\n\n

        \"\"
        Generated Local Unit Tests in Test Class<\/figcaption><\/figure>\n\n\n\n

        The code snippet below from the CalculatorUtilTest class<\/a>, available in the InterestCalculator GitHub repo<\/a>, shows how to use to test the expected value of a method against the actual value calculated through the use of an assert statement with a small margin of error using a delta value.<\/p>\n\n\n