How to Get Rspec to Run All Tests Nested Under a Folder

How can I get Rspec to run all tests nested under a folder?

Rspec should already look recursively through the directory you named and find all tests. Note however, that it's looking for files ending in _spec.rb. Maybe some of your files are not named correctly?

If you need to be more specific about which files it should find, you can also use the --pattern option. For example: rspec --pattern spec/requests/*_spec.rb. (Option --pattern is equal to -P. Taken from rspec --help)

Rspec run all tests except a specific folder

Use an --exclude-pattern, they're quite convenient:

https://www.relishapp.com/rspec/rspec-core/v/3-3/docs/configuration/exclude-pattern

One of the nice things about them:

The --exclude-pattern flag accepts shell style glob unions

So you could do something like rspec --exclude-pattern "spec/{foldername1,foldername2}/**/*_spec.rb"

How can I specify multiple directories for RSpec to pull tests from?

just put spaces in between the directories like:

rspec spec/model/location.rb ../gem/spec/requests/user.rb

How can I run multiple spec folders in rspec with one command?

Use your shell:

rspec `find . -type d -name spec`

The inner command will effectively find all folders named spec under current folder and run rspec for all of them. You might also alias this to save keystrokes:

alias rss="find . -type d -name spec -exec rspec {} \;"

Put the above into your shell’s .rc file and type

rss

to run rspec on all these folders.

With RSpec, how do I write an expression that captures _spec files in nested directories?

glob is not a regex, so you need to enable the shell-option globstar in order to recurse subdirectories.

Adding the line - run: shopt -s globstar before your test call won't work because each step in CircleCI runs it in its own shell instance. You need to set the shell-opt in the same step that you run your tests. Something like this:

- run:
command: |
shopt -s globstar
bundle exec rspec --color --require spec_helper --format RspecJunitFormatter --out $CIRCLE_TEST_REPORTS/rspec/rspec.xml --format progress $(circleci tests glob spec/**/*_spec.rb | circleci tests split --split-by=timings)

In rails, how do I run all my model tests when I run bin/rspec?

As @jdno said, you need to rename your rspec file from some_model.rb to some_model_spec.rb.

Although I do not recommend, but RSpec has config option --pattern which you can change. For example, you can add .rspec file in your project path with this:

# your_project_dir/.rspec
--pattern 'spec/**/*.rb'

more info about --pattern option can be found here: https://relishapp.com/rspec/rspec-core/v/3-8/docs/command-line/pattern-option

Can rspec be configured to only run tests what have been modified within a single spec file?

I think you are looking for guard.

Checkout nicely written article https://collectiveidea.com/blog/archives/2017/02/09/guard-is-your-friend

RSPEC: Is it possible to run other spec file inside a spec file

You really want to be able to run tests independently. Having to run all tests, even "just" all tests in a given file, seems to me like an unacceptable delay in the development process. It also makes in hard to use tools like Guard that run tests for you based on a file watcher.

If there is really something that needs to run before, for example, every request spec, you can do something like:

# spec_helper.rb
RSpec.configure do |config|
config.before :all, type: :request do
# do stuff
end
end

But also look at what you are trying to do. If you are just setting up model/database data for the tests, then you want fixtures, or better yet, factories. Look into FactoryGirl.



Related Topics



Leave a reply



Submit