如何检测 rspec 文件是否被 运行 作为测试套件的一部分
How to detect if an rspec file is being run as part of a test suite
如何从规范文件内部检测文件是 运行 作为测试套件的一部分还是单独存在。如果它本身是 运行,我想要详细的输出,但如果它是多个文件中的一个,那么我想抑制输出。
例如如果文件是 'spec/models/my_model_spec.rb' 我想区分
rspec spec
和
rspec spec/models/my_model_spec.rb
我发现这在我的 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,是默认的格式化程序。它打印代表测试进度的点 运行。绿点代表测试成功 运行。
另一个格式化程序,文档,使用 describe
、context
和 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 中的配置代码允许您在只有 运行 一个文件的情况下更改 default_formatter。您始终可以通过在命令行中指定不同的格式化程序来覆盖默认格式化程序。
RSpec源代码的评论帮助我回答了这个问题:https://github.com/rspec/rspec-core/blob/7b6b9c3f2e2878213f97d6fc9e9eb23c323cfe1c/lib/rspec/core/formatters.rb
如何从规范文件内部检测文件是 运行 作为测试套件的一部分还是单独存在。如果它本身是 运行,我想要详细的输出,但如果它是多个文件中的一个,那么我想抑制输出。
例如如果文件是 'spec/models/my_model_spec.rb' 我想区分
rspec spec
和
rspec spec/models/my_model_spec.rb
我发现这在我的 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,是默认的格式化程序。它打印代表测试进度的点 运行。绿点代表测试成功 运行。
另一个格式化程序,文档,使用 describe
、context
和 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 中的配置代码允许您在只有 运行 一个文件的情况下更改 default_formatter。您始终可以通过在命令行中指定不同的格式化程序来覆盖默认格式化程序。
RSpec源代码的评论帮助我回答了这个问题:https://github.com/rspec/rspec-core/blob/7b6b9c3f2e2878213f97d6fc9e9eb23c323cfe1c/lib/rspec/core/formatters.rb