【问题标题】:API's does not run when run command gradle build运行命令 gradle build 时 API 不运行
【发布时间】:2019-03-06 04:46:01
【问题描述】:

我正在从事 API 自动化方面的工作,我在构建时使用了放心和 Gradle。 当我在终端中运行命令 Gradle build 时,它不会运行我在主类中声明的 API。

我的build.gradle我的如下

  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
        }
    }
}

那么我的mainclass如下-

    import Api_Test_Scripts.RunnerSub;

public class mainRunner {
    public static void main(String[] args) throws InterruptedException {
        RunnerSub obj= new RunnerSub();
        obj.starttest();
        obj.runner();
        obj.endReport();
    }
}

我的期望是当我在终端中运行命令 Gradle build 时,它应该开始执行 API。

【问题讨论】:

  • 您可能需要阅读一些基础知识。应用程序的 api 和入口点是不一样的。您似乎也不了解命名约定。现在是这样:为什么要让这个 main 方法被 build 触发,你怎么知道它没有运行?
  • @Stultuske 我知道它没有运行,因为我已经添加了日志,并且在通过命令 Gradle build 运行 API 时不会出现,当我使用 IntelliJ 时它确实向我显示了日志。我也想从主程序运行它,因为它会创建一个也可以运行的 jar。没有 main 无法创建 jar。我唯一的预期结果是,当我运行命令 Gradle build 时,它应该开始运行 API。
  • 您添加了日志...不是根据您发布的代码。您的 IntelliJ 运行很可能使用 Gradle 构建之外的其他属性,因此不要认为这是“最终证明”。但是,构建应该是构建您的应用程序,而不是运行它。
  • 没有帮助我。仍然感谢您的时间。 @Stultuske

标签: java api gradle automation build.gradle


【解决方案1】:

我发现并解决了这个问题。 有以下两种方法-

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

我们应该同时拥有测试文件夹和主文件夹结构,它们基本上具有使用各自代码调用主文件夹类的测试方法。

我在下面做了这个更改瞧!它的工作原理是我的 gradle.build 是通过其中定义的测试运行的。

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"

还有中提琴!

如果你问我哪个选项比我会选择第一个。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-19
    • 1970-01-01
    • 1970-01-01
    • 2023-02-04
    • 2015-05-15
    • 2018-08-19
    • 1970-01-01
    • 2019-12-09
    相关资源
    最近更新 更多