使用capistrano在部署上自动运行测试



是否有办法让capistrano在我的Rails应用程序上运行单元测试,当我运行cap deploy时,如果它们不通过就会失败?我知道这可以而且应该由部署人员完成,但我希望它是自动的。如有任何意见,不胜感激。

提前感谢!

编辑:我最终使用这个作为一个解决方案。

此设置将在部署之前在本地运行测试。

Capistrano task,例如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配置,配置/deploy.rb

before 'deploy:starting', 'deploy:test_suite'

适用于Capistrano v3.x

这个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

:

配置/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

cap production deploy:run_tests

最新更新