Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Modern testing with JUnit for Android.

License

NotificationsYou must be signed in to change notification settings

mannodermaus/android-junit-framework

Repository files navigation

Android JUnit

CircleCI

A Gradle plugin that allows for the execution ofJUnit 5+ tests in Android environments usingAndroid Gradle Plugin 8.2 or later.

How?

This plugin configures the unit test tasks for each build variant of a project to run on the JUnit Platform.Furthermore, it provides additional configuration options for these teststhrough a DSLand facilitates the usage JUnit for instrumentation tests.

Instructions on how to write tests with modern JUnit can be foundin their User Guide.To get a first look at its features, a small showcase project can be foundhere.

Setup

To get started, declare the plugin in yourapp module's build script alongside the latest version.Snapshots of the development version are available throughSonatype'ssnapshots repository.

plugins {// 1. Apply the plugin    id("de.mannodermaus.android-junit") version"2.0.1"}dependencies {// 2. Add JUnit BOM and the required dependencies    testImplementation(platform("org.junit:junit-bom:5.14.1"))    testImplementation("org.junit.jupiter:junit-jupiter-api")    testImplementation("org.junit.jupiter:junit-jupiter-params")    testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")// 3. Add JUnit Vintage if you also have JUnit 4 tests (e.g. for Robolectric)    testImplementation("junit:junit:4.13.2")    testRuntimeOnly("org.junit.vintage:junit-vintage-engine")}

More information on Getting Started can be foundon the wiki.

Requirements

The latest version of this plugin requires at least:

  • Android Gradle Plugin8.2
  • Gradle8.2

Instrumentation Test Support

You can also write instrumentation tests with new JUnit APIs and execute them on emulators and physical devices.Depending on the Java requirements of the JUnit version, these instrumentation tests will only run on devicesthat meet these requirements, however. On older devices, an exception will be raised at runtime.

  • JUnit 5 requires Java 8; devices require at least Android 8.0 (API 26)
  • JUnit 6 requires Java 17; devices require at least Android 15 (API 35)

Before you can write instrumentation tests with JUnit Jupiter,make sure that your module is using theandroidx.test.runner.AndroidJUnitRunner(or a subclass of it) as itstestInstrumentationRunner. Then, simply add a dependency on JUnit Jupiter APIto theandroidTestImplementation configuration in your build script and the plugin willautomatically configure JUnit 5 tests for you:

dependencies {    androidTestImplementation(platform("org.junit:junit-bom:5.14.1"))    androidTestImplementation("org.junit.jupiter:junit-jupiter-api")}

By enabling JUnit for instrumentation tests, you will gain access toActivityScenarioExtension among other things,which helps with the orchestration ofActivity classes. Checkthe wiki for more info.

Ignoring older devices

Historically, tests were ignored if the Android device running them did not meet the requirements of JUnit.This was changed in the 2.0.0 of the plugin & library, but if you want to restore the previous behavior,you can do so via thejunitPlatform DSL.

junitPlatform {    instrumentationTests.behaviorForUnsupportedDevices=UnsupportedDeviceBehavior.Skip}

Extensions

An optional artifact withextensions is available for specific use cases. It contains the following APIs:

  • GrantPermissionExtension for granting permissions before each test

Can you think of more? Let's discuss in the issues section!

junitPlatform {    instrumentationTests.includeExtensions=true}

Jetpack Compose

To test@Composable functions on devices compatible with modern JUnit,enable support for instrumentation tests as described above. Then add the Compose test dependencyto yourandroidTestImplementation configuration and the plugin will autoconfigure JUnit 5 Compose support for you.

dependencies {// Setup from the previous section for enabling instrumentation tests...// Compose test framework    androidTestImplementation("androidx.compose.ui:ui-test-android:$compose_version")// Needed for createComposeExtension() and createAndroidComposeExtension()    debugImplementation("androidx.compose.ui:ui-test-manifest:$compose_version")}

The wiki includes a section on how to test your Composables.

Override the version of instrumentation test libraries

By default, the plugin will make sure to use a compatible version of the instrumentation test librarieswhen it sets up the artifacts automatically. However, it is possible to choose a custom version instead via its DSL:

junitPlatform {    instrumentationTests.version="2.0.1"}

Official Support

At this time, Google hasn't shared any immediate plans to bring first-party support for anything beyond JUnit 4 to Android.The following list is an aggregation of pending feature requests:

Support for @Rules

Since JUnit has replaced the@Rule mechanism with the concept of anExtension in version 5,the following artifacts help bridge the gap until Android officially transitions, if ever.

InstantExecutorExtension

ReplacesInstantTaskExecutorRule in JUnit 5.

dependencies {    testImplementation("io.github.neboskreb:instant-task-executor-extension:1.0.0")}

For more details seeinstant-task-executor-extension on GitHub.

Building Locally

This repository contains multiple modules, divided into two sub-projects.The repository's root directory contains build logic shared across the sub-projects,which in turn use symlinks to connect to the common build scripts in their parent folder.

  • instrumentation: The root folder for the instrumentation libraries & a sample. Open this folder in Android Studio.
  • plugin: The root folder for the Gradle plugin. Open this folder in IntelliJ IDEA.

Plugin Compatibility Map

For users that cannot match the current minimum version requirement of the Android Gradle Plugin requested by this plugin,refer to the table below to find a suitable alternative version. Note thatno active development will go into legacy versions,so please consider upgrading to at least AGP 8.2 before filing an issue with the latest one.

Your AGP VersionSuggested Plugin Version
>= 8.2.02.0.1
8.0.0 -8.1.41.12.2.0
7.0.0 -7.4.21.10.0.0
4.0.0 -4.2.21.8.2.1
3.5.0 -3.6.41.7.1.1
< 3.5.0none; you shouldreally update your build env, bro

License

Copyright 2017-2026 Marcel SchnelleLicensed under the Apache License, Version 2.0 (the "License");you may not use this file except in compliance with the License.You may obtain a copy of the License at  http://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License.

See also thefull License text.

About

Modern testing with JUnit for Android.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors13


[8]ページ先頭

©2009-2026 Movatter.jp