Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby-rspec - how to print out the test (group) name

Tags:

ruby

rspec

If I have some test, e.g.

  require_relative "Line"
  require_relative "LineParser"

  describe Line do

    it "Can be created" do
      load "spec_helper.rb"
      @line.class.should == Line
    end 
    it "Can be parsed" do
    ...

How can I print out the test group name - "Line" in this case.

I tried adding:

    before :all do
      puts "In #{self.class}"
    end

but that gives: In RSpec::Core::ExampleGroup::Nested_3, not Line

like image 266
Michael Durrant Avatar asked Oct 15 '13 01:10

Michael Durrant


2 Answers

You may have specific reasons for wanting access to the test name while you're in the test...however, just in case it fits your needs to just have the line output in the test report, I like this configuration:

RSpec.configure do |config|

   # Use color in STDOUT
  config.color_enabled = true

  # Use color not only in STDOUT but also in pagers and files
  config.tty = true

  # Use the specified formatter
  config.formatter = :documentation 

end

This gives me output like:

MyClassName
  The initialization process
    should accept two optional arguments
like image 75
dancow Avatar answered Sep 27 '22 23:09

dancow


RSpec will read command line arguments from a file, so you could add the following to a .rspec file in the root of your project:

--format documentation
--color

(This file may already exist depending on the gem you're using for RSpec and how you've installed it.)

like image 22
seancdavis Avatar answered Sep 27 '22 21:09

seancdavis