Using SimpleCov in a Ruby Project - ruby

I am trying to use the simplecov gem in a Ruby project. However I have failed miserably. Here is what I've done until now.
My project structure is:
ProjectFolder
- lib
- test
I have my tests in test and source code in lib. I have created a test_helper.rb in the test directory and added the following.
require 'simplecov'
SimpleCov.start
Then I put `require 'test/test_helper.rb' in every test file. What happened was it sometimes created some report, and sometimes didn't. And when it did it was inconsistent.
All the tutorials I found was for Rails so I turn to StackOverflow once more, to show me the way.

Rcov/SimpleCov will only report the coverage of the tests ran.
For a full coverage report, you must ensure that the full test suite is ran as the last test, to build a full coverage report.
You will also want to make sure it is the first require in your test_helper.rb file.
From the documentation:
Note: If SimpleCov starts after your application code is already
loaded (via require), it won't be able to track your files and their
coverage! The SimpleCov.start must be issued before any of your
application code is required!

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.

Configure Mocha Test Runner in Bamboo

I've configured and am executing mocha tests in WebStorm, so I know the module is working properly. But I can't seem to make it run from a Bamboo task. The task runs with Success but there are 0 tests executed.
This is my configuration atm:
app/ is my working dir. Tried also with app/node_modules/mocha/bin/ and other possibilities. I am not sure which exactly is the Mocha executable of all the mocha named files in the module...
Or maybe the problem lies in the tests dir? I've got test files, respectively in app/test/unit/models/ and app/test/unit/services/. But in WebStorm I configured it with the general test dir - just /app/test. Configuring the Mocha task in Bamboo with the specific test folders did not yield result...
I believe the problem comes from wrong directory configurations in the task, but I've tried writing whatever paths already and I've got no idea what's missing or wrong...
I noticed from your screenshot that the "Parse test results produced by this task" box isn't checked. This is what tells Bamboo to parse the output of the tests that you run.

RCOV underreporting when running the entire spec vs running individual spec file

I am using Rcov for code coverage. When I run the entire spec folder, it underreports the coverage as 9% for that particular file. (I am sure the code is getting covered). When I run only that particular spec file it reports the coverage as 98% for the same file.
Any idea as to why this might happen?

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.

Resources