Skip to content

Commit

Permalink
Refactor allure-rspec tests (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrcuns authored Mar 29, 2020
1 parent de42538 commit 7b421ab
Show file tree
Hide file tree
Showing 15 changed files with 217 additions and 104 deletions.
2 changes: 2 additions & 0 deletions allure-rspec/.rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--format documentation
--require spec_helper
11 changes: 0 additions & 11 deletions allure-rspec/spec/fixture/specs/exception_test.rb

This file was deleted.

23 changes: 0 additions & 23 deletions allure-rspec/spec/fixture/specs/nested_test.rb

This file was deleted.

15 changes: 0 additions & 15 deletions allure-rspec/spec/fixture/specs/simple_test.rb

This file was deleted.

8 changes: 0 additions & 8 deletions allure-rspec/spec/fixture/specs/step_test.rb

This file was deleted.

10 changes: 0 additions & 10 deletions allure-rspec/spec/fixture/specs/tag_test.rb

This file was deleted.

26 changes: 20 additions & 6 deletions allure-rspec/spec/integration/full_report_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
50 changes: 50 additions & 0 deletions allure-rspec/spec/rspec_runner_helper.rb
Original file line number Diff line number Diff line change
@@ -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
13 changes: 7 additions & 6 deletions allure-rspec/spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand All @@ -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)
Expand All @@ -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
11 changes: 9 additions & 2 deletions allure-rspec/spec/unit/formatter_add_step_spec.rb
Original file line number Diff line number Diff line change
@@ -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|
Expand Down
34 changes: 29 additions & 5 deletions allure-rspec/spec/unit/formatter_example_finished_spec.rb
Original file line number Diff line number Diff line change
@@ -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"

Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
10 changes: 8 additions & 2 deletions allure-rspec/spec/unit/formatter_example_group_finished_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
10 changes: 8 additions & 2 deletions allure-rspec/spec/unit/formatter_example_group_started_spec.rb
Original file line number Diff line number Diff line change
@@ -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")
Expand Down
Loading

0 comments on commit 7b421ab

Please sign in to comment.