covr
Test coverage for R packages.
Basic Usage
library(covr)
# Calculate coverage
cov <- package_coverage()
# View report
report(cov)
# Print summary
cov
Coverage Types
# Package coverage
package_coverage()
# File coverage
file_coverage("R/myfile.R", "tests/testthat/test-myfile.R")
# Function coverage
function_coverage("myfunction", test_file = "tests/test.R")
# Code coverage
code_coverage(source_code, test_code)
Coverage Report
# Interactive HTML report
report(cov)
# Zero coverage
zero_coverage(cov)
# Coverage by file
coverage_to_list(cov)
Codecov Integration
# Upload to Codecov
codecov(token = "YOUR_TOKEN")
# In GitHub Actions
# - uses: codecov/codecov-action@v3
Coveralls Integration
# Upload to Coveralls
coveralls(token = "YOUR_TOKEN")
Exclusions
# Exclude lines with comments
# nocov start
code_to_exclude()
# nocov end
# Single line exclusion
x <- 1 # nocov
Options
# Set options
options(covr.exclude_pattern = "# nocov")
options(covr.exclude_start = "# nocov start")
options(covr.exclude_end = "# nocov end")
CI Configuration
# .github/workflows/test-coverage.yaml
on: push
jobs:
test-coverage:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: r-lib/actions/setup-r@v2
- name: Test coverage
run: |
install.packages("covr")
covr::codecov()
shell: Rscript {0}
Percent Coverage
# Get percent
percent_coverage(cov)
# By expression type
tally_coverage(cov)