Skip to content

Commit

Permalink
Output rspec seed if used (#15)
Browse files Browse the repository at this point in the history
Rspec supports running the specs in a [random order][1]. This helps in
surfacing any accidental dependencies and side effects in the test
suite. When debugging test failures caused by such side effects, it's
often necessary to reproduce the issue by running the specs in the same
order again. For that the seed value needs to be known and logged by the
formatter.

[1]: https://relishapp.com/rspec/rspec-core/v/3-8/docs/configuration/set-the-order-and-or-seed
  • Loading branch information
grobie authored Nov 3, 2022
1 parent 1895471 commit ddd92d6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/rspec/github/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
module RSpec
module Github
class Formatter < RSpec::Core::Formatters::BaseFormatter
RSpec::Core::Formatters.register self, :example_failed, :example_pending
RSpec::Core::Formatters.register self, :example_failed, :example_pending, :seed

def example_failed(failure)
notification = NotificationDecorator.new(failure)
Expand All @@ -20,6 +20,12 @@ def example_pending(pending)

output.puts "\n::warning file=#{notification.path},line=#{notification.line}::#{notification.annotation}"
end

def seed(notification)
return unless notification.seed_used?

output.puts notification.fully_formatted
end
end
end
end
22 changes: 22 additions & 0 deletions spec/rspec/github/formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,26 @@
end
end
end

describe '#seed' do
before { formatter.seed(notification) }

context 'when seed used' do
let(:notification) do
RSpec::Core::Notifications::SeedNotification.new(4242, true)
end

it 'outputs the fully formatted seed notification' do
is_expected.to eq "\nRandomized with seed 4242\n"
end
end

context 'when seed not used' do
let(:notification) do
RSpec::Core::Notifications::SeedNotification.new(nil, false)
end

it { is_expected.to be_empty }
end
end
end

0 comments on commit ddd92d6

Please sign in to comment.