2

According to this piece of documentation, it is possible to achieve the following output format by selecting the format documentation:

something
  does something that passes
  does something that fails (FAILED - 1)
  does something that is pending (PENDING: Not Yet Implemented)

Is it possible to slightly edit this so it outputs as:

something
  does something (SUCCESS)
  does something (FAIL)
  does something (PENDING)

Basically I would like the result of the test displayed no matter what - instead of just logging explicitly the failures and the pending ones.

2 Answers 2

2

I was able to do this by subclassing RSpec::Core::Formatters::DocumentationFormatter. Create the following file as spec/formatters/custom_formatter.rb:

class CustomFormatter < RSpec::Core::Formatters::DocumentationFormatter
  RSpec::Core::Formatters.register self

  private

  def passed_output(example)
    format_output(example, 'SUCCESS', :success)
  end

  def pending_output(example, _message)
    format_output(example, 'PENDING', :pending)
  end

  def failure_output(example)
    format_output(example, 'FAILED', :failure)
  end

  def format_output(example, status_text, code_or_symbol)
    RSpec::Core::Formatters::ConsoleCodes.wrap(
      "#{current_indentation}#{example.description.strip} (#{status_text})",
      code_or_symbol
    )
  end
end

Then run the specs using this:

rspec --require formatters/custom_formatter --format CustomFormatter

Instead of --require formatters/custom_formatter, you can also require the formatter on spec/spec_helper.rb, e.g.

require_relative 'formatters/custom_formatter'

Then you only need to run this:

rspec --format CustomFormatter

If you want CustomFormatter to be the default formatter, you can add the command line options to .rspec configuration file at your project root. Here's how it should look like:

--require spec_helper

--require /path/to/custom_formatter.rb
--format CustomFormatter

With that, you no longer need to specify any command line arguments to use CustomFormatter.

Documentations and references:

Sign up to request clarification or add additional context in comments.

2 Comments

That works brilliantly for SUCCESS (not sure about PENDING, however it gives me that error whenever a test fails: failure_output: wrong number of arguments (2 for 1) (ArgumentError). I could fix it by editing the signature of the method you provided as follows: def failure_output(example, _message)
@Pauline Glad that you managed to get it working despite the error. Looks like you're using an older version of RSpec. Starting from RSpec 3.5, that method is changed to just 1 argument. Here's the commit that changed it: github.com/rspec/rspec-core/commit/…
0

You can not change the existing RSpec formatters, but you can create your own

When RSpec's built-in output formatters don't, however, give you everything you need, you can write your own custom formatter and tell RSpec to use that one instead. The simplest way is to subclass RSpec's BaseTextFormatter, and then override just the methods that you want to modify.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.