Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

android
communitycreator

How to test Android apps

Gbohunmi Makinde

Android apps are built based on specifications, i.e., the way it should work and what it should be related to. In order to make sure the app works in accordance with these specifications, testing is used.

Usually, testing is done by manually clicking around the app, but this tends to waste time, and only allows you to test test it once. This means that, if you add new features, you will not know if it works.

With testing, all you have to do is re-run the test instead of going through the long, error-prone process of manually clicking around the app.

JUnit

What is JUnit testing?

JUnit is a unit testing framework whose emphasis is on creating a scenario for a piece of code that can be tested first and implemented later. JUnit allows for extensive testing at the tap of a button. If a test runs smoothly, JUnit will display a green bar. If a test fails, it will display a red bar.

Types of tests

  1. Unit test
  2. Integration test
  3. UI test

Unit test

The unit test is the biggest portion of the overall test. It basically tests the single components in our app, e.g., testing the functions of a class.

#include <iostream>
using namespace std;

int main() {
  // your code goes here
  fun sum(a,b){
    returns a+b
  }

  @Test
  fun sumReturnsAddition(){
    val result =sum(2,2)
    result = 4
    assertThat(result).isTrue
  }
}

Integration test

Integration tests are tests that show how different components of our app work together. These tests make up about 20% of the app test.

UI test

The UI test basically guides the user through the whole app and verifies that the app has a state, as expected. This test makes use of the emulator, so it is done under the Android test folder.

import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4

@RunWith(AndroidJUnit4::class)
@LargeTest
class ChangeTextBehaviorTest {

    private lateinit var stringToBetyped: String

    @get:Rule
    var activityRule: ActivityScenarioRule<MainActivity>
            = ActivityScenarioRule(MainActivity::class.java)

    @Before
    fun initValidString() {
        // Specify a valid string.
        stringToBetyped = "Espresso"
    }

    @Test
    fun changeText_sameActivity() {
        // Type text and then press the button.
        onView(withId(R.id.editTextUserInput))
                .perform(typeText(stringToBetyped), closeSoftKeyboard())
        onView(withId(R.id.changeTextBt)).perform(click())

        // Check that the text was changed.
        onView(withId(R.id.textToBeChanged))
                .check(matches(withText(stringToBetyped)))
    }
}

RELATED TAGS

android
communitycreator
RELATED COURSES

View all Courses

Keep Exploring