Code Monkey home page Code Monkey logo

android-junit5's Introduction

android-junit5

Travis Build Status

A Gradle plugin that allows for the execution of JUnit 5 unit tests in Android environments.

Download

buildscript {
    dependencies {
        classpath "de.mannodermaus.gradle.plugins:android-junit5:1.0.10"
    }
}

Snapshots of the development version are available through Sonatype's snapshots repository.

Setup

apply plugin: "com.android.application"
apply plugin: "de.mannodermaus.android-junit5"

dependencies {
    testImplementation junit5()

    // (Optional) If you need "parameterized tests"
    testImplementation junit5Params()
    
    // (Optional) For running tests inside Android Studio 3.x
    testCompileOnly junit5EmbeddedRuntime()
}

Usage

This plugin configures a junitPlatformTest task for each registered build variant of a project. It automatically attaches both the Jupiter & Vintage Engines during the execution phase of your tests as well.

More instructions on how to write JUnit 5 tests can be found in their User Guide. Furthermore, this repository provides a small showcase of the functionality provided by JUnit 5 here.

Android Studio Workarounds

Note:

The following section deals with fixing Test Execution within Android Studio 3. Running your JUnit 5 tests directly from Android Studio 2.3.3 and earlier will not work: You will encounter an AbstractMethodError when trying to do so (more information here).

The cause of this error is similar in nature to the one described below, and related to outdated APIs. Unlike that issue though, we can't fix the AbstractMethodError inside IntelliJ's internal runtime in the same way. Therefore, please resort to using Gradle for unit testing in Android Studio 2.

All versions up to and including Android Studio 3.0 Beta 7 are built on a version of IntelliJ IDEA that depends on outdated JUnit 5 APIs. Therefore, your tests will fail with an Exception similar to the following when you try to launch your tests from inside the IDE (using an Android JUnit Run Configuration):

Exception in thread "main" java.lang.NoSuchMethodError: org.junit.platform.launcher.Launcher.execute(Lorg/junit/platform/launcher/LauncherDiscoveryRequest;)V
	at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:42)
	...

To work around this, there is a separate dependency you can add to the test scope of your project in Android Studio 3. It provides its own copy of the JUnit 5 Runtime provided by a more recent build of IntelliJ, overriding the one embedded in Android Studio.

To use this, add the following line alongside the other junit5() dependencies:

testCompileOnly junit5EmbeddedRuntime()

As mentioned above, this will only fix Test Execution from Android Studio 3.

Extras

Override Dependency Versions

Inside the configuration closure applied by the plugin, you can specify the same properties as you would for a Java-based project with the JUnit Platform Gradle plugin. However, there are some additional properties that you can apply:

junitPlatform {
    // The JUnit Jupiter dependency version to use; matches the platform's version by default
    jupiterVersion "5.0.1"
    // The JUnit Vintage Engine dependency version to use; matches the platform's version by default
    vintageVersion "4.12.1"
}

JaCoCo Integration

If the plugin detects the usage of JaCoCo inside a project that it's being applied to, it will automatically configure additional tasks to report the unit test coverage of your application based on its JUnit 5 tests. There is no additional setup required to enable this behaviour. You can however customize the reports JaCoCo should generate.

Configuration is applied through the jacoco clause inside the plugin's DSL:

junitPlatform {
  jacoco {
    csvReport true
    xmlReport true
    htmlReport true
  }
}

License

android-junit5 is distributed with multiple Open Source licenses:

Please see the LICENSE.md files in the subfolders for more details.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.