Last active
August 29, 2015 14:07
-
-
Save loeschg/3ce24c34dec364270ab8 to your computer and use it in GitHub Desktop.
Robolectric tests are not working with double-espresso due to it not excluding espresso files. This snippet allows you to exclude Espresso resources when running Robolectric tests. More info can be found here: http://stackoverflow.com/a/24706452/413254
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Hook into existing tasks. */ | |
tasks.whenTaskAdded { theTask -> | |
excludeEspressoFilesIfNecessary(theTask) | |
} | |
/** | |
* This excludes Espresso tests from Robolectric. http://stackoverflow.com/a/24706452/413254 | |
* | |
* This was needed when using double-espresso. I couldn't get Robolectric to exclude them | |
* normally unless doing this. Double-espresso is used to fix a conflicting Dagger 1.2 dependency | |
* with Espresso. | |
* | |
* Android Testing. (╯°□°)╯︵ ┻━┻ | |
*/ | |
def excludeEspressoFilesIfNecessary(theTask) { | |
// Modify these if needed. | |
def final TEST_SRC_DIR = 'src/test/java' | |
def final TO_EXCLUDE = '**/espresso/**/*.java' | |
def final compileTestPattern= /compileTest([^ ]*)Java/ | |
if (theTask.name.matches(compileTestPattern)) { // find all task names. | |
// Grabs the name of the variant to use when naming the new task. | |
def matcher = (theTask.name =~ compileTestPattern) | |
def cleanupTaskName = "touchUp${matcher[0][1]}RobolectricSourceSet" | |
// Exclude espresso and add the task as a dependency. | |
project.task(cleanupTaskName) << { | |
def FileTree tree = fileTree(dir: TEST_SRC_DIR) | |
tree.exclude TO_EXCLUDE | |
theTask.source = tree | |
} | |
theTask.dependsOn(cleanupTaskName) | |
} | |
} |
line 30: should be
def FileTree tree = fileTree(dir: TEST_SRC_DIR)
otherwise gradle 2.1 gets confused
@asherf, I updated. Thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
From http://stackoverflow.com/a/24706452/413254 and modified to handle variants (flavors).