如何用Maven命令仅运行Cucumber Feature文件中的单个测试场景?
问题描述
执行命令mvn test -Dcucumber.options="src\test\resources\features\login.feature:9"试图运行第9行的场景,但实际执行了全部两个场景,需要正确的Maven测试命令。
你的login.feature文件内容
Scenario: Check login with valid credentials Given user is on login page When user enters valid username and password And clicks on login button Then user is navigated to the home page Scenario: Check login with invalid credentials Given user is on login page When user enters invalid username and password And clicks on login button Then error message is displayed - Invalid Credentials
正确命令及说明
核心问题
你之前指定的是步骤行号(第9行是第二个场景的步骤),但Cucumber需要指定Scenario的起始行号才能精准运行单个场景。你的第二个Scenario起始行是第7行(即Scenario: Check login with invalid credentials所在行)。
方案1:使用旧版cucumber.options参数(兼容低版本Cucumber)
优先用/作为路径分隔符,避免Windows下转义问题:
mvn test -Dcucumber.options="src/test/resources/features/login.feature:7"
方案2:使用新版参数(Cucumber 6.x及以上版本推荐)
拆分参数更清晰,减少引号冲突:
mvn test -Dcucumber.features="src/test/resources/features/login.feature" -Dcucumber.line="7"
行号对应验证
你的feature文件行号对应如下:
Scenario: Check login with valid credentials # 行1
Given user is on login page # 行2
When user enters valid username and password # 行3
And clicks on login button # 行4
Then user is navigated to the home page # 行5
# 行6
Scenario: Check login with invalid credentials # 行7
Given user is on login page # 行8
When user enters invalid username and password # 行9
And clicks on login button # 行10
Then error message is displayed - Invalid Credentials # 行11
内容的提问来源于stack exchange,提问作者Tharun S M

