How to use multiple rspec_helpers - ruby

We are experimenting with adding integration tests with rspec into our workflow, and are experimenting the following folder structure:
.
├── spec #unit tests
├── spec-integration #e2e tests
When I run the e2e tests with bundle exec rspec SPECS=spec-integration, the --require spec_helper from .rspec automatically goes to spec/spec_helper. This makes sense and expected, but we don't want to use the spec_helper from the unit tests, we want to use another one for integration tests.
How do I tell rspec to go to spec-integration/spec_handler instead of spec/spec_handler as a default?
(Edit/PS: Comments from a folder architecture or suggestions on a better way to design this are also welcome, as we are pretty new to the Ruby ecosystem)
Thanks!

You can override that by specifying --options command line flag. (ref)
bundle exec rspec --default-path spec-integration --options spec_handler
Also, when you use --default-path, you can have a spec-integration/spec_helper.rb and it will use that(because of --require spec_helper in .rspec).
(This is not working for me on rspec 3.9 though - bundle exec rspec SPECS=spec-integration.)
Coming to the folder structure part, I would keep integration specs in a sub-folder inside spec. Both unit and integration specs can have their own helper files and spec/spec_helper can have common configuration.

Related

Rspec multiple spec folders

Im currently building a project using Rspec in which I sepparated in two different folders with domain code and infrastructure code. Both folders have their own specs in a spec folder. The domain/spec folder is the one containing the spec_helper.rb file, thats required from the tests inside the other folder infrastructure/spec
I'd like to know how to have a spec folder in the root of the project, including the spec_helper file and also tests, and being able to run all the tests with just one command (right now I do it running rspec domain/ infrastructure/)
RSpec is designed to work with all tests in one folder. By default, this folder is called spec/, but you can use a different name with the --default-path option.
So, your options as I see it are:
Edit the source code of rspec-core to let that configuration support multiple directories. Hopefully your PR will be approved and merged.
Or, write a simple wrapper script that runs rspec against both directories. For example, you could alias rspecs='rspec domain/ infrastructure/'.
Or (what I would recommend!), you could just restructure your tests slightly to use spec/domain/ and spec/infrastructure/ folders -- and then everything will just work, by convention, out of the box.

Does .rspec file get run every time rspec runs?

Does the .rspec file you get when initializing the project, get run every time you run your tests (through rspec [file location]) ? Just curious because inside the .rspec file, using cat .rspec, I found that it contains require spec_helper. If it does get run every time, this would save me a lot of time from writing require spec_helper in each of my test files.
Yes. It gets called every time. You don't need to include it in every spec file.
I have --require spec_helper in my .rspec file.
Here's a link to the rspec docs, which includes a list of other options.

How do I set up a Ginkgo test suite?

I have inherited a Go project that consists of a lot of common files, a library of sorts, two executables, and theoretically a test suite. The test suite is being written after the fact. But I dislike the only way I've found of setting up is rather unpalatable
I'm using Ginkgo, and this is my starting directory structure
component1/component1.go
component2/component2.go
cmd1/cmd1.go
cmd2/cmd2.go
project_suite_test.go
component1_test.go
Each cmd?.go file will be compiled into a separate executable.
What I would like is a multi-file test suite, usually one file per component. Where do I put the files so that go test will find and run all of them, without leaving them here in the root of the project?
ginkgo init and ginkgo bootstrap will set up your tests. ginkgo -r will run all your tests recursively.
Reason:
Ginkgo command will only work if you have actually bootstrap your project via ginkgo.
Options:
To use that you have to go to your test dir in terminal and run
ginkgo init : To Initialise project:
ginkgo bootstrap : This will generate new file with test suite config
ginkgo or ginkgo test : this will now be able to run tests based on your new generated file because that's what it is trying to search.
Alternatively:
If you like to keep your tests in a sub-folder, say test, then running
go test ./...
will attempt to run tests in every folder, even those that do not contain any test, thus having a ? in the subsequent report for non-test folders.
Running
go test ./.../test
instead will target only your test folders, thus having a clean report focused on your tests folders only.
you can alternatively use 'go run $(ls *.go)' to run all the files in a given folder.
Notice you have regular expression within () braces.
In-case you want to run test in different path update path as per your desired dir in the regular expression
You can use go test ./... in the root and it will go into child folders and execute the tests:
component1/component1.go
component1/component1_test.go
component2/component2.go
component2/component2_test.go
cmd1/cmd1.go
cmd1/cmd1_test.go
cmd2/cmd2.go
cmd2/cmd2_test.go

Capybara + RSpec, spec/features dir being ignored by the rspec . command?

I'm using the new Capybara DSL with rspec following the short guideline located here
And the tests added to the spec/features directory run well alone, ie.
rails_project$ rspec spec/features/my_first_feature.rb
However the spec/features directory is totally ignored when I try to run the tests for the entire spec/ directory like this:
rails_project$ rspec .
There is no mention in the guide about how to include this directory (or other directory) to accomplish this inclusion that I desire. I really need this for continuous integration of my project.
Can anybody please tell me how to do it?
Thanks!
You need to make sure all of your specs end in_spec.rb.
Change the filename to spec/features/my_first_feature_spec.rb
This is how I do it:
rspec spec
You may also want to use guard-rspec, which gives you better control.

override --fail-fast in rspec

I run specs in my local machine and in a CI server. In the first case I want specs to fail fast, and in the other I want run all of them. I've set --fail-fast in my .rspec file. How can I override it on the CI server? I run there RSpec using rake spec and I set SPEC_OPTS env variable.
The simplest answer is to remove .rspec from your repo. It is used only by you and any other contributor don't need to see that you are using NyanCat to see how your test's are doing.
You can add a .rspec file in your home directory so that the settings only apply to your local machine, and then the .rspec in the project directory will override any settings there.
--no-fail-fast has since been added.
https://relishapp.com/rspec/rspec-core/docs/command-line/fail-fast-option#using-%60--no-fail-fast%60

Resources