Kotlin Unit Test (KUT)

Local testing runs directly on your own workstation, not on an Android device or emulator. As such, it uses your local Java Virtual Machine (JVM) instead of the Android device to run the tests. Local testing allows you to evaluate your application logic more quickly. However, not being able to interact with the Android framework limits the types of tests you can run.

A unit test verifies the behavior of a small piece of code, the unit under test. It does this by executing that code and examining the results.

Unit testing is usually simple but setup can be problematic when the unit being tested is not designed with testability in mind:

  • The code you want to verify must be accessible from the test. For example, you cannot test private methods directly. Instead, you test the class using its public API.
  • To run unit tests in isolation, the dependencies of the unit under test must be replaced with components you control, such as fakes or other test doubles. This is especially problematic if your code relies on the Android framework.

To learn about general strategies for unit testing in Android, read What to test.

Local test locations

By default, the source files for local unit tests are placed in module-name/src/test/. This directory already exists when you create a new project using Android Studio.

Adding test dependencies

You also need to configure the test dependencies for your project to use the standard APIs provided by the JUnit testing framework.

To do this, open your app module's build.gradle file and specify the following libraries as dependencies. Use the testImplementation function to indicate that they apply to the local test source set, and not the app:

dependencies {
  // Required -- JUnit 4 framework
  testImplementation "junit:junit:$jUnitVersion"
  // Optional -- Robolectric environment
  testImplementation "androidx.test:core:$androidXTestVersion"
  // Optional -- Mockito framework
  testImplementation "org.mockito:mockito-core:$mockitoVersion"
  // Optional -- mockito-kotlin
  testImplementation "org.mockito.kotlin:mockito-kotlin:$mockitoKotlinVersion"
  // Optional -- Mockk framework
  testImplementation "io.mockk:mockk:$mockkVersion"
}

Create a local unit test class

You write your local unit test classes as JUnit 4 test classes.

To do this, create a class that contains one or more test methods, usually in the format module-name/src/test/. Test methods start with the @Testa annotation and contain code to exercise and verify one aspect of the component that you want to test.

The following example shows how to implement a local unit test class. The test method emailValidator_correctEmailSimple_returnsTrue() attempts to verify isValidEmail(), which is a method in the application. The test function will return true if isValidEmail() also returns true.

import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test

class EmailValidatorTest {
  @Test fun emailValidator_CorrectEmailSimple_ReturnsTrue() {
    assertTrue(EmailValidator.isValidEmail("name@email.com"))
  }

}

Source

https://developer.android.com/training/testing/local-tests#include-framework-dependencies


Post a Comment

Previous Next

نموذج الاتصال