I am having a problem running my tests from the terminal and from rake, e.g. rake test:integration
At the moment, I have the requires for test_helper.rb specified like this:
require File.dirname(__FILE__) + '/../test_helper'
This works fine when running them from the terminal but obviously when it is ran from rake, the directory is different and the process cannot find the test_helper file.
I think I want to add to this to my $load_path but I am not sure how to add it when running only in the test environment.
Can anyone help me out?
You can revert to just require 'test_helper' (the default for integration tests, at least with Rails 2.3.x). This will allow tests to run from a rake task, and as long as you cd to the test directory within your rails app, you can run tests via the terminal with ruby integration/your_test.rb.
Related
In Rails 5.1, you can do bin/rails test to run normal tests, and bin/rails test:system. What is the Rails sanctioned way of running both at the same time?
bin/rails test:system test
Specifying test:system before test will run both system and ordinary tests. The opposite order will only run the ordinary tests however.
rails test:all (Rails 6.1+)
Rails 6.1 introduces a new command - rails test:all.
It runs all test files in the test directory, including system tests.
Here is a link to PR.
And also a link to the docs (please, scroll down to yellow box).
In case anyone else is looking for the answer:
bin/rails test test/*
If it is your intention to run it using just $ rake or $rake test you can add into your Rakefile:
task test: 'test:system'
This will makes 'test:system' a "prerequisites" for "test" task
At least from the official rails guide, it seems there is no way of doing it:
By default, running bin/rails test won't run your system tests. Make sure to run bin/rails test:system to actually run them.
Ref: rails guide
You can also add this snippet in your lib/tasks folder, that will give you the option to do rake test:all
namespace :test do
desc "Run both regular tests and system tests"
task :all => 'test' do
Minitest.after_run {system('rake test:system')}
end
end
Summary of all the answers for easy reference:
System tests Only
bin/rails test:system
Ordinary tests Only
bin/rails test .
ALL tests
bin/rails test:all
According to the Rails guide for testing:
"By default, running bin/rails test won't run your system tests. Make sure to run bin/rails test:system to actually run them."
Does anyone know how to change the default behavior so it will run all of your tests including system tests?
I'm trying to do the same.
I think that for having rails test to also run system tests you need to redefine the rake task. For what I could investigate the task is defined at testing.rake file inside the railties gem /lib directory:
desc "Runs all tests in test folder except system ones"
task :test do
$: << "test"
if ENV.key?("TEST")
Minitest.rake_run([ENV["TEST"]])
else
Minitest.rake_run(["test"], ["test/system/**/*"])
end
end
The second argument in the function inside else is a pattern to exclude when running the tests.
Here Overriding rails' default rake tasks
it's explained how to override them, however I couldn't put it to work this way.
If it helps you, I'm using bundle exec rake test:system test instead and it runs both the system and all the other tests together
I manage a server running an old rails 2 app (being upgraded to rails 4 soon) and there's a script giving an error that running the same code from the console does not. It outputs this when I run the script:
rails#net:/c$ RAILS_ENV=production script/runner stc_cron.rb
/current/vendor/rails/railties/lib/commands/runner.rb:45: /current/vendor/rails/activerecord/lib/../../activesupport/lib/active_support/dependencies.rb:249:in `load_missing_constant': Expected /current/vendor/other_extensions/stc/app/models/contact.rb to define Contact (LoadError)
but the exact same code run in the rails console is fine and /current/vendor/other_extensions/stc/app/models/contact.rb does define Contact and has no errors
here's the code from the script:
#!/usr/local/bin/ruby
ENV['RAILS_ENV'] = ARGV[0] || 'production'
require 'config/boot'
Extension.load_all
require 'lib/satacard'
stc=SaTaCard.new
stc.get_and_process_new_stc
stc.get_and_process_new_contacts
why would it run fine in the console but fail in the script?
It looks like you are not loading the Rails environment. Load it after config/boot:
require 'config/environment'
I'm having a lot of trouble running my cuke test cases that use capybara-webkit driver. Because this is a headless server, I'm trying to run xvfb-run to run the tests, but keep running into roadblocks.
If I try to run
xvfb-run rake cucumber:all
then I'm getting errors from my server that rake doesn't exist. (Even though it does)
If I try to run it via bundle exec
xvfb-run bundle exec rake cucumber:all
then I get an error telling me bundle doesn't exist!
Both commands DO work from the ssh into the server, but its only when coming from Bamboo that they don't. I've tried both the Rake task, and just doing up a script to run the bundle exec, but nada.
I'm wondering if anyone else has this set up, and if they could walk me through how they have it setup within bamboo... This is a pretty typical rails project, and this is my LAST stage before I can get my CI up and running.
Thanks!
I think I have figured it out based on this page I found here, which was in regard to using Jenkins. http://sermoa.wordpress.com/2011/07/02/cucumber-running-headless-selenium-with-jenkins-the-easy-way/
The basic idea is to use the "headless" gem and then put this section in your env.rb for cucumber:
if ENV['HEADLESS'] == 'true'
require 'headless'
headless = Headless.new
headless.start
at_exit do
headless.destroy
end
end
Then run your normal rake task in bamboo with the environment variable "HEADLESS=true".
You have to have xvfb installed on the server too. (sudo apt-get install xvfb)
Here is a working updated version that will setup capybara-webkit and headless for cucumber using before/after hooks. Just include this in your support/env.rb or another support file (I used support/javascript.rb because there are a few related things I do):
Capybara.javascript_driver = :webkit
Before do
# run capybara-webkit headless if not on mac and this test is selenium based.
if Capybara.current_driver == :selenium
require 'headless'
#headless = Headless.new
#headless.start
end
end
After do
#headless.destroy
end
EDIT: Here's a gist with some taggable goodness where you can use :chrome in local dev mode if you want while making sure the CI environment remains headless:
https://gist.github.com/rosskevin/5937888
In my cucumber scenarios, if I call rake db:schema:load within a target Rails app folder, I get the cucumber process's $LOAD_PATH and not the Rails app's own Gemfile/load path. I think this is very weird.
The consequence is that I get the following error:
no such file to load -- rails/all
I can't reproduce it outside of my cucumber scenario.
ruby -rubygems -e "system 'rake -T'"
works normally -> the 'rake -T' has the application's own Gemfile-based $LOAD_PATH; and doesn't generate the error above.
Can anyone think why a child process (rake -T or rake db:schema:load or rails runner...; invoked by either system, exec, %x[...] or backtick; would start with the parent processes' $LOAD_PATH (from the cucumber scenario's Gemfile) instead of its own $LOAD_PATH (from the Rails app's Gemfile)?
When you use bundler, either via bundle exec or require 'bundler/setup', it finds your Gemfile, then puts its location in ENV["BUNDLE_GEMFILE"]. However, if this is already set, then bundler just reuses the value. This is what causes your Rails app to use the cucumber process's Gemfile.
If you want to execute something in the context of a different Gemfile, clear out ENV["BUNDLE_GEMFILE"] first. Bundler provides the method Bundler.with_clean_env(&blk) that may be helpful; it executes your block with the environment how it was before Bundler was loaded. Of course, you can also clear it out by hand with something like system("env -u BUNDLE_GEMFILE rake sometask").
The parent process's environment (ENV) is passed down to sub-shells. Either cucumber itself, how you are running cucumber (e.g. bundle exec cucumber), your scenarios, or code the scenario loads (e.g. the app, and therefore bundler), is messing with your ENV. Environment variables like RUBYLIB, GEM_PATH, and BUNDLE_GEMFILE can have a significant impact on what your sub-shelled Ruby processes can load / will behave.
Try printing out your ENV variable in your scenario and comparing it to what you get when you do it with ruby -rubygems -rpp -e "pp ENV", or just env on the command line.
For what it's worth, a possible alternative would be to load and invoke the rake task directly, e.g., Rake::Task['db:schema:load'].invoke, without using a sub-shell. Depends on what you're trying to do, though.