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

问题描述 投票:0回答:1

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

例如,如果文件是'spec / models / my_model_spec.rb',我想告诉它们之间的区别

rspec spec

rspec spec/models/my_model_spec.rb
rspec
1个回答
1
投票

我在spec_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,是默认格式化程序。它打印代表测试运行中进度的点。绿点等于成功的测试运行。

另一个格式化程序,文档,使用describecontextit描述来显示测试结果。所以,鉴于此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中的配置代码允许您在仅运行一个文件的情况下更改default_formatter。您始终可以通过在命令行上指定其他格式器来覆盖默认格式化程序。

关于RSpec源代码的评论帮助我回答了这个问题:https://github.com/rspec/rspec-core/blob/7b6b9c3f2e2878213f97d6fc9e9eb23c323cfe1c/lib/rspec/core/formatters.rb

© www.soinside.com 2019 - 2024. All rights reserved.