场景失败时结束加特林模拟但生成报告

Que*_*ing 0 gatling

我有代码,如果它失败,当前不会运行我的场景;

//Defined outside of the scenario scope
var simulationHealthy = true

//defined within the scenario
.exec((session: io.gatling.core.session.Session) => {
  if (session.status == KO) {
      simulationHealthy = false
  }
    session
  })
Run Code Online (Sandbox Code Playgroud)

然而,我的模拟一直运行,直到模拟设置的持续时间结束,尽管场景不会继续执行。

我想要做的是让场景在我定义的条件下失败(类似于断言),并且整个模拟也在那一点失败,并生成报告。

谢谢

编辑:我在 IntelliJ IDE 中运行这些测试。需要以编程方式结束模拟。

Ger*_*cke 8

您可以在没有报告的情况下自行运行测试,并通过第二次调用生成报告,仅从 simulation.log

运行模拟无报告(-nr标志),即

gatling.sh -nr -s YourSimulationClass
Run Code Online (Sandbox Code Playgroud)

生成报告(-ro标志):

gatling.sh -ro yoursimulation
Run Code Online (Sandbox Code Playgroud)

(你的模拟是results文件夹下的路径,可以用 指定-rf,里面包含simulation.log文件)

在 IntelliJ 中,您可以定义另一个 LaunchConfiguration 要在之前执行。因此,您定义了一个用于执行 Gatling 测试(带有-nr标志)的操作和另一个用于报告生成(带有-ro标志)的配置,它在之前执行 Gatling 测试运行操作。

或者,您可以使用 gatling-maven-plugin 并使用相同的标志定义两个执行(运行、报告)。

编辑

根据此组线程,您可以有条件地执行您的步骤或将它们静音。条件可能是存在错误,但也可能是其他任何情况。如果条件取决于全局状态,即全局变量,它将使所有用户静音(与 不同exitHereIfFailed

例如:

val continue = new AtomicBoolean(true)
val scn = scenario("MyTest")
  .exec( 
    doIf(session => continue.get) {
      exec(http("request_0").get("/home").check(status.is(200)))
     .exec((session: io.gatling.core.session.Session) => {
       if (session.status == KO) {
         continue.set(false)
       }
       session
     })
  })
Run Code Online (Sandbox Code Playgroud)

如上所述,这只会停止向 SUT 发送请求。目前似乎没有其他选择(除了System.exit(0)


TMt*_*ech 5

您可以使用exitHereIfFailedinScenarioBuilder返回exec()

.exec(http("login")
    .post("/serviceapp/api/auth/login")
    ...
    .check(status.is(200))))
.exitHereIfFailed
.pause(1)
.exec(http("getProfileDetails")
      .get("/serviceapp/api/user/get_profile")
      .headers(authHeader("${token}"))
      .check(status.is(200)))
Run Code Online (Sandbox Code Playgroud)