Question

I am currently running my test via command

gradle app:connectedCheck

This works great, but as my test are building up, I don't want to go through all of them when I am trying to create one. Does anyone know how to just run one of my tests? Thanks in advance.

Was it helpful?

Solution

This is my detailed solution that I did with the help of MariusVolkhart:

Add this to your build.gradle:

spoon {
     if (project.hasProperty('spoonClassName')){
         className = project.spoonClassName
      }
}

Now, you can execute a specific class with a command like so:

gradle spoon -PspoonClassName=< com.your.pakage.ClassName>


Thats it!

However, if you want to run a series of specific test, create a file at the root of your Android project: runAllTests.sh. This script will contain the tests commands to run.

Edit your .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.WorkoutSchedule
 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"

Then, give the script permissions 1. cd to the script 2. type chmod u+x runAllTest.sh

You're set. Now just cd to your root, then to execute your test, type . 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 wrote 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 in terminal to the Results-SOME-DATE, and type open-results. Again, this was written for terminal. You may need to modify depending on your OS. But the structure should be the same

I hope this helps.

OTHER TIPS

Pretty sure connectedCheck only runs all the tests.

You might want to take a look at Spoon which well let you specify which class and method to execute.

There is a plugin which integrates Spoon with Gradle here.

You can test just a specific method of your Espresso test by these steps: 1) open your Espresso test class (using Android Studio) 2) go to the Structure window (located on the left side of Android Studio) 3) choose the specific test method you want to run 4) right click and then choose "Run..."

I hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top