--format 选项

使用 --format 选项告诉 RSpec 如何格式化输出。

RSpec 附带了几个内置格式化程序。默认情况下,它使用进度格式化程序,生成如下输出

  ....F.....*.....

“.” 代表通过的示例,“F” 代表失败,“*” 代表待处理。

使用文档格式化程序查看传递给 describeit 及其别名的文档字符串

  $ rspec spec --format documentation

您还可以使用 `--out` 选项指定输出目标(默认情况下为 `$stdout`),该选项紧随 `--format` 选项之后

  $ rspec spec --format documentation --out rspec.txt

运行 rspec --help 查看可用格式化程序的列表。

背景

给定一个名为“example_spec.rb”的文件,其中包含

RSpec.describe "something" do
  it "does something that passes" do
    expect(5).to eq(5)
  end

  it "does something that fails" do
    expect(5).to eq(4)
  end

  it "does something that is pending", :pending => true do
    expect(5).to be < 3
  end

  it "does something that is skipped", :skip => true do
    expect(5).to be < 3
  end
end

进度条格式(默认)

我运行 rspec --format progress example_spec.rb

那么输出应该包含“。F**”。

文档格式

我运行 rspec example_spec.rb --format documentation

那么输出应该包含

something
  does something that passes
  does something that fails (FAILED - 1)
  does something that is pending (PENDING: No reason given)
  does something that is skipped (PENDING: No reason given)

保存到文件的文档格式

我运行 rspec example_spec.rb --format documentation --out rspec.txt

那么文件“rspec.txt”应该包含

something
  does something that passes
  does something that fails (FAILED - 1)
  does something that is pending (PENDING: No reason given)
  does something that is skipped (PENDING: No reason given)

多种格式和输出目标

我运行 rspec example_spec.rb --format progress --format documentation --out rspec.txt

那么输出应该包含“。F**”

并且文件“rspec.txt”应该包含

something
  does something that passes
  does something that fails (FAILED - 1)
  does something that is pending (PENDING: No reason given)
  does something that is skipped (PENDING: No reason given)