Skip to content

Instantly share code, notes, and snippets.

@tompata
Created September 10, 2013 17:55
Show Gist options
  • Select an option

  • Save tompata/6513060 to your computer and use it in GitHub Desktop.

Select an option

Save tompata/6513060 to your computer and use it in GitHub Desktop.
More advanced CI rake tasks for running instant and daily builds with Rspec and Cucumber, Cucumber profiles (different screen sizes), re-run options (for failing tests).
CUCUMBER_PROFILES = %w{desktop mobile tablet_landscape tablet_portrait}
if [ :development, :test, :cucumber ].include?( Rails.env.to_sym )
# the 'require' calls are here on a purpose. On staging the rspec and cucumber gems are not
# present and since rake reads up the content of the .rake files, it will fail, because it
# will try to load things, which are not there
require 'rspec/core/rake_task'
require 'cucumber/rake/task'
namespace :ci do
namespace :build do
desc "Internal task for rspec execution, hence DO NOT CALL IT BY HAND!"
RSpec::Core::RakeTask.new(:rspec_internal) do |t|
# t.rspec_opts = ["--format JUnit", '-o log/TEST-rspec.xml']
end
CUCUMBER_PROFILES.each do |profile|
Cucumber::Rake::Task.new("cucumber_#{profile}", "Normal cucumber task for #{profile} profile") do |t|
t.profile = profile
end
Cucumber::Rake::Task.new("cucumber_#{profile}_logging_failing", "Cucumber task for #{profile} profile logging tests to rerun") do |t|
t.profile = profile
t.cucumber_opts = "--format pretty --format rerun --out tmp/rerun_#{profile}.txt"
end
Cucumber::Rake::Task.new("cucumber_#{profile}_rerun", "Cucumber task for #{profile} profile rerunning failed tests") do |t|
t.profile = "#{profile}_rerun"
t.cucumber_opts = "--format pretty --format rerun --out tmp/rerun_#{profile}.txt"
end
end
desc "Executes the rspec and cucumber tests on the latest version"
task :commit => :clean do
# let's check, if there's a non empty rerun file, and let's run it's tests first
if rerun_file_content('desktop').present?
log_out_message "the previous cucumber suite was failing, rerunning failing tests first"
rerun_cucumber('desktop')
else
log_out_message "no failing tests from the previous build"
end
run_rspec
run_cucumber_with_rerun('desktop')
end
desc "Executes the test cases several times, generates coverage and metrics data and checks that the test data up/down is working"
task :daily => :clean do
run_rspec
run_cucumber_with_rerun('desktop')
run_cucumber_with_rerun('mobile')
end
desc "deletes files to clean the environment"
task :clean do
FileUtils.rm_rf(Dir.glob("log/*.sha1"))
FileUtils.rm_rf(Dir.glob("log/test.log"))
FileUtils.rm_rf(Dir.glob("log/*.html.log"))
FileUtils.rm_rf(Dir.glob("log/TEST-*.xml"))
FileUtils.rm_rf(Dir.glob("log/coverage*"))
FileUtils.rm_rf("public/storage")
end
end
end
end
def run_cucumber_with_rerun(profile)
begin
log_out_message "running the whole cucumber stack with #{profile} profile"
Rake::Task["ci:build:cucumber_#{profile}_logging_failing"].execute
log_out_message "the test suite runned with success, no need to rerun failing tests"
rescue Exception => e # must handle Exception, we're looking for exit, which is not a StandardError
rerun_cucumber(profile)
end
end
def rerun_cucumber(profile)
begin
log_out_message "rerunning only the failing tests from the previous run with #{profile} profile"
Rake::Task["ci:build:cucumber_#{profile}_rerun"].execute
log_out_message "rerunning failing tests was a success"
rescue Exception => e
log_out_message "unfortunately rerunning the failing tests didn't help, so it's time to fix them!!! :)"
log_out_message "run the following command for failing tests: bundle exec cucumber -p #{profile} #{rerun_file_content(profile)}"
exit false
end
end
def run_rspec
log_out_message "running rspec tests now"
Rake::Task['ci:build:rspec_internal'].invoke
end
def rerun_file_content(profile)
File.file?("tmp/rerun_#{profile}.txt") ? IO.read("tmp/rerun_#{profile}.txt").to_s.strip : ""
end
def log_out_message(message)
puts "--------------- CI.RAKE -------------------"
puts message
puts "-------------------------------------------"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment