Skip to content

Instantly share code, notes, and snippets.

@Ghedeon
Created January 24, 2015 22:53
Show Gist options
  • Save Ghedeon/640b9fdaa4bc4460cc41 to your computer and use it in GitHub Desktop.
Save Ghedeon/640b9fdaa4bc4460cc41 to your computer and use it in GitHub Desktop.
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "test.co.vv.myapplication"
minSdkVersion 15
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
}
import com.android.builder.testing.TestRunner
import com.android.build.gradle.internal.test.TestDataImpl
import com.android.builder.testing.SimpleTestRunner
import org.gradle.logging.ConsoleRenderer
import com.android.build.gradle.internal.core.GradleVariantConfiguration
project.afterEvaluate {
project.("connectedAndroidTest"){
actions = []
doLast{
File resultsOutDir = getResultsDir()
emptyFolder(resultsOutDir)
File coverageOutDir = getCoverageDir()
emptyFolder(coverageOutDir)
if (testVariantData.outputs.size() > 1) {
throw new RuntimeException("Multi-output in test variant not yet supported")
}
boolean success = false;
// If there are tests to run, and the test runner returns with no results, we fail (since
// this is most likely a problem with the device setup). If no, the task will succeed.
//////////////////////////
GradleVariantConfiguration variantConfiguration = testVariantData.variantConfiguration
def javaDirectories = variantConfiguration.sortedSourceProviders*.javaDirectories
def testsFound = !project.files(javaDirectories).asFileTree.empty
//////////////////////////
if (!testsFound) {
logger.info("No tests found, nothing to do.")
// If we don't create the coverage file, createXxxCoverageReport task will fail.
File emptyCoverageFile = new File(coverageOutDir, SimpleTestCallable.FILE_COVERAGE_EC)
emptyCoverageFile.createNewFile()
success = true;
} else {
File testApk = testVariantData.outputs.get(0).getOutputFile()
String flavor = getFlavorName()
TestRunner testRunner = new SimpleTestRunner(getAdbExec());
deviceProvider.init();
try {
success = testRunner.runTests(project.name, flavor,
testApk, new TestDataImpl(testVariantData),
deviceProvider.devices,
deviceProvider.getMaxThreads(),
deviceProvider.getTimeout(),
resultsOutDir, coverageOutDir, getILogger());
} finally {
deviceProvider.terminate();
}
}
// run the report from the results.
File reportOutDir = getReportsDir()
emptyFolder(reportOutDir)
TestReport report = new TestReport(ReportType.SINGLE_FLAVOR, resultsOutDir, reportOutDir)
report.generateReport()
if (!success) {
testFailed = true
String reportUrl = new ConsoleRenderer().asClickableFileUrl(
new File(reportOutDir, "index.html"));
String message = "There were failing tests. See the report at: " + reportUrl;
if (getIgnoreFailures()) {
logger.warn(message)
return
} else {
throw new GradleException(message)
}
}
testFailed = false
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment