Execute Rspec from Ruby

前端 未结 4 955
孤城傲影
孤城傲影 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:48

    Your problem is that you're using the Kernel#system method to execute your command, which only returns true or false based on whether or not it can find the command and run it successfully. Instead you want to capture the output of the rspec command. Essentially you want to capture everything that rspec outputs to STDOUT. You can then iterate through the output to find and parse the line which will tell you how many examples were run and how many failures there were.

    Something along the following lines:

    require 'open3'
    stdin, stdout, stderr = Open3.popen3('rspec spec/models/my_crazy_spec.rb')
    total_examples = 0
    total_failures = 0
    stdout.readlines.each do |line|
      if line =~ /(\d*) examples, (\d*) failures/
        total_examples = $1
        total_failures = $2
      end
    end
    puts total_examples
    puts total_failures
    

    This should output the number of total examples and number of failures - adapt as needed.

提交回复
热议问题