RSpec--文件执行顺序

  1. 先加载 spec_helper 文件, spec_helper中的可直接运行部分执行
  2. 再顺序加载所有 *_spec.rb 文件, *_spec文件中的可直接执行部分执行
  3. 执行 spec_helper中的before(:suite)
  4. 执行 spec_helper中的before(:context),再执行文件 1_spec.rb中的,before(:context)-----类似于JUnit5中先父类的注解@BeforeAll再执行本类的@BeforeAll注解。
  5. 执行 spec_helper中的before(:example),再执行文件 1_spec.rb中的before(:example)----类似于先执行父类的@BeforeEach再执行本类的@BeforeEach
  6. 执行 1_spec.rb 文件中的,it测试用例
  7. 执行 1_spec.rb中的after(:example),再执行spec_helper中的after(:example)-----类似于先执行本类的@AfterEach再执行父类的@AfterEach
  8. 执行 1_spec.rb中的after(:context),再执行spec_helper中的after(:context)---类似于先执行本类的@AfterAll再执行父类的@AfterAll
  9. 执行文件 2_spec.rb(重复4,5,6,7,8)
  10. 执行 spec_helper 中的,after(:suite)
如下例子
# spec_helper.rb puts "AAA1"RSpec.configure do |config|config.before(:suite) do puts "Begin to run all cases..." end config.after(:suite) do puts "All cases done..." endputs "AAA2"# 父类中对每个测试用例集设置钩子 config.before(:context) do puts 'before a context defined in spec_helper' endconfig.after(:context) do puts 'after a context defined in spec_helper' end# 父类中对每个测试用例设置钩子 config.before(:example) do puts 'before a testcase defined in spec_helper' endconfig.after(:example) do puts 'after a testcase defined in spec_helper' endend# 1_spec.rb describe 'Test' do puts "进入Spec文件"# 某个spec文件中设置测试用例集的钩子 before(:context) do puts "before a context defined in 1_spec" endafter(:context) do puts "after a context defined in 1_spec" end# 某个spec文件中设置测试用例的钩子 before(:example) do puts "before a testcase defined in 1_spec" endafter(:example) do puts "after a testcase defined in 1_spec" endit 'test case1' do puts "running testcase1" endcontext 'contextAA' do puts "a context" it 'test case2' do puts "running testcase2" end end end## 执行结果 # AAA1 # AAA2 # 进入Spec文件 # a context# Begin to run all cases...# Test (就是describe名称) # before a context defined in spec_helper # before a context defined in 1_spec# before a testcase defined in spec_helper # before a testcase defined in 1_spec # running testcase1 # after a testcase defined in 1_spec # after a testcase defined in spec_helper #test case1(就是it的名称也可以是 . )#contextAA # before a testcase defined in spec_helper # before a testcase defined in 1_spec # running testcase2 # after a testcase defined in 1_spec # after a testcase defined in spec_helper #testcase2# after a context defined in 1_spec # after a context defined in spec_helper# All cases done...

    推荐阅读