Test Coverage for Packages

Track and report code coverage for your package and (optionally) upload the results to a coverage service like 'Codecov' ( http://codecov.io) or 'Coveralls' ( http://coveralls.io). Code coverage is a measure of the amount of code being exercised by a set of tests. It is an indirect measure of test quality and completeness. This package is compatible with any testing methodology or framework and tracks coverage of both R code and compiled C/C++/FORTRAN code.

Track test coverage for your R package and (optionally) upload the results to coveralls or codecov.


If you are already using Travis-CI or Appveyor CI add the following to your project's .travis.yml to track your coverage results over time with Codecov.

  - jimhester/covr
  - Rscript -e 'covr::codecov()'

To use a different CI service or call codecov() locally you can set the environment variable CODECOV_TOKEN to the token generated on codecov.io.

Codecov currently has support for the following CI systems (* denotes support without needing CODECOV_TOKEN).

You will also need to enable the repository on Codecov.

Alternatively you can upload your results to Coveralls using coveralls().

  - jimhester/covr
  - Rscript -e 'covr::coveralls()'

For CI systems not supported by coveralls you need to set the COVERALLS_TOKEN environment variable. It is wise to use a Secure Variable so that it is not revealed publicly.

Also you will need to turn on coveralls for your project at https://coveralls.io/repos.

Interactive Usage

A shiny Application can be used to view coverage per line.

cov <- package_coverage()

If used with type = "all", combine_types = FALSE the Shiny Application will allow you to interactively toggle between Test, Vignette and Example coverage.

cov <- package_coverage(type = "all", combine_types = FALSE)
# if your working directory is in the packages base directory
# or a package in another directory
cov <- package_coverage("lintr")
# view results as a data.frame
# zero_coverage() can be used to filter only uncovered lines.


covr supports a couple of different ways of excluding some or all of a file.

The function_exclusions argument to package_coverage() can be used to exclude functions by name. This argument takes a vector of regular expressions matching functions to exclude.

# exclude print functions
package_coverage(function_exclusions = "print\\.")
# exclude `.onLoad` function
package_coverage(function_exclusions = "\\.onLoad")

The line_exclusions argument to package_coverage() can be used to exclude some or all of a file. This argument takes a list of filenames or named ranges to exclude.

# exclude whole file of R/test.R
package_coverage(line_exclusions = "R/test.R")
# exclude lines 1 to 10 and 15 from R/test.R
package_coverage(line_exclusions = list("R/test.R" = c(1:10, 15)))
# exclude lines 1 to 10 from R/test.R, all of R/test2.R
package_coverage(line_exclusions = list("R/test.R" = c(1, 10), "R/test2.R"))

In addition you can exclude lines from the coverage by putting special comments in your source code.

This can be done per line.

f1 <- function(x) {
  x + 1 # nocov

Or by specifying a range with a start and end.

f2 <- function(x) { # nocov start
  x + 2
} # nocov end

The patterns used can be specified by setting the global options covr.exclude_pattern, covr.exclude_start, covr.exclude_end.


Covr should be compatible with any testing package, it uses tools::testInstalledPackage() to run your packages tests.

Covr will not work with icc, Intel's compiler as it does not have Gcov compatible output.

Covr is known to work with clang versions 3.5+ and gcc version 4.2+.

If the appropriate gcov version is not on your path you can set the appropriate location with the covr.gcov options. If you set this path to "" it will turn off coverage of compiled code.

options(covr.gcov = "path/to/gcov")

covr tracks test coverage by modifying a package's code to add tracking calls to each call.

The vignette vignettes/how_it_works.Rmd contains a detailed explanation of the technique and the rationale behind it.

You can view the vignette from within R using

vignette("how_it_works", package = "covr")

Because covr modifies the package code it is possible there are unknown edge cases where that modification affects the output. In addition when tracking coverage for compiled code covr compiles the package without optimization, which can modify behavior (usually due to package bugs which are masked with higher optimization levels).

Alternative Coverage Tools


  • Fix test broken with DT 0.2
  • Fix tests broken with updated htmlwidgets
  • Change report tab title based on filename (Chen Liang).
  • Add support for cobertura XML output (@wligtenberg).
  • Add mcparallel support by patching mcparallel:::mcexit() automatically for packages using parallel (#195, @kforner).
  • Add support for GitLab CI (#190, @enbrown).
  • Update exclusion documentation to include line_exclusions and function exclusions (#191).
  • Support coverage of R6 methods (#174).
  • Explicitly set default packages (including methods) (#183, #180)
  • Set R_LIBS and R_LIBS_SITE as well as R_LIBS_USER (#188).
  • Automatically exclude RcppExport files (#170).
  • Memoised and Vectorized functions now able to be tracked.
  • Support for filtering by function as well as line.
  • Now tracks coverage for RC methods
  • Rewrote loading and saving to support parallel code and tests including quit() calls.
  • Made passing code to function_coverage() and package_coverage() not use non-standard evaluation.
  • NULL statements are analyzed for coverage (#156, @krlmlr).
  • Finer coverage analysis for brace-less if, while and for statements (#154, @krlmlr).
  • Run any combination of coverage types (#104, #133)
  • Remove inconsistencies in line counts between shiny app and services (#129)
  • Include header files in gcov output (#112)
  • Add support for C++11 (#131)
  • Always clean gcov files even on failure (#108)
  • zero_coverage works with RStudio markers (#119)
  • Remove the devtools dependency
  • Set .libPaths() in subprocess to match those in calling process (#140, #147).
  • Move devtools dependency to suggests, only needed on windows
  • move htmltools to suggests

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.


2.2.2 by Jim Hester, 2 months ago


Report a bug at https://github.com/jimhester/covr/issues

Browse source code at https://github.com/cran/covr

Authors: Jim Hester [aut, cre], Willem Ligtenberg [ctb], Kirill Müller [ctb], Henrik Bengtsson [ctb], Steve Peak [ctb], Kirill Sevastyanenko [ctb], Jon Clayden [ctb], Robert Flight [ctb], Eric Brown [ctb], Brodie Gaslam [ctb], Will Beasley [ctb], Robert Krzyzanowski [ctb], Markus Wamser [ctb], Karl Forner [ctb], Gergely Daróczi [ctb], Jouni Helske [ctb], Kun Ren [ctb], Jeroen Ooms [ctb], Ken Williams [ctb], Chris Campbell [ctb]

Documentation:   PDF Manual  

MIT + file LICENSE license

Imports stats, utils, jsonlite, rex, httr, crayon, withr

Depends on methods

Suggests R6, knitr, rmarkdown, shiny, htmltools, htmlwidgets, DT, testthat, rstudioapi, devtools, xml2, parallel, memoise

Suggested by ABCoptim, ALA4R, BTYDplus, ClusterR, DBI, DataExplorer, EpiModel, FLightR, FTRLProximal, GGIR, GSODR, GeneralTree, HARtools, KernelKnn, Momocs, OpenImageR, RSauceLabs, RSelenium, SpaDES, Wmisc, aemo, after, analogsea, ashr, awsjavasdk, betalink, bgmfiles, binman, blob, bold, broom, brranching, bsplus, callr, cellranger, colorplaner, crul, cvequality, datastepr, debugme, dendextend, desc, descriptr, devtools, diffobj, discgolf, dplyr, dtplyr, elastic, enigma, errorlocate, fauxpas, fiery, filesstrings, finch, fmbasics, fmdates, foghorn, forcats, formattable, fuzzyjoin, geojson, geojsonlint, getCRUCLdata, getlandsat, gghalfnorm, ggplot2, goldi, googleAnalyticsR, googleAuthR, googleCloudStorageR, googleComputeEngineR, googlesheets, gtable, haven, incidence, inferr, isdparser, joineR, jqr, largeVis, lawn, lazyeval, lexRankr, lingtypology, listWithDefaults, liteq, lubridate, mapr, mcparallelDo, modelr, mregions, naptime, natserv, netdiffuseR, normalr, oai, odbc, openadds, optiRum, originr, outbreaks, owmr, pacotest, palr, pangaear, plyr, pmc, prettycode, primefactr, productplots, prof.tree, proto, ptstem, purrr, qwraps2, radiant, radiant.basics, radiant.data, radiant.model, radiant.multivariate, randgeo, ratelimitr, rbgm, rbhl, rbison, rcrossref, rdpla, readr, rematch, reprex, rgeospatialquality, rintrojs, ritis, rjsonapi, rnoaa, rnpn, rodham, roxygen2, rplos, rrecsys, rredlist, rslp, rsnps, rstack, rstantools, rvertnet, rvest, rvg, scales, seleniumPipes, semver, sigmoid, simmer, simmer.plot, sofa, solrium, spbabel, spdplyr, spex, spocc, stringr, svglite, taxize, templates, testthat, text2vec, textTinyR, textreuse, tidyr, tokenizers, toxboot, tracer, traits, trip, units, valr, vembedr, wdman, worrms, xml2, xmlparsedata, yesno.

See at CRAN