Skip to content

Commit

Permalink
add test case where mixin generates several tests dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Mar 11, 2024
1 parent 09c647b commit c637993
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 26 deletions.
86 changes: 60 additions & 26 deletions spec/datadog/ci/contrib/activesupport/instrumentation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,67 @@
let(:integration_options) { {service_name: "ltest"} }
end

before do
Minitest::Runnable.reset
require_relative "test/test_calculator"
Minitest.run([])
context "when mixin used" do
before do
Minitest::Runnable.reset
require_relative "test/test_calculator"
Minitest.run([])
end

it "instruments this minitest session" do
# test session and module traced
expect(test_session_span).not_to be_nil
expect(test_module_span).not_to be_nil

expect([test_session_span, test_module_span]).to all have_pass_status

expect(test_suite_spans).to have(1).items
expect(test_suite_spans).to have_tag_values_no_order(:status, ["pass"])
expect(test_suite_spans).to have_tag_values_no_order(
:suite,
[
"CalculatorTest at spec/datadog/ci/contrib/activesupport/test/test_calculator.rb"
]
)

# there is test span for every test case
expect(test_spans).to have(3).items
# each test span has test suite, module, and session
expect(test_spans).to all have_test_tag(:test_suite_id)
expect(test_spans).to all have_test_tag(:test_module_id)
expect(test_spans).to all have_test_tag(:test_session_id)
end
end

it "instruments this minitest session" do
# test session and module traced
expect(test_session_span).not_to be_nil
expect(test_module_span).not_to be_nil

expect([test_session_span, test_module_span]).to all have_pass_status

expect(test_suite_spans).to have(1).items
expect(test_suite_spans).to have_tag_values_no_order(:status, ["pass"])
expect(test_suite_spans).to have_tag_values_no_order(
:suite,
[
"CalculatorTest at spec/datadog/ci/contrib/activesupport/test/test_calculator.rb"
]
)

# there is test span for every test case
expect(test_spans).to have(3).items
# each test span has test suite, module, and session
expect(test_spans).to all have_test_tag(:test_suite_id)
expect(test_spans).to all have_test_tag(:test_module_id)
expect(test_spans).to all have_test_tag(:test_session_id)
context "when mixin creates methods dynamically" do
before do
Minitest::Runnable.reset
require_relative "test/test_calculator_generated"
Minitest.run([])
end

it "instruments this minitest session" do
# test session and module traced
expect(test_session_span).not_to be_nil
expect(test_module_span).not_to be_nil

expect([test_session_span, test_module_span]).to all have_pass_status

expect(test_suite_spans).to have(1).items
expect(test_suite_spans).to have_tag_values_no_order(:status, ["pass"])
expect(test_suite_spans).to have_tag_values_no_order(
:suite,
[
"CalculatorGeneratedTest at spec/datadog/ci/contrib/activesupport/test/test_calculator_generated.rb"
]
)

# there is test span for every test case
expect(test_spans).to have(5).items
# each test span has test suite, module, and session
expect(test_spans).to all have_test_tag(:test_suite_id)
expect(test_spans).to all have_test_tag(:test_module_id)
expect(test_spans).to all have_test_tag(:test_session_id)
end
end
end
13 changes: 13 additions & 0 deletions spec/datadog/ci/contrib/activesupport/test/generator_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module GeneratorHelper
extend ActiveSupport::Concern

class_methods do
def test_operations(*operations)
operations.each do |operation|
test "performs #{operation}" do
assert Calculator.new.public_send(operation, 1, 2)
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require "active_support"
require "minitest/spec"

require_relative "calculator"
require_relative "generator_helper"

class CalculatorGeneratedTest < ActiveSupport::TestCase
include GeneratorHelper

test_operations(:add, :subtract, :multiply, :divide)

test "adds two numbers" do
assert Calculator.new.add(1, 2) == 3
end
end

0 comments on commit c637993

Please sign in to comment.