Skip to content

Commit

Permalink
Released 1.0.0-M4-rev1
Browse files Browse the repository at this point in the history
* Some advancements towards AGP 3.0.0

* Expanded Unit Test suite to run tests for 2.x & 3.x variants of the AGP

* Mark test source folders as such in IDEA

* Use default JUnit Platform version

* Performed some sweeping inside the plugin’s compile & runtime dependencies, and tasks

* JUnit 4 is included by default, which allows AS builds w/o “class not found”
* Deprecated junitVintage() dependency handler & issue a warning
* Both TestEngines are included on the runtime classpath by default
* Disable the default unit test task if JUnit 5 is used
* Some clean-up

* Moved around unit test cases for 2.x & 3.x

* Bump to 1.0.0-M4-rev1
  • Loading branch information
aurae authored May 19, 2017
1 parent a168d73 commit 07015f5
Show file tree
Hide file tree
Showing 14 changed files with 365 additions and 155 deletions.
2 changes: 2 additions & 0 deletions .idea/runConfigurations/Run_Unit_Tests.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ buildscript {
}
dependencies {
// 2. Add the plugin as a classpath dependency
classpath "de.mannodermaus.gradle.plugins:android-junit5:1.0.0-M4"
classpath "de.mannodermaus.gradle.plugins:android-junit5:1.0.0-M4-rev1"
}
}
Expand All @@ -22,15 +22,14 @@ apply plugin: "de.mannodermaus.android-junit5"
dependencies {
// 4. Add the testCompile dependencies on JUnit Jupiter
testCompile junitJupiter()
// 5. (Optional) Add the testCompile dependency on the JUnit Vintage Engine
testCompile junitVintage()
}
```

## Usage

This plugin configures the `junitPlatform` task for each registered build variant of a project. Further instructions on how to write JUnit 5 tests can be found [in their User Guide][junit5ug].
This plugin configures the `junitPlatform` task for each registered build variant of a project. Starting with version `1.0.0-M4-rev1`, the plugin automatically attaches both the Jupiter & Vintage Engines.

Further instructions on how to write JUnit 5 tests can be found [in their User Guide][junit5ug].

## Extras

Expand Down
51 changes: 49 additions & 2 deletions android-junit5/build.gradle
Original file line number Diff line number Diff line change
@@ -1,23 +1,70 @@
apply plugin: 'groovy'
apply plugin: 'idea'
apply plugin: 'maven'
apply plugin: 'maven-publish'
apply plugin: 'com.jfrog.bintray'

sourceCompatibility = JavaVersion.VERSION_1_6
targetCompatibility = JavaVersion.VERSION_1_6

configurations {
testAgp2xCompile {
extendsFrom configurations.testCompile
}
testAgp3xCompile {
extendsFrom configurations.testCompile
}
}

sourceSets {
testAgp2x {
java.srcDir "src/testAgp2x/groovy"
compileClasspath += sourceSets.test.output
runtimeClasspath += sourceSets.test.output
}
testAgp3x {
java.srcDir "src/testAgp3x/groovy"
compileClasspath += sourceSets.test.output
runtimeClasspath += sourceSets.test.output
}
}

idea {
module {
testSourceDirs += file("src/testAgp2x/groovy")
testSourceDirs += file("src/testAgp3x/groovy")
}
}

dependencies {
compile gradleApi()
compile localGroovy()
compile "org.junit.platform:junit-platform-gradle-plugin:$JUNIT_PLATFORM_VERSION"

testCompile "com.android.tools.build:gradle:$ANDROID_PLUGIN_VERSION"
testCompile "junit:junit:$JUNIT4_VERSION"
testCompile("org.spockframework:spock-core:$SPOCK_VERSION") {
transitive = false
}
testCompile "junit:junit:$JUNIT4_VERSION"

testAgp2xCompile "com.android.tools.build:gradle:2.3.2"
testAgp3xCompile "com.android.tools.build:gradle:3.0.0-alpha1"
}

// Run Unit Tests against Android Gradle Plugin version 2.x
task testAgp2x(type: Test) {
testClassesDirs = sourceSets.testAgp2x.output.classesDirs
classpath = sourceSets.main.runtimeClasspath + sourceSets.testAgp2x.runtimeClasspath
}

// Run Unit Tests against Android Gradle Plugin version 3.x
task testAgp3x(type: Test) {
testClassesDirs = sourceSets.testAgp3x.output.classesDirs
classpath = sourceSets.main.runtimeClasspath + sourceSets.testAgp3x.runtimeClasspath
}

// Combine all tests when executing the main JUnit task
tasks.getByName("test").dependsOn(testAgp2x, testAgp3x)

version = VERSION_NAME

bintray {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package de.mannodermaus.gradle.anj5

class AndroidJUnit5Compat {

/**
* Fetches the Java output directories for the given Variant scopes
* across different versions of the Android Gradle plugin.
* @param variantScope VariantScope to look up the Java outputs from
* @return An Iterable container depicting the output directories
*/
static Iterable<File> getJavaOutputDirs(variantScope) {
if (variantScope.hasProperty("javaOutputs")) {
return variantScope.javaOutputs

} else if (variantScope.hasProperty("javaOuptuts")) {
return variantScope.javaOuptuts

} else {
return Collections.singletonList(variantScope.javaOutputDir)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ import org.junit.platform.gradle.plugin.JUnitPlatformExtension
*/
class AndroidJUnit5PlatformExtension extends JUnitPlatformExtension {

private static final String PLATFORM_VERSION = "1.0.0-M4"

AndroidJUnit5PlatformExtension(Project project) {
super(project)
platformVersion = PLATFORM_VERSION
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ import org.junit.platform.gradle.plugin.*
*/
class AndroidJUnitPlatformPlugin extends JUnitPlatformPlugin {

private static final String LOG_TAG = "[android-junit5]"

private static final String VINTAGE_WARNING = "AGPBI: {\"kind\":\"warning\",\"text\":\"$LOG_TAG You don't need to depend on junitVintage() directly anymore!\",\"sources\":[{},{}]}"

private static final String EXTENSION_NAME = 'junitPlatform'
private static final String TASK_NAME = 'junitPlatformTest'

Expand Down Expand Up @@ -51,22 +55,34 @@ class AndroidJUnitPlatformPlugin extends JUnitPlatformPlugin {
// by this plugin.
def configuration = project.configurations.maybeCreate('junitPlatform')
configuration.defaultDependencies { deps ->
def version = junitExtension.platformVersion
deps.add(project.dependencies.create("org.junit.platform:junit-platform-launcher:${version}"))
deps.add(project.dependencies.create("org.junit.platform:junit-platform-console:${version}"))
// By default, include both TestEngines
// and the Launcher-related dependencies
// on the runtime classpath
def platformVersion = junitExtension.platformVersion
deps.add(project.dependencies.create("org.junit.platform:junit-platform-launcher:${platformVersion}"))
deps.add(project.dependencies.create("org.junit.platform:junit-platform-console:${platformVersion}"))

def jupiterVersion = junitExtension.jupiterVersion
deps.add(project.dependencies.create("org.junit.jupiter:junit-jupiter-engine:${jupiterVersion}"))

def vintageVersion = junitExtension.vintageVersion
deps.add(project.dependencies.create("org.junit.vintage:junit-vintage-engine:${vintageVersion}"))
}

// Add a junitJupiter() dependency handler
project.dependencies.ext.junitJupiter = {
def jupiterVersion = junitExtension.jupiterVersion
project.dependencies.create("org.junit.jupiter:junit-jupiter-api:${jupiterVersion}")
project.dependencies.create("org.junit.jupiter:junit-jupiter-engine:${jupiterVersion}")

return [
project.dependencies.create("junit:junit:4.12"),
project.dependencies.create("org.junit.jupiter:junit-jupiter-api:${jupiterVersion}"),
]
}

// Add a junitVintage() dependency handler
project.dependencies.ext.junitVintage = {
def vintageVersion = junitExtension.vintageVersion
project.dependencies.create("org.junit.vintage:junit-vintage-engine:${vintageVersion}")
project.logger.warn(VINTAGE_WARNING)
return []
}

project.afterEvaluate {
Expand All @@ -86,12 +102,12 @@ class AndroidJUnitPlatformPlugin extends JUnitPlatformPlugin {
// Obtain variant properties
def variantData = variant.variantData
def variantScope = variantData.scope
def scopeJavaOutputs = variantScope.hasProperty("javaOutputs") ? variantScope.javaOutputs : variantScope.javaOuptuts
def scopeJavaOutputs = AndroidJUnit5Compat.getJavaOutputDirs(variantScope)

// Obtain tested variant properties
def testedVariantData = variant.testedVariant.variantData
def testedVariantScope = testedVariantData.scope
def testedScopeJavaOutputs = testedVariantScope.hasProperty("javaOutputs") ? testedVariantScope.javaOutputs : testedVariantScope.javaOuptuts
def testedScopeJavaOutputs = AndroidJUnit5Compat.getJavaOutputDirs(testedVariantScope)

// Collect the root directories for unit tests from the variant's scopes
def testRootDirs = []
Expand All @@ -111,19 +127,24 @@ class AndroidJUnitPlatformPlugin extends JUnitPlatformPlugin {
classpath.add(scopeJavaOutputs)
}

// 2) Add the testApk configuration
def testApk = project.configurations.findByName("testApk")
if (testApk != null) {
classpath.add(testApk)
}
// 2) Add the runtime configurations
// def testRuntime = project.configurations.findByName("testRuntimeOnly")
// if (testRuntime == null) {
// testRuntime = project.configurations.findByName("testApk")
// }
// if (testRuntime != null) {
// classpath.add(testRuntime)
// }

// 3) Add test resources
classpath.add(variantData.javaResourcesForUnitTesting)
classpath.add(testedVariantData.javaResourcesForUnitTesting)

// 4) Add filtered boot classpath
def globalScope = variantScope.globalScope
classpath.add(globalScope.androidBuilder.getBootClasspath(false).findAll { it.name != "android.jar" })
classpath.add(globalScope.androidBuilder.getBootClasspath(false).findAll {
it.name != "android.jar"
})

// 5) Add mocked version of android.jar
classpath.add(globalScope.mockableAndroidJarFile)
Expand Down Expand Up @@ -152,6 +173,11 @@ class AndroidJUnitPlatformPlugin extends JUnitPlatformPlugin {
group: 'verification',
description: 'Runs tests on the JUnit Platform.') { junitTask ->

// Disable the default Unit Test task, since we're running JUnit 5 anyway
def defaultTestTask = project.tasks.findByName("test${nameSuffix}UnitTest")
defaultTestTask.setEnabled(false)
defaultTestTask.dependsOn += junitTask

junitTask.inputs.property('enableStandardTestTask', junitExtension.enableStandardTestTask)
junitTask.inputs.property('selectors.uris', junitExtension.selectors.uris)
junitTask.inputs.property('selectors.files', junitExtension.selectors.files)
Expand Down Expand Up @@ -188,25 +214,6 @@ class AndroidJUnitPlatformPlugin extends JUnitPlatformPlugin {

junitTask.main = ConsoleLauncher.class.getName()
junitTask.args buildArgs(project, junitExtension, reportsDir, testRootDirs)

doFirst {
project.logger.info("CLASS PATH ==")
classpath.each {
project.logger.info("$it")
}
project.logger.info("=============")
project.logger.info("TASK ARGS ===")
project.logger.info("${junitTask.args.join(" ")}")
project.logger.info("=============")

// def rootDirs = []
// project.sourceSets.each { sourceSet ->
// rootDirs.add(sourceSet.output.classesDir)
// rootDirs.add(sourceSet.output.resourcesDir)
// rootDirs.addAll(sourceSet.output.dirs.files)
// }
// args.addAll(['--scan-class-path', rootDirs.join(File.pathSeparator)])
}
}
}

Expand Down Expand Up @@ -245,10 +252,10 @@ class AndroidJUnitPlatformPlugin extends JUnitPlatformPlugin {
args.addAll(['-n', pattern])
}
filters.packages.include.each { includedPackage ->
args.addAll(['--include-package',includedPackage])
args.addAll(['--include-package', includedPackage])
}
filters.packages.exclude.each { excludedPackage ->
args.addAll(['--exclude-package',excludedPackage])
args.addAll(['--exclude-package', excludedPackage])
}
filters.tags.include.each { tag ->
args.addAll(['-t', tag])
Expand Down
Loading

0 comments on commit 07015f5

Please sign in to comment.