You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Karate-Gatling按标签执行场景异常:仅运行@perf标签场景的方法

问题

我希望对Feature文件中带有@perf标签的scenario1执行性能测试,Feature文件内容如下:

@tag1 @tag2 @tag3

**background:**
user login

@tag4 @perf
**scenario1:**

@tag4
**scenario2:**

我的Scala仿真文件配置如下:

class PerfTest extends Simulation {

  val protocol = karateProtocol()


  val getTags = scenario("Name goes here").exec(karateFeature("classpath:filepath"))

  setUp(
    getTags.inject(
      atOnceUsers(1)
      ).protocols(protocol)
  )

}

我尝试过两种方式筛选标签:

  • 通过命令行传参:
    mvn clean test-compile gatling:test "-Dkarate.env={env}" "-Dkarate.options= --tags @perf"
    
  • 在Scala代码中给karateFeature方法传入@perf参数:
    val getTags = scenario("Name goes here").exec(karateFeature("classpath:filepath", "@perf"))
    

但两种方式都会同时执行两个场景,请问如何强制仅运行带有@perf标签的测试?

解决方案

1. 修正Feature文件的Gherkin语法

当前Feature文件的语法不符合标准Gherkin规范,会导致Karate无法正确识别标签和场景,需要调整为标准格式:

@tag1 @tag2 @tag3
Feature: 测试功能

Background:
  Given user login

@tag4 @perf
Scenario: 场景1
  # 补充具体测试步骤

@tag4
Scenario: 场景2
  # 补充具体测试步骤

2. 调整标签筛选的参数格式

命令行方式

修正karate.options的引号和空格格式,避免参数解析错误:

mvn clean test-compile gatling:test -Dkarate.env={env} -Dkarate.options="--tags @perf"

代码内指定标签

确保karateFeature的标签参数格式正确,直接传入标签即可:

val getTags = scenario("仅运行带@perf的场景").exec(karateFeature("classpath:filepath", "@perf"))

3. 额外排查项

如果以上操作后仍未解决问题,检查以下内容:

  • 确认classpath:filepath路径正确,指向目标Feature文件
  • 验证Karate与Gatling的版本兼容性,避免版本bug导致标签筛选失效

内容的提问来源于stack exchange,提问作者rc22

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 05:00:53