【发布时间】:2011-07-20 22:50:03
【问题描述】:
当我运行cap deploy 时,有没有办法让 capistrano 在我的 Rails 应用程序上运行单元测试,如果没有通过则失败?我知道这可以而且应该由部署人员完成,但我希望它是自动的。任何想法将不胜感激。
提前致谢!
编辑:我最终使用this 作为解决方案。
【问题讨论】:
标签: ruby-on-rails unit-testing capistrano
当我运行cap deploy 时,有没有办法让 capistrano 在我的 Rails 应用程序上运行单元测试,如果没有通过则失败?我知道这可以而且应该由部署人员完成,但我希望它是自动的。任何想法将不胜感激。
提前致谢!
编辑:我最终使用this 作为解决方案。
【问题讨论】:
标签: ruby-on-rails unit-testing capistrano
此 capistrano 任务将在正在部署的服务器上以生产模式运行单元测试:
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
在这里找到解决方案:http://marklunds.com/articles/one/338
:D
【讨论】:
此设置将在部署之前在本地运行您的测试。
Capistrano 任务,例如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/deploy.rb
before 'deploy:starting', 'deploy:test_suite'
在 Capistrano v3.x 中工作
【讨论】:
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
使用 with 运行它
cap production deploy:run_tests
【讨论】: