Is there a way to have capistrano run unit tests on my Rails app when I run cap deploy
, and fail if they don't pass? I know this can and should be done by the deplo开发者_如何学Cyer, but I'd love it to be automatic. Any ideas would be greatly appreciated.
Thanks in advance!
EDIT: I ended up using this as a solution.
This setup will run your tests locally before deploying.
Capistrano task, e.g. lib/capistrano/tasks/deploy.rake
namespace :deploy do
desc 'Run test suite before deployment'
task :test_suite do
run_locally do
execute :rake, 'test'
end
end
end
Capistrano config, config/deploy.rb
before 'deploy:starting', 'deploy:test_suite'
Works in Capistrano v3.x
This capistrano task will run the unit tests on the server being deployed, in production mode:
desc "Run the full tests on the deployed app."
task :run_tests do
run "cd #{release_path} && RAILS_ENV=production rake && cat /dev/null > log/test.log"
end
Found the solution here: http://marklunds.com/articles/one/338
:D
config/deploy.rb
# Path of tests to be run, use array with empty string to run all tests
set :tests, ['']
namespace :deploy do
desc "Runs test before deploying, can't deploy unless they pass"
task :run_tests do
test_log = "log/capistrano.test.log"
tests = fetch(:tests)
tests.each do |test|
puts "--> Running tests: '#{test}', please wait ..."
unless system "bundle exec rspec #{test} > #{test_log} 2>&1"
puts "--> Aborting deployment! One or more tests in '#{test}' failed. Results in: #{test_log}"
exit;
end
puts "--> '#{test}' passed"
end
puts "--> All tests passed, continuing deployment"
system "rm #{test_log}"
end
# Only allow a deploy with passing tests to be deployed
before :deploy, "deploy:run_tests"
end
Run it with with
cap production deploy:run_tests
精彩评论