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

时间:2011-07-20 22:50:04

标签: ruby-on-rails unit-testing capistrano

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

提前致谢!

编辑:我最终使用this作为解决方案。

3 个答案:

答案 0 :(得分:4)

此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

答案 1 :(得分:2)

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

Capistrano任务,例如 LIB / Capistrano的/任务/ 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

答案 2 :(得分:0)

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