如何检测rspec文件是否作为测试套件的一部分运行

如何检测rspec文件是否作为测试套件的一部分运行,rspec,Rspec,从spec文件内部,我如何检测该文件是作为测试套件的一部分运行还是单独运行。如果它是自己运行的,我想要详细的输出,但是如果它是多个文件中的一个,那么我想要抑制输出 例如,如果文件是'spec/models/my_model_spec.rb',我想区分 rspec spec 及 我在我的spec\u helper.rb文件中发现了这个注释: # Many RSpec users commonly either run the entire suite or an individual # fil

从spec文件内部,我如何检测该文件是作为测试套件的一部分运行还是单独运行。如果它是自己运行的,我想要详细的输出,但是如果它是多个文件中的一个,那么我想要抑制输出

例如,如果文件是'spec/models/my_model_spec.rb',我想区分

rspec spec


我在我的
spec\u helper.rb
文件中发现了这个注释:

# Many RSpec users commonly either run the entire suite or an individual
# file, and it's useful to allow more verbose output when running an
# individual spec file.
if config.files_to_run.one?
  # Use the documentation formatter for detailed output,
  # unless a formatter has already been configured
  # (e.g. via a command-line flag).
  config.default_formatter = "doc"
end
将其移动到
RSpec.configure do | config |
块会生成您要查找的结果

编辑

RSpec提供四种不同的输出格式化程序:进度、文档、HTML和JSON。最后两个是不言自明的。第一个是progress,它是默认的格式化程序。它打印代表测试运行中进度的点。绿点表示测试成功

另一个格式化程序documentation使用
描述
上下文
it
描述来显示测试结果。因此,考虑到这种RSpec结构:

describe Stack do
  describe '#push' do
    context 'when the stack is empty' do
      it 'increases the size of the stack by 1'
    end
    context 'when the stack is full' do
      it 'throws a stack overflow exception'
      it 'does not increase the size of the stack'
    end
  end
end
文档格式化程序将输出以下内容:

Stack
  #push
    when the stack is empty
      increases the size of the stack by 1
    when the stack is full
      throws a stack overflow exception
      does not increase the size of the stack
您可以在命令行上试用各种格式设置程序,如下所示:

rspec --format progress
rspec --format doc (or documentation)
rspec --format html
rspec --format json
上面spec_helper中的配置代码允许您在仅运行一个文件的情况下更改默认的_格式化程序。通过在命令行上指定不同的格式化程序,您始终可以覆盖默认格式化程序

关于RSpec源代码的评论帮助我回答了这个问题:

rspec --format progress
rspec --format doc (or documentation)
rspec --format html
rspec --format json