Ruby-rspec - 如何打印出测试(组)名称

Mic*_*ant 5 ruby rspec

如果我有一些测试,例如

  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
    ...
Run Code Online (Sandbox Code Playgroud)

如何打印测试组名称 - 在这种情况下为"Line".

我尝试添加:

    before :all do
      puts "In #{self.class}"
    end
Run Code Online (Sandbox Code Playgroud)

但这给了:In RSpec::Core::ExampleGroup::Nested_3Line

Dan*_*yen 10

在测试过程中,您可能有特定的理由想要访问测试名称...但是,为了满足您在测试报告中只需要输出行的需要,我喜欢这样的配置:

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
Run Code Online (Sandbox Code Playgroud)

这给我的输出如下:

MyClassName
  The initialization process
    should accept two optional arguments
Run Code Online (Sandbox Code Playgroud)


sea*_*vis 10

RSpec 将从文件中读取命令行参数,因此您可以将以下内容添加到.rspec项目根目录中的文件中:

--format documentation
--color
Run Code Online (Sandbox Code Playgroud)

(此文件可能已经存在,具体取决于您用于 RSpec 的 gem 以及安装方式。)


Mic*_*ant 8

答案是:

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

$ rspec line_spec.rb 
In Line
Run Code Online (Sandbox Code Playgroud)