首页 > 解决方案 > 无法从 gradle 命令行执行 testng.xml 文件

问题描述

我在 android studio 中使用 kotlin 创建了一个用于移动自动化的 gradle 项目,并在 build.gradle 中创建了任务当我从命令提示符执行测试时,例如:'gradle Test' 我的测试没有被执行(正在打印消息,上面提到了这个任务)。当我直接从 testng.xml 文件执行时,脚本会成功执行。

下面是 build.gradle 的示例代码 App -> build.gradle

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion 28
defaultConfig {
    applicationId "com.appium.automation"
    minSdkVersion 21
    targetSdkVersion 28`enter code here`
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"


}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }

}

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {

implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation 'io.appium:java-client:7.0.0'
implementation 'org.assertj:assertj-core:3.12.2'
implementation 'org.testng:testng:6.14.3'
implementation 'org.junit.jupiter:junit-jupiter-api:5.5.0-M1'
implementation 'io.cucumber:cucumber-java8:4.3.1'
implementation 'org.apache.commons:commons-lang3:3.9'
implementation 'io.cucumber:cucumber-testng:4.3.1'
implementation 'io.cucumber:gherkin:5.1.0'
implementation 'com.aventstack:extentreports:4.0.9'
}

tasks.withType(Test) {
print("Hi")
useTestNG(){
    println("Hi2")
    suites '/app/testng.xml'
    println("Hi3")
}
}

项目-> build.gradle

buildscript {
ext.kotlin_version = '1.3.31'
repositories {
    google()
    jcenter()
    mavenCentral()
}

dependencies {
    classpath 'com.android.tools.build:gradle:3.4.0'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}
}
allprojects {
repositories {
    google()
    jcenter()
    mavenCentral()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}

标签: androidgradlekotlintestngappium

解决方案


要从命令行执行具体测试,您可以尝试:

./gradlew test --tests "com.MyTestCkass.myTest"

推荐阅读