CodeNewbie Community

Cover image for Testing in Android. How to run a test
Tristan
Tristan

Posted on

Testing in Android. How to run a test

Introduction

  • Now that I have officially released my first app, which can be found HERE on the Google Play store. I want to add more features to my app, however, I think the next best step is to add some tests. So, this series is going to be a practical series dedicated to understanding testing in the Android framework. All the information in this blog post is from a Google codelab which can be found HERE.

The Environment

  • In Android there are 3 folders that get automatically created for us. Those folders are called Source Sets and they contain the source code for our app. The source sets are:

1) main : contains our app's code. This code is a shared amongst all different versions of the app we build(build variants).

2) androidTest : contains tests know as instrumented tests(tests that run on physical device or emulator)

1) test : contains tests know as local tests(tests that run without a physical device or emulator)

deleteAfterupload1

Running a local test:

  • To run our first local there are two steps that we must take:

1) open the test folder until you find the ExampleUnitTest.java file.

2) Right-click the ExampleUnitTest class and select Run ExampleUnitTest.

  • Once you click the run option, a window will pop up at the bottom of the screen. To show all of the tests that have passed click on the Show passed check mark icon.

delete2

Making the test fail

  • If you look at the ExampleUnitTest class you will notice that each method has a @Test annotation. This is what marks it as a test in the Android System. Also, notice the test contains an assertion, asserEquals().An assertion is the core of your test. It's a code statement that checks that your code or app behaved as expected.

  • Android uses the testing library JUnit for testing. Both @Test and asserEquals() come from JUnit.

  • Now to make a test fail, create a new method inside the ExampleUnitTest class and mark the method with @Test. It will be very similar to the test addition_isCorrect but we will purposely make the assertion fail. Below is an example of the failing test:

@Test
    public void addition_isIncorrect() {
        assertEquals(3, 2 + 2);
    }
Enter fullscreen mode Exit fullscreen mode
  • If the you run the above code, you should get a window that looks similar to:

delete3

  • fix the assertion and run the code again. Boom, you and I now have a basic idea of how to run a test in the android framework.

Running a instrumented test

  • To run a instrumented test, you perform the exact some process but in the androidTest source set folder. However, when you run a instrumented test notice the emulator starting up. Remember that to run an instrumented test you need a physical device or an emulator.

  • Now that you can run tests, look forward to the next post where we will create our own tests.

Conclusion

  • Thank you for taking the time out of your day to read this blog post of mine. If you have any questions or concerns please comment below or reach out to me on Twitter

Discussion (0)