Execute Rspec from Ruby

前端 未结 4 968
孤城傲影
孤城傲影 2020-12-09 04:15

I am trying to execute rspec from ruby, and get the status or number of failures from a method or something like that. Actually I am running something like this:

<         


        
4条回答
  •  醉酒成梦
    2020-12-09 04:40

    I think the best way would be using RSpec's configuration and Formatter. This would not involve parsing the IO stream, also gives much richer result customisation programmatically.

    RSpec 2:

    require 'rspec'
    
    config = RSpec.configuration
    
    # optionally set the console output to colourful
    # equivalent to set --color in .rspec file
    config.color = true
    
    # using the output to create a formatter
    # documentation formatter is one of the default rspec formatter options
    json_formatter = RSpec::Core::Formatters::JsonFormatter.new(config.output)
    
    # set up the reporter with this formatter
    reporter =  RSpec::Core::Reporter.new(json_formatter)
    config.instance_variable_set(:@reporter, reporter)
    
    # run the test with rspec runner
    # 'my_spec.rb' is the location of the spec file
    RSpec::Core::Runner.run(['my_spec.rb'])
    

    Now you can use the json_formatter object to get result and summary of a spec test.

    # gets an array of examples executed in this test run
    json_formatter.output_hash
    

    An example of output_hash value can be found here:

    RSpec 3

    require 'rspec'
    require 'rspec/core/formatters/json_formatter'
    
    config = RSpec.configuration
    
    formatter = RSpec::Core::Formatters::JsonFormatter.new(config.output_stream)
    
    # create reporter with json formatter
    reporter =  RSpec::Core::Reporter.new(config)
    config.instance_variable_set(:@reporter, reporter)
    
    # internal hack
    # api may not be stable, make sure lock down Rspec version
    loader = config.send(:formatter_loader)
    notifications = loader.send(:notifications_for, RSpec::Core::Formatters::JsonFormatter)
    
    reporter.register_listener(formatter, *notifications)
    
    RSpec::Core::Runner.run(['spec.rb'])
    
    # here's your json hash
    p formatter.output_hash
    

    Other Resources

    • Detailed work through
    • Gist example

提交回复
热议问题