分析示例(--profile
)
--profile
命令行选项(可从 RSpec.configure
获取为 #profile_examples
),设置后,将导致 RSpec 打印最慢示例的列表。默认情况下,它打印 10 个最慢的示例,但您可以将其设置为不同的值以打印更多或更少的慢速示例。如果 --fail-fast
选项与 --profile
一起使用,并且存在失败,则不会显示慢速示例。
背景
给定一个名为“spec/spec_helper.rb”的文件,其中包含
并且一个名为“spec/example_spec.rb”的文件,其中包含
require "spec_helper"
RSpec.describe "something" do
it "sleeps for 0.1 seconds (example 1)" do
sleep 0.1
expect(1).to eq(1)
end
it "sleeps for 0 seconds (example 2)" do
expect(2).to eq(2)
end
it "sleeps for 0.15 seconds (example 3)" do
sleep 0.15
expect(3).to eq(3)
end
it "sleeps for 0.05 seconds (example 4)" do
sleep 0.05
expect(4).to eq(4)
end
it "sleeps for 0.05 seconds (example 5)" do
sleep 0.05
expect(5).to eq(5)
end
it "sleeps for 0.05 seconds (example 6)" do
sleep 0.05
expect(6).to eq(6)
end
it "sleeps for 0.05 seconds (example 7)" do
sleep 0.05
expect(7).to eq(7)
end
it "sleeps for 0.05 seconds (example 8)" do
sleep 0.05
expect(8).to eq(8)
end
it "sleeps for 0.05 seconds (example 9)" do
sleep 0.05
expect(9).to eq(9)
end
it "sleeps for 0.05 seconds (example 10)" do
sleep 0.05
expect(10).to eq(10)
end
it "sleeps for 0.05 seconds (example 11)" do
sleep 0.05
expect(11).to eq(11)
end
end
默认情况下不显示配置文件
当我运行rspec spec
时
那么示例应该全部通过
并且输出不应包含“example 1”
并且输出不应包含“example 2”
并且输出不应包含“example 3”
并且输出不应包含“example 4”
并且输出不应包含“example 5”
并且输出不应包含“example 6”
并且输出不应包含“example 7”
并且输出不应包含“example 8”
并且输出不应包含“example 9”
并且输出不应包含“example 10”
并且输出不应包含“example 11”。
将 profile_examples
设置为 true 会显示 10 个示例
给定一个名为“spec/spec_helper.rb”的文件,其中包含
RSpec.configure { |c| c.profile_examples = true }
当我运行rspec spec
时
那么示例应该全部通过
并且输出应该包含“Top 10 slowest examples”
并且输出应该包含“example 1”
并且输出不应包含“example 2”
并且输出应该包含“example 3”
并且输出应该包含“example 4”
并且输出应该包含“example 5”
并且输出应该包含“example 6”
并且输出应该包含“example 7”
并且输出应该包含“example 8”
并且输出应该包含“example 9”
并且输出应该包含“example 10”
并且输出应该包含“example 11”。
将 profile_examples
设置为 2 会显示 2 个示例
给定一个名为“spec/spec_helper.rb”的文件,其中包含
RSpec.configure { |c| c.profile_examples = 2 }
当我运行rspec spec
时
那么示例应该全部通过
并且输出应该包含“Top 2 slowest examples”
并且输出应该包含“example 1”
并且输出不应包含“example 2”
并且输出应该包含“example 3”
并且输出不应包含“example 4”
并且输出不应包含“example 5”
并且输出不应包含“example 6”
并且输出不应包含“example 7”
并且输出不应包含“example 8”
并且输出不应包含“example 9”
并且输出不应包含“example 10”
并且输出不应包含“example 11”。
通过 CLI 使用 --profile
设置配置文件示例
当我运行rspec spec --profile 2
时
那么示例应该全部通过
并且输出应该包含“Top 2 slowest examples”
并且输出应该包含“example 1”
并且输出不应包含“example 2”
并且输出应该包含“example 3”
并且输出不应包含“example 4”
并且输出不应包含“example 5”
并且输出不应包含“example 6”
并且输出不应包含“example 7”
并且输出不应包含“example 8”
并且输出不应包含“example 9”
并且输出不应包含“example 10”
并且输出不应包含“example 11”。
使用 --no-profile
覆盖配置选项
给定一个名为“spec/spec_helper.rb”的文件,其中包含
RSpec.configure { |c| c.profile_examples = true }
当我运行rspec spec --no-profile
时
那么示例应该全部通过
并且输出不应包含“example 1”
并且输出不应包含“example 2”
并且输出不应包含“example 3”
并且输出不应包含“example 4”
并且输出不应包含“example 5”
并且输出不应包含“example 6”
并且输出不应包含“example 7”
并且输出不应包含“example 8”
并且输出不应包含“example 9”
并且输出不应包含“example 10”
并且输出不应包含“example 11”。
使用 --profile
与 --fail-fast
如果一切都通过,则显示慢速示例
当我运行rspec spec --fail-fast --profile
时
那么示例应该全部通过
并且输出应该包含“Top 10 slowest examples”
并且输出应该包含“example 1”
并且输出不应包含“example 2”
并且输出应该包含“example 3”
并且输出应该包含“example 4”
并且输出应该包含“example 5”
并且输出应该包含“example 6”
并且输出应该包含“example 7”
并且输出应该包含“example 8”
并且输出应该包含“example 9”
并且输出应该包含“example 10”
并且输出应该包含“example 11”。
使用 --profile
即使在发生故障的情况下也会显示慢速示例
给定一个名为“spec/example_spec.rb”的文件,其中包含
require "spec_helper"
RSpec.describe "something" do
it "sleeps for 0.1 seconds (example 1)" do
sleep 0.1
expect(1).to eq(1)
end
it "fails" do
fail
end
end
当我运行rspec spec --profile
时
那么输出应该包含“2 examples, 1 failure”
并且输出应该包含“Top 2 slowest examples”
并且输出应该包含“example 1”。
使用 --profile
与 --fail-fast
在发生故障的情况下不会显示慢速示例
给定一个名为“spec/example_spec.rb”的文件,其中包含
require "spec_helper"
RSpec.describe "something" do
it "sleeps for 0.1 seconds (example 1)" do
sleep 0.1
expect(1).to eq(1)
end
it "fails" do
fail
end
end
当我运行rspec spec --fail-fast --profile
时
那么输出不应包含“Top 2 slowest examples”
并且输出不应包含“example 1”。
使用 --profile
与慢速 before 钩子包括钩子执行时间
给定一个名为“spec/example_spec.rb”的文件,其中包含
RSpec.describe "slow before context hook" do
before(:context) do
sleep 0.2
end
context "nested" do
it "example" do
expect(10).to eq(10)
end
end
end
RSpec.describe "slow example" do
it "slow example" do
sleep 0.1
expect(10).to eq(10)
end
end
当我运行rspec spec --profile 1
时
那么输出应该报告“slow before context hook” 作为最慢的示例组。