Skip to content

Commit

Permalink
Merge pull request #101 from DataDog/anmarchenko/parametrized_rspec_t…
Browse files Browse the repository at this point in the history
…ests

[CIVIS-7722] Deduplicate dynamically generated RSpec examples using metadata.scoped_id
  • Loading branch information
anmarchenko committed Jan 15, 2024
2 parents a94f9de + 76f2a35 commit c56656c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
2 changes: 2 additions & 0 deletions lib/datadog/ci/contrib/rspec/example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ def run(example_group_instance, reporter)
},
service: configuration[:service_name]
) do |test_span|
test_span.set_parameters({}, {"scoped_id" => metadata[:scoped_id]})

result = super

case execution_result.status
Expand Down
2 changes: 1 addition & 1 deletion lib/datadog/ci/span.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def set_default_tags
# @param [Hash] metadata optional metadata
# @return [void]
def set_parameters(arguments, metadata = {})
return if arguments.nil? || arguments.empty?
return if arguments.nil?

set_tag(
Ext::Test::TAG_PARAMETERS,
Expand Down
15 changes: 12 additions & 3 deletions spec/datadog/ci/contrib/rspec/instrumentation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,8 @@ def rspec_session_run(with_failed_test: false, with_shared_test: false)

if with_shared_test
require_relative "some_shared_examples"
include_examples "Testing shared examples"
include_examples "Testing shared examples", 2
include_examples "Testing shared examples", 1
end
end

Expand Down Expand Up @@ -371,8 +372,16 @@ def rspec_session_run(with_failed_test: false, with_shared_test: false)
let!(:spec) { rspec_session_run(with_shared_test: true) }

it "creates correct test spans connects all tests to a single test suite" do
shared_test_span = test_spans.find { |test_span| test_span.name == "SomeTest shared examples adds 1 and 1" }
expect(shared_test_span.get_tag(Datadog::CI::Ext::Test::TAG_SUITE)).to eq(spec.file_path)
shared_test_spans = test_spans.filter { |test_span| test_span.name == "SomeTest shared examples adds 1 and 1" }
expect(shared_test_spans).to have(2).items

shared_test_spans.each_with_index do |shared_test_span, index|
expect(shared_test_span.get_tag(Datadog::CI::Ext::Test::TAG_SUITE)).to eq(spec.file_path)

expect(shared_test_span.get_tag(Datadog::CI::Ext::Test::TAG_PARAMETERS)).to eq(
"{\"arguments\":{},\"metadata\":{\"scoped_id\":\"1:#{2 + index}:1\"}}"
)
end

test_spans.each do |test_span|
expect(test_span.get_tag(Datadog::CI::Ext::Test::TAG_TEST_SUITE_ID)).to eq(test_suite_span.id.to_s)
Expand Down
4 changes: 2 additions & 2 deletions spec/datadog/ci/contrib/rspec/some_shared_examples.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
RSpec.shared_examples "Testing shared examples" do
RSpec.shared_examples "Testing shared examples" do |expected_result|
context "shared examples" do
it "adds 1 and 1" do
expect(1 + 1).to eq(2)
expect(1 + 1).to eq(expected_result)
end
end
end

0 comments on commit c56656c

Please sign in to comment.