Cucumber多标签匹配疑问:为何@tag2 and @tag3会执行对应场景?
Cucumber标签匹配不符合预期的原因解析
问题场景
在Cucumber的Feature文件中,存在标注了@tag1 @tag2 @tag3标签的Scenario_2场景。在Runner类中配置tags = "@tag2 and @tag3",原本预期该场景不会被匹配执行,但实际却运行了Scenario_2的所有步骤,这是为什么?
Feature文件代码
@tag1 @tag2 @tag3 Scenario: Scenario_2 Given This is the first step in Second Scenario When This is the second step in Second Scenario Then This is the third step in Second Scenario
Runner类代码
@RunWith(Cucumber.class) @CucumberOptions(features = {"src/test/resources/features/cucumbertags_26.feature"}, glue = {"com.stepdefinitions"}, tags = "@tag2 and @tag3", monochrome = true) public class TestRunner_SamplePage { }
实际执行输出
^This is the first step in Second Scenario$ ^This is the second step in Second Scenario$ ^This is the third step in Second Scenario$
原因说明
这是Cucumber标签匹配的正常预期行为:
- Cucumber中
@tag2 and @tag3的逻辑是:匹配同时包含@tag2和@tag3标签的场景,场景是否带有其他额外标签(比如这里的@tag1)不影响匹配结果。 - 你的Scenario_2同时具备@tag2和@tag3两个标签,完全满足匹配条件,因此会被执行。
- 若要实现“只包含@tag2和@tag3,排除带有其他标签的场景”,需要调整标签逻辑为:
(@tag2 and @tag3) and not @tag1。
内容的提问来源于stack exchange,提问作者asn
相关产品推荐
相关产品推荐

