Skip to content

Commit

Permalink
Merge pull request #105 from DataDog/anmarchenko/readable_test_suite_…
Browse files Browse the repository at this point in the history
…names

[CIVIS-8548] make test suite names more readable
  • Loading branch information
anmarchenko committed Jan 16, 2024
2 parents eb4dfad + 00d39f0 commit 8dd092a
Show file tree
Hide file tree
Showing 15 changed files with 115 additions and 65 deletions.
17 changes: 16 additions & 1 deletion lib/datadog/ci/contrib/cucumber/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def on_test_run_finished(event)
end

def on_test_case_started(event)
test_suite_name = event.test_case.location.file
test_suite_name = test_suite_name(event.test_case)

tags = {
CI::Ext::Test::TAG_FRAMEWORK => Ext::FRAMEWORK,
Expand Down Expand Up @@ -108,6 +108,21 @@ def on_test_step_finished(event)

private

def test_suite_name(test_case)
feature = if test_case.respond_to?(:feature)
test_case.feature
elsif @ast_lookup
gherkin_doc = @ast_lookup.gherkin_document(test_case.location.file)
gherkin_doc.feature if gherkin_doc
end

if feature
"#{feature.name} at #{test_case.location.file}"
else
test_case.location.file
end
end

def finish_test(span, result)
if result.skipped?
span.skipped!
Expand Down
4 changes: 1 addition & 3 deletions lib/datadog/ci/contrib/minitest/hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ def before_setup
super
return unless datadog_configuration[:enabled]

test_name = "#{class_name}##{name}"

test_suite_name = Helpers.test_suite_name(self.class, name)
if Helpers.parallel?(self.class)
test_suite_name = "#{test_suite_name} (#{name} concurrently)"
Expand All @@ -27,7 +25,7 @@ def before_setup
source_file, line_number = method(name).source_location

CI.start_test(
test_name,
name,
test_suite_name,
tags: {
CI::Ext::Test::TAG_FRAMEWORK => Ext::FRAMEWORK,
Expand Down
27 changes: 21 additions & 6 deletions lib/datadog/ci/contrib/rspec/example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,33 @@ def self.included(base)
base.prepend(InstanceMethods)
end

# Instance methods for configuration
module InstanceMethods
def run(example_group_instance, reporter)
return super unless configuration[:enabled]
def run(*)
return super unless datadog_configuration[:enabled]

test_name = full_description.strip
if metadata[:description].empty?
# for unnamed it blocks this appends something like "example at ./spec/some_spec.rb:10"
test_name += " #{description}"
end

test_suite_description = fetch_top_level_example_group[:description]
suite_name = "#{test_suite_description} at #{metadata[:example_group][:rerun_file_path]}"

# remove suite name from test name to avoid duplication
test_name = test_name.sub(test_suite_description, "").strip

CI.trace_test(
test_name,
metadata[:example_group][:rerun_file_path],
suite_name,
tags: {
CI::Ext::Test::TAG_FRAMEWORK => Ext::FRAMEWORK,
CI::Ext::Test::TAG_FRAMEWORK_VERSION => CI::Contrib::RSpec::Integration.version.to_s,
CI::Ext::Test::TAG_TYPE => CI::Ext::Test::TEST_TYPE,
CI::Ext::Test::TAG_SOURCE_FILE => Utils::Git.relative_to_root(metadata[:file_path]),
CI::Ext::Test::TAG_SOURCE_START => metadata[:line_number].to_s
},
service: configuration[:service_name]
service: datadog_configuration[:service_name]
) do |test_span|
test_span.set_parameters({}, {"scoped_id" => metadata[:scoped_id]})

Expand All @@ -56,7 +61,17 @@ def run(example_group_instance, reporter)

private

def configuration
def fetch_top_level_example_group
return metadata[:example_group] unless metadata[:example_group][:parent_example_group]

res = metadata[:example_group][:parent_example_group]
while (parent = res[:parent_example_group])
res = parent
end
res
end

def datadog_configuration
Datadog.configuration.ci[:rspec]
end
end
Expand Down
11 changes: 6 additions & 5 deletions lib/datadog/ci/contrib/rspec/example_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@ module Datadog
module CI
module Contrib
module RSpec
# Instrument RSpec::Core::Example
# Instrument RSpec::Core::ExampleGroup
module ExampleGroup
def self.included(base)
base.singleton_class.prepend(ClassMethods)
end

# Instance methods for configuration
module ClassMethods
def run(reporter = ::RSpec::Core::NullReporter)
return super unless configuration[:enabled]
def run(*)
return super unless datadog_configuration[:enabled]
return super unless top_level?

test_suite = Datadog::CI.start_test_suite(file_path)
suite_name = "#{description} at #{file_path}"
test_suite = Datadog::CI.start_test_suite(suite_name)

result = super

Expand All @@ -35,7 +36,7 @@ def run(reporter = ::RSpec::Core::NullReporter)

private

def configuration
def datadog_configuration
Datadog.configuration.ci[:rspec]
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/datadog/ci/contrib/rspec/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ def self.included(base)
end

module InstanceMethods
def run_specs(example_groups)
return super unless configuration[:enabled]
def run_specs(*)
return super unless datadog_configuration[:enabled]

test_session = CI.start_test_session(
tags: {
CI::Ext::Test::TAG_FRAMEWORK => Ext::FRAMEWORK,
CI::Ext::Test::TAG_FRAMEWORK_VERSION => CI::Contrib::RSpec::Integration.version.to_s,
CI::Ext::Test::TAG_TYPE => CI::Ext::Test::TEST_TYPE
},
service: configuration[:service_name]
service: datadog_configuration[:service_name]
)

test_module = CI.start_test_module(test_session.name)
Expand All @@ -46,7 +46,7 @@ def run_specs(example_groups)

private

def configuration
def datadog_configuration
Datadog.configuration.ci[:rspec]
end
end
Expand Down
2 changes: 2 additions & 0 deletions sig/datadog/ci/contrib/cucumber/formatter.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ module Datadog

private

def test_suite_name: (untyped test_case) -> String

def start_test_suite: (String test_suite_name) -> void

def finish_current_test_suite: () -> void
Expand Down
3 changes: 2 additions & 1 deletion sig/datadog/ci/contrib/rspec/example.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ module Datadog

private

def configuration: () -> untyped
def fetch_top_level_example_group: () -> Hash[Symbol, untyped]
def datadog_configuration: () -> untyped
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion sig/datadog/ci/contrib/rspec/example_group.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module Datadog

private

def configuration: () -> untyped
def datadog_configuration: () -> untyped
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion sig/datadog/ci/contrib/rspec/runner.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module Datadog

private

def configuration: () -> untyped
def datadog_configuration: () -> untyped
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/datadog/ci/contrib/cucumber/features/failing.feature
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Feature: Datadog integration
Feature: Datadog integration - test failing features
Scenario: cucumber failing scenario
Given datadog
And datadog
Expand Down
10 changes: 6 additions & 4 deletions spec/datadog/ci/contrib/cucumber/instrumentation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
expect(scenario_span.get_tag(Datadog::CI::Ext::Test::TAG_SPAN_KIND)).to eq(Datadog::CI::Ext::AppTypes::TYPE_TEST)
expect(scenario_span.get_tag(Datadog::CI::Ext::Test::TAG_NAME)).to eq("cucumber scenario")
expect(scenario_span.get_tag(Datadog::CI::Ext::Test::TAG_SUITE)).to eq(
"spec/datadog/ci/contrib/cucumber/features/passing.feature"
"Datadog integration at spec/datadog/ci/contrib/cucumber/features/passing.feature"
)
expect(scenario_span.get_tag(Datadog::CI::Ext::Test::TAG_TYPE)).to eq(Datadog::CI::Ext::Test::TEST_TYPE)
expect(scenario_span.get_tag(Datadog::CI::Ext::Test::TAG_FRAMEWORK)).to eq(
Expand Down Expand Up @@ -147,7 +147,7 @@

it "creates test suite span" do
expect(test_suite_span).not_to be_nil
expect(test_suite_span.name).to eq(features_path)
expect(test_suite_span.name).to eq("Datadog integration at spec/datadog/ci/contrib/cucumber/features/passing.feature")
expect(test_suite_span.service).to eq("jalapenos")
expect(test_suite_span.get_tag(Datadog::CI::Ext::Test::TAG_SPAN_KIND)).to eq(
Datadog::CI::Ext::AppTypes::TYPE_TEST
Expand Down Expand Up @@ -189,7 +189,9 @@
Datadog::CI::Ext::Test::Status::FAIL
)

expect(test_suite_span.name).to eq(features_path)
expect(test_suite_span.name).to eq(
"Datadog integration - test failing features at spec/datadog/ci/contrib/cucumber/features/failing.feature"
)
expect(test_suite_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq(
Datadog::CI::Ext::Test::Status::FAIL
)
Expand Down Expand Up @@ -224,7 +226,7 @@
)
end
expect(span.get_tag(Datadog::CI::Ext::Test::TAG_SUITE)).to eq(
"spec/datadog/ci/contrib/cucumber/features/with_parameters.feature"
"Datadog integration for parametrized tests at spec/datadog/ci/contrib/cucumber/features/with_parameters.feature"
)
expect(span.get_tag(Datadog::CI::Ext::Test::TAG_TEST_SUITE_ID)).to eq(test_suite_span.id.to_s)
expect(span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq(
Expand Down
28 changes: 14 additions & 14 deletions spec/datadog/ci/contrib/minitest/instrumentation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ def test_foo
klass.new(:test_foo).run

expect(span.type).to eq(Datadog::CI::Ext::AppTypes::TYPE_TEST)
expect(span.name).to eq("SomeTest#test_foo")
expect(span.resource).to eq("SomeTest#test_foo")
expect(span.name).to eq("test_foo")
expect(span.resource).to eq("test_foo")
expect(span.service).to eq("ltest")
expect(span.get_tag(Datadog::CI::Ext::Test::TAG_NAME)).to eq("SomeTest#test_foo")
expect(span.get_tag(Datadog::CI::Ext::Test::TAG_NAME)).to eq("test_foo")
expect(span.get_tag(Datadog::CI::Ext::Test::TAG_SUITE)).to eq(
"SomeTest at spec/datadog/ci/contrib/minitest/instrumentation_spec.rb"
)
Expand Down Expand Up @@ -110,9 +110,9 @@ def self.name
klass.new(method_name).run

expect(span.type).to eq(Datadog::CI::Ext::AppTypes::TYPE_TEST)
expect(span.resource).to eq("SomeSpec##{method_name}")
expect(span.resource).to eq(method_name)
expect(span.service).to eq("ltest")
expect(span.get_tag(Datadog::CI::Ext::Test::TAG_NAME)).to eq("SomeSpec##{method_name}")
expect(span.get_tag(Datadog::CI::Ext::Test::TAG_NAME)).to eq(method_name)
expect(span.get_tag(Datadog::CI::Ext::Test::TAG_SUITE)).to eq(
"SomeSpec at spec/datadog/ci/contrib/minitest/instrumentation_spec.rb"
)
Expand Down Expand Up @@ -514,7 +514,7 @@ def test_fail
end

it "traces test, test session, test module with failed status" do
expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_NAME)).to eq("SomeFailedTest#test_fail")
expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_NAME)).to eq("test_fail")
expect(first_test_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq(
Datadog::CI::Ext::Test::Status::FAIL
)
Expand Down Expand Up @@ -561,10 +561,10 @@ class SomeSpec < Minitest::Spec

expect(test_names).to eq(
[
"SomeSpec#test_0001_does not fail",
"in context#test_0001_does not fail",
"in context::deeper context#test_0001_does not fail",
"in other context#test_0001_does not fail"
"test_0001_does not fail",
"test_0001_does not fail",
"test_0001_does not fail",
"test_0001_does not fail"
]
)
end
Expand Down Expand Up @@ -635,10 +635,10 @@ def test_b_2
test_names = test_spans.map { |span| span.get_tag(Datadog::CI::Ext::Test::TAG_NAME) }.sort
expect(test_names).to eq(
[
"TestA#test_a_1",
"TestA#test_a_2",
"TestB#test_b_1",
"TestB#test_b_2"
"test_a_1",
"test_a_2",
"test_b_1",
"test_b_2"
]
)

Expand Down
Loading

0 comments on commit 8dd092a

Please sign in to comment.