当运行命令gradle build时API无法运行

时间:2018-10-01 05:17:49

标签: java api gradle automation build.gradle

我正在致力于API自动化,在此我使用了放心的工具和Gradle进行构建。 当我运行命令Gradle build in terminal时,它不会运行在主类中声明的API。

我的build.gradle我如下

def starts_with(prefix, wordlist):
    return [word for word in wordlist if word.startswith(prefix)]

然后我的主类如下-

  plugins {
    id 'java'
}

sourceSets {
    main.java.srcDir "src/main"
    main.resources.srcDir "src/main/resources"
}
group 'com.API_AUTO'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}
dependencies {
    compile 'io.rest-assured:rest-assured:3.0.0'
    //compile "io.rest-assured, name: rest-assured, version: 3.0.2"
    compile group: 'org.testng', name: 'testng', version: '6.10'
    compile group: 'org.apache.poi', name: 'poi-ooxml', version: '3.15'
    compile group: 'net.sourceforge.jexcelapi', name: 'jxl', version: '2.6.12'
    compile group: 'commons-lang', name: 'commons-lang', version: '2.6'
    compile group: 'com.googlecode.htmlcompressor', name: 'htmlcompressor', version: '1.5.2'
    compile group: 'commons-dbutils', name: 'commons-dbutils', version: '1.6'
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.8.6'
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.8.6'
    compile group: 'mysql', name: 'mysql-connector-java', version: '6.0.5'
    compile group: "com.github.fge", name: "json-schema-validator", version: "2.2.6"
    compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1.1'
    compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1'
    compile group: 'org.json', name: 'json', version: '20160810'
    compile group: 'org.uncommons', name: 'reportng', version: '1.1.4'
    compile group: 'com.google.code.guice-repository', name: 'guice-repository', version: '2.1.0'
    compile group: 'org.easytesting', name: 'fest-assert-core', version: '2.0M10'
    compile group: 'org.uncommons', name: 'reportng', version: '1.1.4'
    compile group: 'org.apache.commons', name: 'commons-csv', version: '1.5'
    compile group: 'org.apache.commons', name: 'commons-exec', version: '1.3'
    compile group: 'com.opencsv', name: 'opencsv', version: '4.1'
    testCompile 'junit:junit:4.12'
    compile group: 'javax.xml.bind', name: 'jaxb-api', version: '2.3.0'
    testCompile group: 'org.slf4j', name: 'slf4j-simple', version: '1.6.1'
    compile 'com.relevantcodes:extentreports:2.41.2'
    compile group: 'com.sun.mail', name: 'javax.mail', version: '1.6.0'
    compile 'javax.mail:javax.mail-api:1.6.2'
    compile 'com.sun.mail:smtp:1.6.2'
    compile group: 'javax.mail', name: 'javax.mail-api', version: '1.6.2'

}
/*jar {
    from('src/main/java') {
        include 'resource/extent-config.xml'
        include 'resource/config.properties'
    }
    from configurations.compile.collect { zipTree it }
    manifest.attributes "Main-Class": "com.xxxx.yyy.mainRunner"
}*/

configurations {
    cucumberRuntime {
        extendsFrom testRuntime
    }
}

task cucumber() {
    dependsOn assemble, compileTestJava
    doLast {
        javaexec {
            main = "com.xxxx.yyy.mainRunner"
            classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
        }
    }
}

我的期望是,当我运行Gradle build in terminal命令时,它应该开始执行API。

1 个答案:

答案 0 :(得分:1)

我找到并解决了此问题。 有以下两种方法-

  1. 基本上,我试图运行该API,或者您可以通过build.gradle指出主文件夹中的类,以执行此操作。

我们应该同时具有测试文件夹和主文件夹结构,这些结构基本上都具有使用相应代码调用主文件夹类的测试方法。

我在下面更改了此内容!它像我的gradle.build一样通过其中定义的测试运行。

Runner file which invokes the main

gradle.build文件更改-

test {
    reports {
        junitXml.enabled = true
        html.enabled = false
        reports.junitXml.destination = file("test-output/reports/")
    }
    useTestNG()
            {
                useDefaultListeners = true
                options.suites("src/test/java/testApi_Test_Scripts/smsApiAutomationSuite.xml")
                options.listeners << 'com.kaleyra.smsApi.qa.framework.listener.CustomListener'
                //options.listeners << 'com.kaleyra.smsApi.qa.framework.listener.EmailListener'
                //options.listeners << 'org.uncommons.reportng.HTMLReporter'
                options.listeners << 'org.uncommons.reportng.JUnitXMLReporter'
                systemProperty 'org.uncommons.reportng.title', 'sms_api_automation_results'

            }
    testLogging.events "passed", "skipped", "failed"
    testLogging.showStandardStreams = true
    testLogging.exceptionFormat = "full"
  1. 将此添加到您的build.gradle-

    应用插件:“应用” mainClassName =“ com.XXXX.smsApi.qa.framework.mainRunner”

还有中提琴!

如果您问我哪个选择比我选择的更好。