您無法直接從步驟定義中訪問該信息。如果您需要這些信息,您必須在掛鉤前捕捉它。
黃瓜V3 +
鉤子將捕獲功能名稱,場景/提綱名稱和標籤的列表之前以下。請注意,此解決方案適用於Cucumber v3.0 +。對於早期版本,請參閱答案的結尾。
Before do |scenario|
# Feature name
@feature_name = scenario.feature.name
# Scenario name
@scenario_name = scenario.name
# Tags (as an array)
@scenario_tags = scenario.source_tag_names
end
舉例來說,該功能文件:
@feature_tag
Feature: Feature description
@regular_scenario_tag
Scenario: Scenario description
Given scenario details
@outline_tag
Scenario Outline: Outline description
Given scenario details
Examples:
|num_1 | num_2 | result |
| 1 | 1 | 2 |
對於步驟定義爲:
Given /scenario details/ do
p @feature_name
p @scenario_name
p @scenario_tags
end
會給結果:
"Feature description"
"Scenario description"
["@feature_tag", "@regular_scenario_tag"]
"Feature description"
"Outline description, Examples (#1)"
["@feature_tag", "@outline_tag"]
然後,您可以檢查@scenario_name或@scenario_tags爲你的條件邏輯。
黃瓜V2
黃瓜V2,所需的掛鉤是一個比較複雜:
Before do |scenario|
# Feature name
case scenario
when Cucumber::Ast::Scenario
@feature_name = scenario.feature.name
when Cucumber::Ast::OutlineTable::ExampleRow
@feature_name = scenario.scenario_outline.feature.name
end
# Scenario name
case scenario
when Cucumber::Ast::Scenario
@scenario_name = scenario.name
when Cucumber::Ast::OutlineTable::ExampleRow
@scenario_name = scenario.scenario_outline.name
end
# Tags (as an array)
@scenario_tags = scenario.source_tag_names
end
輸出略有不同:
"Feature description"
"Scenario description"
["@regular_scenario_tag", "@feature_tag"]
"Feature description"
"Outline description"
["@outline_tag", "@feature_tag"]
尼斯問題,謝謝大家 –