How to organize rspec 2 tests into 'unit' (fast) and 'integration' (slow) categories?
- I want to be able to run all unit tests with just
rspec
command, but not the 'integration' tests. - I want to be able to run only 'integration' tests.
How to organize rspec 2 tests into 'unit' (fast) and 'integration' (slow) categories?
rspec
command, but not the 'integration' tests. We have groups of the same nature. We run then one by one both on the local dev boxes and on the CI.
you can simply do
bundle exec rake spec:unit
bundle exec rake spec:integration
bundle exec rake spec:api
This is what our spec.rake looks like
namespace :spec do
RSpec::Core::RakeTask.new(:unit) do |t|
t.pattern = Dir['spec/*/**/*_spec.rb'].reject{ |f| f['/api/v1'] || f['/integration'] }
end
RSpec::Core::RakeTask.new(:api) do |t|
t.pattern = "spec/*/{api/v1}*/**/*_spec.rb"
end
RSpec::Core::RakeTask.new(:integration) do |t|
t.pattern = "spec/integration/**/*_spec.rb"
end
end
lib/tasks/spec.rake
–
Robustious load rspec/core/rake_task
line in a begin/rescue block to catch a LoadError. This will allow your rake tasks to be loaded in environments where Rspec is not available relishapp.com/rspec/rspec-core/docs/command-line/rake-task –
Robustious One way to do it is to tag your RSpec test cases like this:
it "should do some integration test", :integration => true do
# something
end
When you execute your test cases use this:
rspec . --tag integration
This will execute all the test cases with the tag :integration => true
. For more refer to this page.
rspec
command without arguments? –
Waggon --tag "~integration"
in your project .rpsec file. This is my preferred approach. Of course it is overridden by options you might specify explicitly on the command-line, so you still have complete flexibility. –
Pyromorphite I had to configure my unit
and feature
tests as follows:
require 'rspec/rails'
namespace :spec do
RSpec::Core::RakeTask.new(:unit) do |t|
t.pattern = Dir['spec/*/**/*_spec.rb'].reject{ |f| f['/features'] }
end
RSpec::Core::RakeTask.new(:feature) do |t|
t.pattern = "spec/features/**/*_spec.rb"
end
end
Had to add require 'rspec/rails'
and change Rspec
to RSpec
in the answer given by @KensoDev.
spec/unit
), and use a less-complicated pattern (e.g. spec/unit/**/*_spec.rb
)? –
Ministerial Notice at https://github.com/rspec/rspec-rails, they are telling you to place the gem under "group :development, :test" like so,
group :development, :test do
gem 'rspec-rails', '~> 2.0'
end
but if you place this only under :test group only,
group :test do
gem 'rspec-rails', '~> 2.0'
end
then you'll get the above error.
HTH
I suggest to use .rspec
file to configure patterns instead of using rake
because it's tricky to pass flags to RSpec when using rake.
You can read environment variables in your .rspec
file:
<%= if ENV['TEST'] == 'integration' %>
--pattern spec/integration/**/*_spec.rb
<% else %>
<% ENV['TEST'] = 'unit' %>
--pattern spec/unit/**/*_spec.rb
<% end %>
Then you can run TEST=integration rspec
to run integration tests or just rspec
to run unit tests. The advantage of this approach is that you can still pass flags to it like:
TEST=integration rspec -t login -f doc
© 2022 - 2024 — McMap. All rights reserved.