Automatically run tests during deployment with capistrano

Is there any way that the capistrano module tests run in my Rails application when cap deploy starts and fails if they fail? I know that this can and should be done by the installer, but I would like it to be automatic. Any ideas would be greatly appreciated.

Thanks in advance!

EDIT: I ended up using this as a solution.

+6
source share
3 answers

This capistrano task will run unit tests on the deployed server 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 a solution here: http://marklunds.com/articles/one/338

: D

+4
source

This installation will run your tests locally before deployment.

Capistrano task , for example. Library / 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 configuration, config / deploy.rb

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

Powered by Capistrano v3.x

+1
source

configuration / 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

 cap production deploy:run_tests 
0
source

Source: https://habr.com/ru/post/893215/


All Articles