This answer描述了一种运行特定浓缩咖啡测试的方法:

./gradlew app:connectedAndroidTest -Pandroid.testInstrumentationRunnerArguments.class=com.my.tests.MyTest

但是我想创建一个gradle任务来像这样运行它:
./gradlew app:runMyTest

但是当我尝试定义runMyTest任务时:
task runMyTest {
    finalizedBy connectedDAT

    project.extensions.add("android.testInstrumentationRunnerArguments.class", "com.my.tests.MyTest")
}

并运行它,我所有的测试都会运行,而不仅仅是指定的测试。

最佳答案

你可以做这样的事情,

apply plugin: 'java'

test {
  filter {
      //specific test method
      includeTestsMatching "com.yourpackage.YourTest"
  }
}

或查看下面的示例代码并解决问题,
sourceSets {

  integration {
    java.srcDir 'src/test/integration/java'
    resources.srcDir 'src/test/resources'
    compileClasspath += main.output + test.output
    runtimeClasspath += main.output + test.output
  }

}

configurations {
  integrationCompile.extendsFrom testCompile
  integrationRuntime.extendsFrom testRuntime
}

task integration(type: Test, description: 'Runs the integration tests.', group: 'Verification') {
  testClassesDir = sourceSets.integration.output.classesDir
  classpath = sourceSets.integration.runtimeClasspath
}

10-08 07:01