类: RSpec::Core::Runner

继承
Object
  • Object
显示全部
定义在
lib/rspec/core/runner.rb

概述

提供运行一组 RSpec 示例的主要入口点。

类方法摘要 折叠

实例方法摘要 折叠

构造函数详情

#initialize(options, configuration = RSpec.configuration, world = RSpec.world) ⇒Runner

返回 Runner 的一个新实例。

75
76
77
78
79
# File 'lib/rspec/core/runner.rb', line 75
def initialize(options, configuration=RSpec.configuration, world=RSpec.world)
  @options       = options
  @configuration = configuration
  @world         = world
end

类方法详情

.autorunvoid

注意

这通常不需要。rspec 命令会自行处理运行示例,无需涉及 at_exit 钩子。这只有在您使用 ruby 命令运行规范时才需要,即使那样,正常调用此方法的方式也是通过需要 rspec/autorun

注册一个 at_exit 钩子,在进程退出时运行套件。

16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rspec/core/runner.rb', line 16
def self.autorun
  if autorun_disabled?
    RSpec.deprecate("Requiring `rspec/autorun` when running RSpec via the `rspec` command")
    return
  elsif installed_at_exit? || running_in_drb?
    return
  end
  at_exit { perform_at_exit }
  @installed_at_exit = true
end

.invokevoid

运行规范套件并以适当的退出代码退出进程。

43
44
45
46
47
# File 'lib/rspec/core/runner.rb', line 43
def self.invoke
  disable_autorun!
  status = run(ARGV, $stderr, $stdout).to_i
  exit(status) if status != 0
end

.run(args, err = $stderr, out = $stdout) ⇒Fixnum

运行一组 RSpec 示例。不退出。

这在 RSpec 内部用于运行套件,但可供任何其他自动化工具使用。

如果您想在同一个进程中多次运行它,并且您希望像 spec_helper.rb 这样的文件被重新加载,请确保加载 load 而不是 require

参数

  • args (Array)

    命令行支持的参数

  • err (IO) (默认值: $stderr)

    错误流

  • out (IO) (默认值: $stdout)

    输出流

返回

  • (Fixnum)

    退出状态代码。如果所有规范都通过,则为 0;如果规范失败,则为配置的失败退出代码(默认值为 1)。

64
65
66
67
68
69
70
71
72
73
# File 'lib/rspec/core/runner.rb', line 64
def self.run(args, err=$stderr, out=$stdout)
  trap_interrupt
  options = ConfigurationOptions.new(args)
  if options.options[:runner]
    options.options[:runner].call(options, err, out)
  else
    new(options).run(err, out)
  end
end

实例方法详情

#run(err, out) ⇒void

配置并运行规范套件。

参数

  • err (IO)

    错误流

  • out (IO)

    输出流

85
86
87
88
89
90
91
92
# File 'lib/rspec/core/runner.rb', line 85
def run(err, out)
  setup(err, out)
  return @configuration.reporter.exit_early(exit_code) if RSpec.world.wants_to_quit
  run_specs(@world.ordered_example_groups).tap do
    persist_example_statuses
  end
end

#run_specs(example_groups) ⇒Fixnum

运行提供的示例组。

参数

返回

  • (Fixnum)

    退出状态代码。如果所有规范都通过,则为 0;如果规范失败,则为配置的失败退出代码(默认值为 1)。

113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/rspec/core/runner.rb', line 113
def run_specs(example_groups)
  examples_count = @world.example_count(example_groups)
  examples_passed = @configuration.reporter.report(examples_count) do |reporter|
    @configuration.with_suite_hooks do
      if examples_count == 0 && @configuration.fail_if_no_examples
        return @configuration.failure_exit_code
      end
      example_groups.map { |g| g.run(reporter) }.all?
    end
  end
  exit_code(examples_passed)
end

#setup(err, out) ⇒void

将各种配置对象和状态持有者连接在一起。

参数

  • err (IO)

    错误流

  • out (IO)

    输出流

98
99
100
101
102
103
104
105
# File 'lib/rspec/core/runner.rb', line 98
def setup(err, out)
  configure(err, out)
  return if RSpec.world.wants_to_quit
  @configuration.load_spec_files
ensure
  @world.announce_filters
end