|
| 1 | +## General Information |
| 2 | + |
| 3 | +As per the official documentation, `covered` is a modern code-coverage library for Ruby. |
| 4 | + |
| 5 | +Instead of relying only on Ruby’s built-in `Coverage` API, it combines **tracepoints** with |
| 6 | +static analysis from the `parser` gem so it can track _any_ Ruby that eventually gets executed |
| 7 | +(including code produced by `eval`, or templates such as ERB/ HAML that are compiled to Ruby). |
| 8 | +Because it knows which lines are _actually_ executable, the reported percentages are usually |
| 9 | +more accurate than classic line-count tools like SimpleCov. |
| 10 | + |
| 11 | +## Installation |
| 12 | + |
| 13 | +Add this line to your application's `Gemfile`: |
| 14 | + |
| 15 | +```ruby |
| 16 | +gem 'covered' |
| 17 | +``` |
| 18 | + |
| 19 | +## Configuration |
| 20 | + |
| 21 | +### Configure what you see |
| 22 | + |
| 23 | +Under the hood, `covered` uses a single environment variable `COVERAGE` to: |
| 24 | +1. turn the coverage tracking on/ off |
| 25 | +2. allow the user to select how much detail to be printed |
| 26 | + |
| 27 | +By default, when the `COVERAGE` is not specifically set anywhere, you will not see anything, and nothing will be stored during the runs. |
| 28 | + |
| 29 | +You can modify this behavior either by defining the environment variable or specifying it when running the tests command. Your choices of values are: |
| 30 | +1. `BriefSummary` - you see a brief summary showcasing the overall percentage of line coverage. |
| 31 | + - Ideally you would use this for quick feedback locally |
| 32 | + - You can also use this to set a threshold through Github Actions around merging rules in Pull Requests. |
| 33 | +2. `PartialSummary` - you see contextual snippets around missing lines |
| 34 | + - Ideally you would use this for quickly investigating missing coverage in specific files |
| 35 | + - You can also use this to set a threshold through Github Actions around merging rules in Pull Requests, and also deliver information about which lines are not tested to the developer. |
| 36 | +3. `FullSummary` - you see every line, both covered and uncovered, which may be overwhelming |
| 37 | + - Ideally you would use this when doing a deep dive that requires verbosity. |
| 38 | +4. `Quiet` - you do not see anything in the console but the coverage is saved internally for later usage |
| 39 | + - Ideally used in CI pipelines. |
| 40 | + |
| 41 | +### Configure file choices for coverage |
| 42 | + |
| 43 | +You can configure covered by creating a `config/covered.rb` file in your project. |
| 44 | + |
| 45 | +```ruby |
| 46 | +def ignore_paths |
| 47 | + super + ["engines/"] |
| 48 | +end |
| 49 | + |
| 50 | +def include_patterns |
| 51 | + super + ["bake/**/*.rb"] |
| 52 | +end |
| 53 | +``` |
| 54 | + |
| 55 | +1. `ignore_paths` specifies which paths to ignore when computing coverage for a given project. |
| 56 | +2. `include_patterns` specifies which paths to include when computing coverage for a given project. |
| 57 | + |
| 58 | +More information around the Configuration possibilities can be found here: https://socketry.github.io/covered/source/Covered/Config/index.html. |
| 59 | + |
| 60 | +One possibly helpful functionality to take note of is that you can override the `make_policy` method in order to implement your own policy. |
| 61 | +## Integration |
| 62 | + |
| 63 | +### Sus Integration |
| 64 | + |
| 65 | +In your `config/sus.rb` add the following: |
| 66 | + |
| 67 | +```ruby |
| 68 | +require 'covered/sus' |
| 69 | +include Covered::Sus |
| 70 | +``` |
| 71 | +### RSpec Integration |
| 72 | + |
| 73 | +In your `spec/spec_helper.rb` add the following before loading any other code: |
| 74 | + |
| 75 | +```ruby |
| 76 | +require 'covered/rspec' |
| 77 | +``` |
| 78 | + |
| 79 | +Ensure that you have a `.rspec` file with `--require spec_helper`: |
| 80 | + |
| 81 | +```plain |
| 82 | +--require spec_helper |
| 83 | +--format documentation |
| 84 | +--warnings |
| 85 | +``` |
| 86 | + |
| 87 | +### Minitest Integration |
| 88 | + |
| 89 | +In your `test/test_helper.rb` add the following before loading any other code: |
| 90 | + |
| 91 | +```ruby |
| 92 | +require 'covered/minitest' |
| 93 | +require 'minitest/autorun' |
| 94 | +``` |
| 95 | + |
| 96 | +In your test files, e.g. `test/dummy_test.rb` add the following at the top: |
| 97 | + |
| 98 | +```ruby |
| 99 | +require_relative 'test_helper' |
| 100 | +``` |
| 101 | + |
| 102 | + |
| 103 | +## Coverage Improvement |
| 104 | + |
| 105 | +The taxonomy of tests isn't really relevant for the purpose of improving the coverage and the safety of your codebase. |
| 106 | + |
| 107 | +You are going to think about tests by referring to the level of software processes that they test. When trying to improve coverage, you must first understand what the purpose of the on-going process is, with the scope of coverage in mind: |
| 108 | + 1. Verifying individual components - you are going to be writing Unit Tests. |
| 109 | + 2. Verifying interaction between multiple units - you are going to be writing Integration Tests. |
| 110 | + 3. Verifying end-to-end-workflows - you are going to be writing System Tests. |
0 commit comments