15

I have some library project that has its own tests. I'm not responsible for this library project and don't care about its tests, however, when I run gradle :app:connectedCheck it runs my tests but it also runs the dependencies' tests. Is there any way I can prevent this?

I should mention that my dependencies are not within the app module I speak of.

EDIT: More specifically, the library project I'm depending on has extremely long tests as they are meant to run on a build server at 2 in the morning, so I'm sitting here waiting for paint to dry just to run my really short tests.

EDIT2: I've also tried using Spoon. I have it setup right now and can run individual classes, but I'd like to run everything in one package. Is that possible?

EDIT3: The folder structure is insignificant but here's a very very non-detailed look:

root
-some_library_project
-main_project
-settings.gradle

The main_project build.gradle looks like this. I should mention that spoon is currently doing nothing, but my options are open:

buildscript{
 repositories {
     jcenter()
 }
 dependencies{
     classpath 'com.stanfy.spoon:spoon-gradle-plugin:0.10.+'
 }
}

apply plugin: 'com.android.application'
apply plugin: 'spoon'

android {
    compileSdkVersion 20
    buildToolsVersion '20.0.0'

 defaultConfig {
    applicationId "com.something.main_project"
    minSdkVersion 17
    targetSdkVersion 20
    versionCode 1
    versionName "1.0"

    testPackageName "com.something.main_project.test"
    testInstrumentationRunner "android.test.InstrumentationTestRunner"

 }

 signingConfigs {
    debug {
        storeFile file("debug.keystore")
    }

    release {
        storeFile file("release.keystore")
        storePassword "something"
        keyAlias "something"
        keyPassword "something"
    }
 }

 buildTypes {
    release {
        runProguard false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        signingConfig signingConfigs.release
    }
    debug{
        signingConfig signingConfigs.debug
    }
 }

}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

    compile project(':some_library_project')

    androidTestCompile 'com.squareup.spoon:spoon-client:1.1.0'
}

spoon {
    if (project.hasProperty('spoonClassName')){
        className = project.spoonClassName
    }
}
snotyak
  • 3,709
  • 6
  • 35
  • 52
  • 1
    The odd thing is that they _shouldn't_ be running when you qualify the task with `:app:` like that. You could try explicitly skipping the library task like this: `./gradlew connectedAndroidTest -x :mylib:connectedAndroidTest` – Krylez Jul 15 '14 at 18:10
  • were you able to figure out how to skip the tests in library projects ? – rahul.ramanujam Jul 30 '16 at 03:14

2 Answers2

5

You have the Spoon plugin, but are running with gradle :app:connectedCheck. First, make these changes, then run with gradle spoon

Comment out androidTest.setRoot any other build.gradle other that the main package

Add these to build.gradle of the main package:

Under tag android

sourceSets {  //this one may not be needed. But wont hurt.
    androidTest.setRoot('src/androidTest')
}

Modify spoon tag:

 spoon {
     debug = true
     testSizes = ['small', 'medium', 'large']   <<--- Change this to run specific test sizes
     if (project.hasProperty('spoonClassName')){
        className = project.spoonClassName
     }
  }

To run

All tests:

gradle spoon

Specific Test Class

gradle spoon -PspoonClassName=[THE.PACKAGE.NAME]


Edit

After reading your edit "Edit2", I see that this will only specify a certain class to run, and not a specific package. Unfortunately, I have not found a solution to this, only a work-around. I built a bash script and in it I added my classes to test, like so:

Step 1: Create a file at the root of your Android project: runAllTests.sh

Step 2: Edit .sh to look like this:

 #!/bin/sh
 date +%b-%dT%H.%M > timestamp.out

 sites="$HOME"/path/to/project/root

 timestamp="$(cat "$sites"/timestamp.out)"
 result_folder="$sites"/results
 destdir="$result_folder/Results-$timestamp"

 mkdir -p "$destdir"
 echo "Directory created: ${destdir##*/}"

  <---------- Here you start running the test --------------->

  echo "Starting Master Setup"
  gradle spoon -PspoonClassName=com.espresso.test.MasterSetup
  cp -r "$sites"/app/build/spoon "$destdir"/MasterSetup
  echo "Results saved to MasterSetup"

  echo "Starting WorkoutSchedule"
  gradle spoon -PspoonClassName=com.espresso.test.CupcakeSchedule
  cp -f "$sites"/app/build/spoon "$destdir"/WorkoutSchedule
  echo "Results saved to WorkoutSchedule"

  echo "Starting Setting.test"
  gradle spoon -PspoonClassName=com.espresso.test.Settings
  cp -r "$sites"/app/build/spoon "$destdir"/Settings
  echo "Results saved to Settings"

Step 3: Give the script permissions

  1. cd to the script
  2. type chmod u+x runAllTest.sh

So, what this does:

  1. First, it creates a timestamp.out. I use this so I can save my results to a file over and over without previous results being overwritten. You do not need this part.

  2. Next, it creates a result folder in the root of your project if it is not already there.

  3. Then, it will make a folder inside the results folder named Results-SOME-DATE.

  4. Lastly, each test will run, saving the results to the normal spot on your project. (Inside build/spoon) Once test are complete it will copy the results to the results folder, and name each test result appropriately so it is easy to see all your tests ran.

NOTE: This script was written for MAC. If your on windows or anything else, this script may need modifications.


Additionally: You will find it is inconvenient to open in to each folder to get the index.html opened. So I wrote this script to add to your bash_profile:

function open-results () {
# the browser to open up `index.html' in.
browser='/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'

# let the user know what directory we're looking in
printf "looking in %s" "$(pwd)"
echo ...

for paths in $(find ./ -name 'debug' -type d); do
  for files in $(find "$paths" -name 'index.html'); do
    open -a "$browser" "$files"
  done
done
echo done
}

Now, cd to the Results-SOME-DATE, and type open-results. Again, this was written for bash. You may need to modify depending on your OS. But the structure should be the same

I hope this helps.

Chad Bingham
  • 32,650
  • 19
  • 86
  • 115
  • Could you clarify which Gradle plugin for Spoon you're actually using? There's the one from [stanfy](https://github.com/stanfy/spoon-gradle-plugin), and the one from [x2on](https://github.com/x2on/gradle-spoon-plugin). I'm confused because you use `testSizes`, which seems to be a feature of only the latter, but you also use `className` which seems to be a feature on only the first. – sschuberth Nov 28 '14 at 11:41
  • @sschuberth I used stanfy plugin. I am not sure about how I got test sizes to work... I don't work on this project any more or I would look, but I think I may have implemented another testing lib to specify test sizes. I thought it was included with stanfy. Sorry I am not more help. – Chad Bingham Nov 28 '14 at 17:26
3

If you have a project dependency to this library, gradle wants to build and test it, since both is the usual build process of a java project. To avoid these test you can do one of this:

  1. like Krylez mentioned it in his comment you may just skip the specific test task with ./gradlew connectedAndroidTest -x :mylib:connectedAndroidTest
  2. you filter your tests in the test closure like it is explained here: http://www.gradle.org/docs/current/userguide/java_plugin.html#sec:java_test
The End
  • 709
  • 3
  • 18
  • #1 doesn't work, unfortunately. #2 doesn't work unless I'm putting it in the wrong place. I've also tried replacing `test` with `androidTest` – snotyak Jul 16 '14 at 19:59
  • gradle executed the test tasks even when you say him to skip those tasks? Can you add the folder structure of your projects and build.gradle please. – The End Jul 17 '14 at 05:46
  • Yep. Added. And yes, the directories are on the same level – snotyak Jul 19 '14 at 16:05