无法强制 gradlew 根据条件构建失败

问题描述 投票:0回答:1

我已将

StandardOutputListener
添加到我的
gatlingRun
文件下的
build.gradle
任务中。预期是,如果作业开始因标准输出中失败的请求而出现错误
OK=0
,则任务
gatlingRun
和 gradle 构建应该会失败。

build.gradle

plugins {
    id 'java'
    id 'io.gatling.gradle' version '3.9.0.2'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

gatling {
    simulations =  {
        exclude "**/simulations/CallBackSimulation.java"
    }
}

dependencies {
    gatling 'org.json:json:20230227'
    // ...
}

test {
    useJUnitPlatform()
}

// Problematic method
tasks.named("gatlingRun") {
    def outputListener = new StandardOutputListener() {
        void onOutput(CharSequence output) {
            if(output.contains("OK=0")) {
                throw new BuildCancelledException('An error occurred with Gatling requests')
            }
        }
    }
    logging.addStandardOutputListener(outputListener)
}

当我运行gradle命令时,构建仍然成功。虽然异常被准确抛出,但仍然打印 Build Sucessful 消息,并且

echo $?
返回 0,而不是 1。

$ ./gradlew gatlingRun
> Task :gatlingRun
Gatling 3.9.5 is available! (you're using 3.9.0)
Simulation com.example.Simulation1 started...

================================================================================
2023-08-24 17:33:46                                           0s elapsed
---- Requests ------------------------------------------------------------------
> Global                                                   (OK=0      KO=1     )
Could not read standard output of command '/usr/lib/jvm/java-8-openjdk-amd64/bin/java'.
org.gradle.api.BuildCancelledException: An error occurred with Gatling requests
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        { ... a lot of nested exceptions}
        at java.lang.Thread.run(Thread.java:750)

Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

See https://docs.gradle.org/7.4/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 4s
4 actionable tasks: 1 executed, 3 up-to-date
$ echo $?
0

我做错了什么?我还尝试过

GradleException
StopExecutionException
。不工作。有人可以帮忙吗?

gradle groovy gatling
1个回答
0
投票

这不是加特林测试失败的正确方法。您必须在模拟中定义断言

© www.soinside.com 2019 - 2024. All rights reserved.