Rake Task
RSpec comes with a Rake task for executing specs. See Spec::Rake::SpecTask API for details.
Rake task example
This is a snippet from RSpec’s own Rakefile. It creates a task to run the examples.
require 'rake'
require 'spec/rake/spectask'
desc "Run all examples"
Spec::Rake::SpecTask.new('examples') do |t|
t.spec_files = FileList['examples/**/*.rb']
end
It can be invoked from the command line with:
rake examples
Also see the RCov page for info about how to generate a coverage report.
Generate specdocs in RDoc format
require 'rake'
require 'spec/rake/spectask'
desc "Generate specdocs for examples for inclusion in RDoc"
Spec::Rake::SpecTask.new('examples_specdoc') do |t|
t.spec_files = FileList['examples/**/*.rb']
t.spec_opts = ["--format", "rdoc"]
t.out = 'EXAMPLES.rd'
end
This will write the specdocs to a file, which can be included in your RDoc. Just like this.
Generate HTML report
require 'rake'
require 'spec/rake/spectask'
desc "Generate HTML report for failing examples"
Spec::Rake::SpecTask.new('failing_examples_with_html') do |t|
t.spec_files = FileList['failing_examples/**/*.rb']
t.spec_opts = ["--format", "html:../doc/output/tools/failing_examples.html", "--diff"]
t.fail_on_error = false
end
This will write a HTML file that looks like this.