RSpec > Is there a way to run all tests with one command? - ruby-on-rails

Is there a command that does this? I've searched but can not find anything

Try this in your terminal window:
bundle exec rspec spec

if you are using rspec-rails then you can run using rake spec
if you're testing models, use rake spec:models (or rake spec:routing or rake spec:controllers)
if just testing one model, use rake SPEC=app/models/modelname.rb

Create a .rspec file in the root of your project
Add the path to where your specs are e.g.
--default-path test/spec/
Add the pattern of how your files are named e.g.
--pattern ****/*.spec
Run rspec and it should pick all your specs and run them :)
default-path and pattern are simply command line arguments, which means you can also add any other command line argument that rspec takes (can run rspec --help to see the available options)
According to https://www.relishapp.com/rspec/rspec-core/v/2-0/docs/configuration/read-command-line-configuration-options-from-files you can also put the options in ~/.rspec, which will make the options available globally. Local .rspec file options will override the global options.

For controller test
bundle exec rake spec:controllers
For model test
bundle exec rake spec:models
For all test
bundle exec rake spec
For specific file test do
rspec file_name_spec.rb (example rspec spec/controllers/groups_controller_spec.rb)

go to your app directory and run rspec spec or bundle exec rspec spec.
use spork to speed your testing...(i kinda say its compulsory)

Related

Rails 5.1 run system tests and normal tests with one command

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

Change Rails/Rake Test Default Behavior

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

How to run only controller tests in MiniTest?

What is the rake command for running only controller tests in minitest?
rake test:controller doesn't do the trick.
Try making it plural. This is the typical command:
rake test:controllers
http://guides.rubyonrails.org/testing.html#rake-tasks-for-running-your-tests
Section 6 covers the rake commands for testing.
Has to be plural rake test:controllers as you're running all of them.
Please take a look at http://guides.rubyonrails.org/testing.html#rake-tasks-for-running-your-tests for more rake commands.
If you want to run a specific file, then use the TEST argument:
rake test TEST=test/controllers/application_controller_test.rb
As an update:
rails test path/to/test/file.rb has become my go-to solution.
rails test path/to/test/file.rb:123 lets you pick the test via line number as well.

How to configure guard-rspec to ignore a directory when all the test cases are running?

Lets say I have three directors in my spec folder; features, test, integration.
When I run bundle exec guard and press enter, is there a way that I can configure my Guardfile to ignore the test cases that are located in the integrations directory?
In your Guardfile, you can specify the command to use when running all specs. You can specify an rSpec command with a file exclude pattern to run everything but the integration specs:
guard :rspec,
cmd: 'bundle exec rspec',
run_all: { cmd: 'bundle exec rspec --exclude-pattern "**/integrations/*_spec.rb"' } do
# ...
end
You might have to tweak this a bit depending on what you need exactly, see the links in the answer for the relevant bits of documentation.

How to 'rake spec' one directory, e.g. models?

I can rake spec and all specs run.
However trying to run specs for one directory, as in
rake spec/models/ or rake spec/models/*.rb
does not provide any output or errors.
One option is that I can do
rspec spec/models/*.rb
or
rspec spec/models/
but I was wondering if I could stay within Rake.
Try rake spec:models instead of rake spec/models. Run rake -T | grep spec to see all the available rake spec tasks.
UPDATE: Running your specs through rake spec may be slower than running them through rspec spec, said by the rspec-rails guys. Read de installation section of rspec-rails.
Use SPEC env. variable:
rake spec SPEC=spec/models

Resources