Skip to content

Instantly share code, notes, and snippets.

@swenson
Last active July 30, 2021 22:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swenson/f797ffea7e243d889406 to your computer and use it in GitHub Desktop.
Save swenson/f797ffea7e243d889406 to your computer and use it in GitHub Desktop.
Run Android tests (returning with exit code 1 if they fail)
# Use whatever your test app directory is for ExampleTest, and whatever the
# package you are testing for com.example.test
export PATH="$ANDROID_HOME/platform-tools:$ANDROID_HOME/tools:$PATH"
pushd ExampleTest
# adb shell throws away the return value, so we have to hack do some magic
# see https://code.google.com/p/android/issues/detail?id=3254
adb logcat -c &&
python - <<END
import os
import re
import subprocess as sp
import sys
import threading
import time
done = False
def update():
# prevent CI from killing the process for inactivity
while not done:
time.sleep(5)
print "Running..."
t = threading.Thread(target=update)
t.dameon = True
t.start()
def run():
os.system('adb wait-for-device')
p = sp.Popen('adb shell am instrument -w com.example.test/android.test.InstrumentationTestRunner',
shell=True, stdout=sp.PIPE, stderr=sp.PIPE, stdin=sp.PIPE)
return p.communicate()
success = re.compile(r'OK \(\d+ tests\)')
stdout, stderr = run()
done = True
print stderr
print stdout
if success.search(stderr + stdout):
sys.exit(0)
else:
sys.exit(1) # make sure we fail if the tests fail
END
RETVAL=$?
adb logcat -d '*:E'
popd
exit $RETVAL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment