From 7b421abd54688cc281a25a7ae0a1d9b7e50f28cb Mon Sep 17 00:00:00 2001 From: Andrejs Date: Sun, 29 Mar 2020 15:37:06 +0300 Subject: [PATCH] Refactor allure-rspec tests (#103) --- allure-rspec/.rspec | 2 + .../spec/fixture/specs/exception_test.rb | 11 --- .../spec/fixture/specs/nested_test.rb | 23 ----- .../spec/fixture/specs/simple_test.rb | 15 ---- allure-rspec/spec/fixture/specs/step_test.rb | 8 -- allure-rspec/spec/fixture/specs/tag_test.rb | 10 --- .../spec/integration/full_report_spec.rb | 26 ++++-- allure-rspec/spec/rspec_runner_helper.rb | 50 +++++++++++ allure-rspec/spec/spec_helper.rb | 13 +-- .../spec/unit/formatter_add_step_spec.rb | 11 ++- .../unit/formatter_example_finished_spec.rb | 34 ++++++-- .../formatter_example_group_finished_spec.rb | 10 ++- .../formatter_example_group_started_spec.rb | 10 ++- .../unit/formatter_example_started_spec.rb | 86 ++++++++++++++++--- .../spec/unit/formatter_start_spec.rb | 12 ++- 15 files changed, 217 insertions(+), 104 deletions(-) create mode 100644 allure-rspec/.rspec delete mode 100644 allure-rspec/spec/fixture/specs/exception_test.rb delete mode 100644 allure-rspec/spec/fixture/specs/nested_test.rb delete mode 100644 allure-rspec/spec/fixture/specs/simple_test.rb delete mode 100644 allure-rspec/spec/fixture/specs/step_test.rb delete mode 100644 allure-rspec/spec/fixture/specs/tag_test.rb create mode 100644 allure-rspec/spec/rspec_runner_helper.rb diff --git a/allure-rspec/.rspec b/allure-rspec/.rspec new file mode 100644 index 0000000..f7ec799 --- /dev/null +++ b/allure-rspec/.rspec @@ -0,0 +1,2 @@ +--format documentation +--require spec_helper diff --git a/allure-rspec/spec/fixture/specs/exception_test.rb b/allure-rspec/spec/fixture/specs/exception_test.rb deleted file mode 100644 index 468e83e..0000000 --- a/allure-rspec/spec/fixture/specs/exception_test.rb +++ /dev/null @@ -1,11 +0,0 @@ -# frozen_string_literal: true - -describe "Suite" do - it "failed expectation", failed: true do - expect(1).to eq(2) - end - - it "broken expectation", broken: true do - raise Exception.new("Simple error!") - end -end diff --git a/allure-rspec/spec/fixture/specs/nested_test.rb b/allure-rspec/spec/fixture/specs/nested_test.rb deleted file mode 100644 index 5a9cda2..0000000 --- a/allure-rspec/spec/fixture/specs/nested_test.rb +++ /dev/null @@ -1,23 +0,0 @@ -# frozen_string_literal: true - -describe "Suite" do - describe "Nested Suite 1" do - it "Spec 1 - 1" do - end - end - describe "Nested Suite 2" do - it "Spec 2 - 1" do - end - it "Spec 2 - 2" do - end - describe "Nested Suite 2:1" do - it "Spec 2:1 - 1" do - end - describe "Nested Suite 2:1:1" do - it "Spec 2:1:1 - 1" - end - end - end - it "Spec" do - end -end diff --git a/allure-rspec/spec/fixture/specs/simple_test.rb b/allure-rspec/spec/fixture/specs/simple_test.rb deleted file mode 100644 index 48c5f51..0000000 --- a/allure-rspec/spec/fixture/specs/simple_test.rb +++ /dev/null @@ -1,15 +0,0 @@ -# frozen_string_literal: true - -describe "Suite" do - before(:each) do |e| - e.step(name: "Before hook") - end - - after(:each) do |e| - e.step(name: "After hook") - end - - it "spec", allure: "some_label" do |e| - e.step(name: "test body") - end -end diff --git a/allure-rspec/spec/fixture/specs/step_test.rb b/allure-rspec/spec/fixture/specs/step_test.rb deleted file mode 100644 index 3104bca..0000000 --- a/allure-rspec/spec/fixture/specs/step_test.rb +++ /dev/null @@ -1,8 +0,0 @@ -# frozen_string_literal: true - -describe "Suite" do - it "spec" do |example| - example.run_step("custom step") do - end - end -end diff --git a/allure-rspec/spec/fixture/specs/tag_test.rb b/allure-rspec/spec/fixture/specs/tag_test.rb deleted file mode 100644 index 7f5059a..0000000 --- a/allure-rspec/spec/fixture/specs/tag_test.rb +++ /dev/null @@ -1,10 +0,0 @@ -# frozen_string_literal: true - -describe "Suite" do - it( - "spec", - tms: "QA-123", tms_2: "QA-124", issue: "BUG-123", issue_2: "BUG-124", - flaky: true, muted: true, severity: "critical" - ) do - end -end diff --git a/allure-rspec/spec/integration/full_report_spec.rb b/allure-rspec/spec/integration/full_report_spec.rb index 2e8ceae..427b628 100644 --- a/allure-rspec/spec/integration/full_report_spec.rb +++ b/allure-rspec/spec/integration/full_report_spec.rb @@ -1,15 +1,29 @@ # frozen_string_literal: true -describe "allure-rspec" do +describe "allure rspec" do include_context "rspec runner" let(:results_dir) { Allure.configuration.results_directory } - it "Generates allure json results files", integration: true do - run_rspec("spec/fixture/specs/simple_test.rb") + it "generates allure json results files", integration: true do + run_rspec(<<~SPEC) + describe "Suite" do + before(:each) do |e| + e.step(name: "Before hook") + end - container = File.new(Dir["#{results_dir}/*container.json"].first) - result = File.new(Dir["#{results_dir}/*result.json"].first) + after(:each) do |e| + e.step(name: "After hook") + end + + it "spec", allure: "some_label" do |e| + e.step(name: "test body") + end + end + SPEC + + container = File.new(Dir["#{test_tmp_dir}/#{results_dir}/*container.json"].first) + result = File.new(Dir["#{test_tmp_dir}/#{results_dir}/*result.json"].first) aggregate_failures "Results files should exist" do expect(File.exist?(container)).to be_truthy @@ -21,7 +35,7 @@ aggregate_failures "Json results should contain valid data" do expect(container_json[:name]).to eq("Suite") expect(result_json[:name]).to eq("spec") - expect(result_json[:description]).to eq("Location - spec/fixture/specs/simple_test.rb:12") + expect(result_json[:description]).to eq("Location - #{test_tmp_dir}/spec/test_spec.rb:10") expect(result_json[:steps].size).to eq(3) end end diff --git a/allure-rspec/spec/rspec_runner_helper.rb b/allure-rspec/spec/rspec_runner_helper.rb new file mode 100644 index 0000000..94ed6a3 --- /dev/null +++ b/allure-rspec/spec/rspec_runner_helper.rb @@ -0,0 +1,50 @@ +# frozen_string_literal: true + +class RspecRunner + def initialize(tmp_dir) + @stdout = StringIO.new + @stderr = StringIO.new + @tmp_dir = tmp_dir + end + + def run(spec) + setup(spec) + + Dir.chdir(tmp_dir) do + RSpec::Core::Runner.run([spec_file, *args], @stdout, @stderr) + end + ensure + write_file("#{tmp_dir}/rspec_output.txt", all_output) + end + + private + + attr_reader :tmp_dir + + def all_output + [@stdout.string, @stderr.string].reject(&:empty?).join("\n") + end + + def setup(spec) + FileUtils.rm_rf(tmp_dir) + + write_file("#{tmp_dir}/#{spec_file}", spec) + end + + def write_file(path, content) + FileUtils.mkdir_p(File.dirname(path)) + File.open(path, "w") { |file| file.write(content) } + end + + def spec_file + "spec/test_spec.rb" + end + + def args + %w[ + --no-color + --format documentation + --format AllureRspecFormatter + ] + end +end diff --git a/allure-rspec/spec/spec_helper.rb b/allure-rspec/spec/spec_helper.rb index 944e19f..72cf750 100644 --- a/allure-rspec/spec/spec_helper.rb +++ b/allure-rspec/spec/spec_helper.rb @@ -2,9 +2,10 @@ require "simplecov" require "rspec" -require "allure-ruby-commons" require "allure-rspec" +require_relative "rspec_runner_helper" + SimpleCov.command_name("allure-rspec") AllureRspec.configure do |c| @@ -22,6 +23,9 @@ end RSpec.shared_context("rspec runner") do + let(:test_tmp_dir) { |e| "tmp/#{e.full_description.tr(' ', '_')}" } + let(:rspec_runner) { RspecRunner.new(test_tmp_dir) } + before do configuration = RSpec::Core::Configuration.new world = RSpec::Core::World.new(configuration) @@ -30,10 +34,7 @@ allow(RSpec).to receive(:world).and_return(world) end - def run_rspec(spec, tag = nil) - [spec, "--format", "AllureRspecFormatter"].tap do |args| - args.push("--tag", tag) if tag - RSpec::Core::Runner.run(args, StringIO.new, StringIO.new) - end + def run_rspec(spec) + rspec_runner.run(spec) end end diff --git a/allure-rspec/spec/unit/formatter_add_step_spec.rb b/allure-rspec/spec/unit/formatter_add_step_spec.rb index 177757f..9ab2be8 100644 --- a/allure-rspec/spec/unit/formatter_add_step_spec.rb +++ b/allure-rspec/spec/unit/formatter_add_step_spec.rb @@ -1,11 +1,18 @@ # frozen_string_literal: true -describe "RSpecFormatter.run_step" do +describe "run_step" do include_context "allure mock" include_context "rspec runner" it "runs step from example" do - run_rspec("spec/fixture/specs/step_test.rb") + run_rspec(<<~SPEC) + describe "Suite" do + it "spec" do |example| + example.run_step("custom step") do + end + end + end + SPEC aggregate_failures "Runs step" do expect(lifecycle).to have_received(:start_test_step).once do |arg| diff --git a/allure-rspec/spec/unit/formatter_example_finished_spec.rb b/allure-rspec/spec/unit/formatter_example_finished_spec.rb index fafffc0..cf53131 100644 --- a/allure-rspec/spec/unit/formatter_example_finished_spec.rb +++ b/allure-rspec/spec/unit/formatter_example_finished_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -describe "RSpecFormatter.example_finished" do +describe "example_finished" do include_context "allure mock" include_context "rspec runner" @@ -11,13 +11,25 @@ let(:result_utils) { Allure::ResultUtils } it "stops test case" do - run_rspec("spec/fixture/specs/simple_test.rb") + run_rspec(<<~SPEC) + describe "Suite" do + it "spec", allure: "some_label" do |e| + e.step(name: "test body") + end + end + SPEC expect(lifecycle).to have_received(:stop_test_case).once end it "correctly updates passed test case" do - run_rspec("spec/fixture/specs/simple_test.rb") + run_rspec(<<~SPEC) + describe "Suite" do + it "spec", allure: "some_label" do |e| + e.step(name: "test body") + end + end + SPEC expect(lifecycle).to have_received(:update_test_case).with(no_args).once do |&arg| arg.call(@test_case) @@ -30,7 +42,13 @@ end it "correctly updates failed test case" do - run_rspec("spec/fixture/specs/exception_test.rb", "failed") + run_rspec(<<~SPEC) + describe "Suite" do + it "failed expectation" do + expect(1).to eq(2) + end + end + SPEC expect(lifecycle).to have_received(:update_test_case).with(no_args) do |&arg| arg.call(@test_case) @@ -44,7 +62,13 @@ end it "correctly updates broken test case" do - run_rspec("spec/fixture/specs/exception_test.rb", "broken") + run_rspec(<<~SPEC) + describe "Suite" do + it "broken expectation" do + raise Exception.new("Simple error!") + end + end + SPEC expect(lifecycle).to have_received(:update_test_case).with(no_args) do |&arg| arg.call(@test_case) diff --git a/allure-rspec/spec/unit/formatter_example_group_finished_spec.rb b/allure-rspec/spec/unit/formatter_example_group_finished_spec.rb index 17ee244..820e6ed 100644 --- a/allure-rspec/spec/unit/formatter_example_group_finished_spec.rb +++ b/allure-rspec/spec/unit/formatter_example_group_finished_spec.rb @@ -1,11 +1,17 @@ # frozen_string_literal: true -describe "RSpecFormatter.example_group_finished" do +describe "example_group_finished" do include_context "allure mock" include_context "rspec runner" it "stops test container" do - run_rspec("spec/fixture/specs/simple_test.rb") + run_rspec(<<~SPEC) + describe "Suite" do + it "spec", allure: "some_label" do |e| + e.step(name: "test body") + end + end + SPEC expect(lifecycle).to have_received(:stop_test_container).once end diff --git a/allure-rspec/spec/unit/formatter_example_group_started_spec.rb b/allure-rspec/spec/unit/formatter_example_group_started_spec.rb index b6e8734..e4f35bd 100644 --- a/allure-rspec/spec/unit/formatter_example_group_started_spec.rb +++ b/allure-rspec/spec/unit/formatter_example_group_started_spec.rb @@ -1,11 +1,17 @@ # frozen_string_literal: true -describe "RSpecFormatter.example_group_started" do +describe "example_group_started" do include_context "allure mock" include_context "rspec runner" it "starts test container with correct arguments" do - run_rspec("spec/fixture/specs/simple_test.rb") + run_rspec(<<~SPEC) + describe "Suite" do + it "spec", allure: "some_label" do |e| + e.step(name: "test body") + end + end + SPEC expect(lifecycle).to have_received(:start_test_container).once do |arg| expect(arg.name).to eq("Suite") diff --git a/allure-rspec/spec/unit/formatter_example_started_spec.rb b/allure-rspec/spec/unit/formatter_example_started_spec.rb index 6715e1f..d6e3bc6 100644 --- a/allure-rspec/spec/unit/formatter_example_started_spec.rb +++ b/allure-rspec/spec/unit/formatter_example_started_spec.rb @@ -1,30 +1,45 @@ # frozen_string_literal: true -describe "RSpecFormatter.example_started" do +describe "example_started" do include_context "allure mock" include_context "rspec runner" let(:result_utils) { Allure::ResultUtils } it "starts test case with correct default arguments" do - run_rspec("spec/fixture/specs/simple_test.rb") - suite = "Suite" spec = "spec" + + run_rspec(<<~SPEC) + describe "#{suite}" do + before(:each) do |e| + e.step(name: "Before hook") + end + + after(:each) do |e| + e.step(name: "After hook") + end + + it "#{spec}", allure: "some_label" do |e| + e.step(name: "test body") + end + end + SPEC + expect(lifecycle).to have_received(:start_test_case).once do |arg| aggregate_failures "Should have correct args" do expect(arg.name).to eq(spec) - expect(arg.description).to eq("Location - spec/fixture/specs/simple_test.rb:12") + expect(arg.description).to eq("Location - #{test_tmp_dir}/spec/test_spec.rb:10") expect(arg.full_name).to eq("#{suite} #{spec}") expect(arg.links).to be_empty expect(arg.parameters).to be_empty - expect(arg.history_id).to eq(Digest::MD5.hexdigest("./spec/fixture/specs/simple_test.rb[1:1]")) + expect(arg.history_id).to eq(Digest::MD5.hexdigest("./#{test_tmp_dir}/spec/test_spec.rb[1:1]")) expect(arg.labels).to include( result_utils.feature_label(suite), result_utils.story_label(spec), result_utils.framework_label("rspec"), - result_utils.package_label("spec/fixture/specs"), - result_utils.test_class_label("simple_test"), + result_utils.package_label("#{test_tmp_dir}/spec"), + result_utils.test_class_label("test_spec"), result_utils.tag_label("some_label"), ) end @@ -32,7 +47,16 @@ end it "creates issue and tms links" do - run_rspec("spec/fixture/specs/tag_test.rb") + run_rspec(<<~SPEC) + describe "Suite" do + it( + "spec", + tms: "QA-123", tms_2: "QA-124", issue: "BUG-123", issue_2: "BUG-124", + flaky: true, muted: true, severity: "critical" + ) do + end + end + SPEC expect(lifecycle).to have_received(:start_test_case).once do |arg| expect(arg.links).to contain_exactly( @@ -45,7 +69,16 @@ end it "adds test severity" do - run_rspec("spec/fixture/specs/tag_test.rb") + run_rspec(<<~SPEC) + describe "Suite" do + it( + "spec", + tms: "QA-123", tms_2: "QA-124", issue: "BUG-123", issue_2: "BUG-124", + flaky: true, muted: true, severity: "critical" + ) do + end + end + SPEC expect(lifecycle).to have_received(:start_test_case).once do |arg| expect(arg.labels).to include(result_utils.severity_label("critical")) @@ -53,7 +86,16 @@ end it "adds custom status details" do - run_rspec("spec/fixture/specs/tag_test.rb") + run_rspec(<<~SPEC) + describe "Suite" do + it( + "spec", + tms: "QA-123", tms_2: "QA-124", issue: "BUG-123", issue_2: "BUG-124", + flaky: true, muted: true, severity: "critical" + ) do + end + end + SPEC expect(lifecycle).to have_received(:start_test_case).once do |arg| expect(arg.status_details).to eq(Allure::StatusDetails.new(flaky: true, muted: true, known: false)) @@ -61,7 +103,29 @@ end it "adds suite labels", test: true do - run_rspec("spec/fixture/specs/nested_test.rb") + run_rspec(<<~SPEC) + describe "Suite" do + describe "Nested Suite 1" do + it "Spec 1 - 1" do + end + end + describe "Nested Suite 2" do + it "Spec 2 - 1" do + end + it "Spec 2 - 2" do + end + describe "Nested Suite 2:1" do + it "Spec 2:1 - 1" do + end + describe "Nested Suite 2:1:1" do + it "Spec 2:1:1 - 1" + end + end + end + it "Spec" do + end + end + SPEC examples = [] expect(lifecycle).to have_received(:start_test_case).exactly(6).times do |arg| diff --git a/allure-rspec/spec/unit/formatter_start_spec.rb b/allure-rspec/spec/unit/formatter_start_spec.rb index af247e6..62ec2eb 100644 --- a/allure-rspec/spec/unit/formatter_start_spec.rb +++ b/allure-rspec/spec/unit/formatter_start_spec.rb @@ -1,11 +1,17 @@ # frozen_string_literal: true -describe "RSpecFormatter.start" do +describe "start" do include_context "allure mock" include_context "rspec runner" - it "Cleans allure results directory" do - run_rspec("spec/fixture/specs/simple_test.rb") + it "cleans allure results directory" do + run_rspec(<<~SPEC) + describe "Suite" do + it "spec", allure: "some_label" do |e| + e.step(name: "test body") + end + end + SPEC expect(lifecycle).to have_received(:clean_results_dir).once end