Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code coverage for rspec

Tags:

I am rather new to rails, and I am using rspec to write unit tests, integration tests and functional tests.

We want to integrate TDD in our team. While it is a great way to plan how to write functionality, for me it doesn't feel at all like a proper unit test, when it comes to code coverage.

I would like to have insight in the coverage of our unit tests. If I write a method, it should be tested in such a way that the following is ensured:

  • Every line of code is executed
  • Every outcome of boolean expressions (e.g. in if/unless statements) is produced during the test.

I want to be able to get insight in how my tests are doing in terms of these two requirements, i.e. to see my code and see what lines and expressions are being tested and which are not. Is there a tool / gem that you could recommend me that gives me this information?

like image 923
Pelle Avatar asked Feb 25 '14 12:02

Pelle


People also ask

How is code coverage calculated?

To calculate the code coverage percentage, simply use the following formula: Code Coverage Percentage = (Number of lines of code executed by a testing algorithm/Total number of lines of code in a system component) * 100.

What is SimpleCov?

Test coverage using SimpleCov SimpleCov is a code coverage analysis tool for Ruby. It uses Ruby's built-in Coverage library to gather code coverage data. Add simplecov to your Gemfile and run bundle install: 1gem 'simplecov', require: false, group: :test.

How do I run an RSpec on a specific file?

Running tests by their file or directory names is the most familiar way to run tests with RSpec. RSpec can take a file name or directory name and run the file or the contents of the directory. So you can do: rspec spec/jobs to run the tests found in the jobs directory.


1 Answers

This is exactly what you are looking for: https://github.com/colszowka/simplecov

like image 133
dennis Avatar answered Oct 20 '22 15:10

dennis