Skip to content

Commit 9f739da

Browse files
committed
Introduce title path
1 parent 3997a59 commit 9f739da

9 files changed

Lines changed: 111 additions & 5 deletions

File tree

allure-cucumber/lib/allure_cucumber/models/cucumber_model.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def test_result(test_case)
2626
description_html: scenario.description,
2727
history_id: scenario.id,
2828
full_name: scenario.name,
29+
title_path: scenario.title_path,
2930
labels: parser.labels,
3031
links: parser.links,
3132
parameters: parser.parameters,

allure-cucumber/lib/allure_cucumber/models/scenario.rb

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ def feature_name
2525
@feature_name ||= feature.name
2626
end
2727

28+
# Title path without the final scenario display name.
29+
# @return [Array<String>]
30+
def title_path
31+
[feature_path, feature_name, rule_name].compact
32+
end
33+
2834
# Scenario name
2935
# @return [String]
3036
def name
@@ -52,19 +58,60 @@ def tags
5258
# Feature file name
5359
# @return [String]
5460
def feature_file_name
55-
@feature_file_name ||= test_case.location.file.split("/").last.gsub(".feature", "")
61+
@feature_file_name ||= File.basename(location_file, ".feature")
5662
end
5763

5864
# Feature folder
5965
# @return [String]
6066
def feature_folder
61-
@feature_folder ||= test_case.location.file.split("/")[-2]
67+
@feature_folder ||= begin
68+
directory = File.dirname(location_file)
69+
directory == "." ? nil : File.basename(directory)
70+
end
6271
end
6372

6473
private
6574

6675
attr_reader :test_case, :scenario_source, :feature
6776

77+
# @return [String]
78+
def feature_path
79+
@feature_path ||= location_file.delete_prefix("./")
80+
end
81+
82+
# @return [String]
83+
def location_file
84+
@location_file ||= test_case.location.file
85+
end
86+
87+
# @return [String, nil]
88+
def rule_name
89+
@rule_name ||= rule&.name
90+
end
91+
92+
# @return [Object, nil]
93+
def rule
94+
return unless scenario_id
95+
96+
@rule ||= Array(feature.children)
97+
.filter_map { |child| child.rule if child.respond_to?(:rule) }
98+
.find { |child_rule| rule_scenario_ids(child_rule).include?(scenario_id) }
99+
end
100+
101+
# @return [Array<String>]
102+
def rule_scenario_ids(rule)
103+
Array(rule.children).filter_map do |child|
104+
next unless child.respond_to?(:scenario)
105+
106+
child.scenario&.id
107+
end
108+
end
109+
110+
# @return [String, nil]
111+
def scenario_id
112+
@scenario_id ||= scenario.respond_to?(:id) ? scenario.id : nil
113+
end
114+
68115
# Is scenario outline
69116
# @return [Boolean]
70117
def scenario_outline?

allure-cucumber/spec/unit/formatter_test_case_started_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
expect(arg.name).to eq(scenario)
5757
expect(arg.description).to eq("Simple scenario description")
5858
expect(arg.full_name).to eq(scenario)
59+
expect(arg.title_path).to eq(["#{test_tmp_dir}/features/test.feature", feature])
5960
expect(arg.links).to be_empty
6061
expect(arg.parameters).to be_empty
6162
expect(arg.labels).to match_array(labels)
@@ -223,4 +224,20 @@
223224
end
224225
end
225226
end
227+
228+
context "feature hierarchy" do
229+
it "adds title path for rules" do
230+
run_cucumber_cli(<<~FEATURE)
231+
Feature: #{feature}
232+
233+
Rule: Core scenarios
234+
Scenario: #{scenario}
235+
Given a is 5
236+
FEATURE
237+
238+
expect(lifecycle).to have_received(:start_test_case).once do |arg|
239+
expect(arg.title_path).to eq(["#{test_tmp_dir}/features/test.feature", feature, "Core scenarios"])
240+
end
241+
end
242+
end
226243
end

allure-rspec/lib/allure_rspec/formatter.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ def test_result(example)
108108
description_html: "Location - #{strip_relative(parser.location)}",
109109
history_id: example.id,
110110
full_name: example.full_description,
111+
title_path: parser.title_path,
111112
labels: parser.labels,
112113
links: parser.links,
113114
status_details: parser.status_details,

allure-rspec/lib/allure_rspec/metadata_parser.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ def status_details
6767
)
6868
end
6969

70+
# Title path without the final example display name.
71+
# @return [Array<String>]
72+
def title_path
73+
[strip_relative(example.file_path), *SuiteLabels.new(example.example_group).title_path]
74+
end
75+
7076
# Example location
7177
#
7278
# @return [String]

allure-rspec/lib/allure_rspec/suite_labels.rb

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ def initialize(example_group)
1313
# Get test suite labels
1414
# @return [Array<Allure::Label>]
1515
def fetch
16-
parents = example_group.parent_groups.map do |group|
17-
group.description.empty? ? "Anonymous" : group.description
18-
end
16+
parents = parent_groups
1917

2018
labels = []
2119
labels << Allure::ResultUtils.suite_label(suite(parents))
@@ -25,10 +23,23 @@ def fetch
2523
labels
2624
end
2725

26+
# Get title path from outermost group to innermost group.
27+
# @return [Array<String>]
28+
def title_path
29+
parent_groups.reverse
30+
end
31+
2832
private
2933

3034
attr_reader :example_group
3135

36+
# @return [Array<String>]
37+
def parent_groups
38+
@parent_groups ||= example_group.parent_groups.map do |group|
39+
group.description.empty? ? "Anonymous" : group.description
40+
end
41+
end
42+
3243
# @param [Array<String>] parents
3344
# @return [String]
3445
def suite(parents)

allure-rspec/spec/unit/formatter_example_started_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
expect(arg.name).to eq(spec)
4545
expect(arg.description).to eq("Location - #{test_tmp_dir}/spec/test_spec.rb:10")
4646
expect(arg.full_name).to eq("#{suite} #{spec}")
47+
expect(arg.title_path).to eq(["#{test_tmp_dir}/spec/test_spec.rb", suite])
4748
expect(arg.links).to be_empty
4849
expect(arg.parameters).to be_empty
4950
expect(arg.history_id).to eq(Digest::MD5.hexdigest("./#{test_tmp_dir}/spec/test_spec.rb[1:1]"))
@@ -256,15 +257,20 @@
256257

257258
aggregate_failures "Examples should contain correct suite labels" do
258259
expect(examples.first.labels).to include(Allure::ResultUtils.suite_label("Suite"))
260+
expect(examples.first.title_path).to eq(["#{test_tmp_dir}/spec/test_spec.rb", "Suite"])
259261
expect(examples[1].labels).to include(
260262
Allure::ResultUtils.suite_label("Nested Suite 1"),
261263
Allure::ResultUtils.parent_suite_label("Suite")
262264
)
265+
expect(examples[1].title_path).to eq(["#{test_tmp_dir}/spec/test_spec.rb", "Suite", "Nested Suite 1"])
263266
expect(examples.last.labels).to include(
264267
Allure::ResultUtils.suite_label("Nested Suite 2"),
265268
Allure::ResultUtils.parent_suite_label("Suite"),
266269
Allure::ResultUtils.sub_suite_label("Nested Suite 2:1:1 > Nested Suite 2:1")
267270
)
271+
expect(examples.last.title_path).to eq(
272+
["#{test_tmp_dir}/spec/test_spec.rb", "Suite", "Nested Suite 2", "Nested Suite 2:1", "Nested Suite 2:1:1"]
273+
)
268274
end
269275
end
270276
end

allure-ruby-commons/lib/allure_ruby_commons/model/test_result.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class TestResult < ExecutableItem
99
# @param [Hash] options
1010
# @option options [String] :name
1111
# @option options [String] :full_name
12+
# @option options [Array<String>] :title_path
1213
# @option options [String] :description
1314
# @option options [String] :description_html
1415
# @option options [String] :status ('broken')
@@ -26,13 +27,15 @@ def initialize(uuid: SecureRandom.uuid, history_id: SecureRandom.uuid, environme
2627
@uuid = uuid
2728
@history_id = history_id
2829
@full_name = options[:full_name] || "Unnamed"
30+
@title_path = options[:title_path]
2931
@labels = options[:labels] || []
3032
@links = options[:links] || []
3133
@parameters << Parameter.new("environment", environment) if environment
3234
end
3335

3436
attr_accessor :uuid,
3537
:full_name,
38+
:title_path,
3639
:labels,
3740
:links
3841

allure-ruby-commons/spec/unit/test_result_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,20 @@
8080
end
8181
end
8282

83+
context "title path" do
84+
let(:environment) { nil }
85+
let(:title_path) { ["spec/test_spec.rb", "Suite", "Nested Suite"] }
86+
let!(:test_case) { start_test_case(name: "Test Case", title_path: title_path, environment: environment) }
87+
88+
it "stores title path on test results" do
89+
expect(test_case.title_path).to eq(title_path)
90+
end
91+
92+
it "serializes title path as titlePath" do
93+
expect(test_case.to_hash["titlePath"]).to eq(title_path)
94+
end
95+
end
96+
8397
context "with allure environment" do
8498
let(:environment) { "test" }
8599

0 commit comments

Comments
 (0)