covr tracks and reports code coverage for your package and (optionally) upload the results to a coverage service like 'Codecov' https://about.codecov.io or 'Coveralls' https://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 and Rust code.
Details
A coverage report can be used to inspect coverage for each line in your
package. Using report() requires the additional dependency htmltools.
# If run with no arguments `report()` implicitly calls `package_coverage()`
report()Package options
covr uses the following options() to configure behaviour:
covr.branch_coverage: IfTRUE(defaultFALSE), also record which branches of eachifandswitch()in R code were taken. This is experimental. It must be set before the code is instrumented, e.g. before callingpackage_coverage(). For more details, see?covr.branch_coverage.covr.covrignore: A filename to use as an ignore file, listing glob-style wildcarded paths of files to ignore for coverage calculations. Defaults to the value of the environment variableCOVR_COVRIGNORE, or".covrignore"if neither the option nor the environment variable is set.covr.exclude_end: Used along withcovr.exclude_start, an optional regular expression which ends a line-exclusion region. For more details, see?exclusions.covr.exclude_pattern: An optional line-exclusion pattern. Lines which match the pattern will be excluded from coverage. For more details, see?exclusions.covr.exclude_start: Used along withcovr.exclude_end, an optional regular expression which starts a line-exclusion region. For more details, see?exclusions.covr.filter_non_package: IfTRUE(the default behavior), coverage of files outside the target package are filtered from coverage output.covr.failure_lines: The number of trailing lines of test output shown in the message andprint()method of acovr_errorwhen tests, vignettes, examples orcodefail (default50). The full output is always available in the condition's$outputfield.covr.allow_failures: IfTRUE(defaultFALSE), failures or errors when running the tests, vignettes, examples orcodeofpackage_coverage()are reported as warnings and coverage is still returned, the default of itsallow_failuresargument.covr.annotate_max_lines: The maximum number of source lines shown byannotate_coverage()(default200), the default of itsmax_linesargument.Infshows everything.covr.print_max_ranges: The maximum number of missed ranges, and of missed branches, listed per file or function byprint(x, missing = TRUE)(default10). More are summarised as... (<k> more).Inflists them all.covr.fix_parallel_mcexit: Whether to include code run in processes forked by parallel'smclapply(),mcparallel()and related functions, by patchingparallel:::mcexit. By defaultpackage_coverage()does this if the package uses parallel, and the functions that measure coverage in the current session, such asfile_coverage(), always do. Can also be set with the environment variableCOVR_FIX_PARALLEL_MCEXIT. Not supported on Windows.covr.flags: A named character vector of the compiler and linker settings (the values of aMakevarsfile, e.g.CFLAGS) that are added whenpackage_coverage()installs a package, orfile_coverage()builds one, for coverage of compiled code. The default, set when covr is loaded, compiles C, C++ and Fortran code without optimization and with--coverage("-O0 --coverage") and links it to gcov (FLIBSis"-lgcov",LDFLAGSor on WindowsSHLIB_LIBADDis"--coverage").covr.gcov: Path to the gcov executable. Defaults to the gcov found on yourPATH, orNAif there is none. If the appropriate gcov version is not on your path you can use this option to set the appropriate location. If set to""it will turn off coverage of C, C++ and Fortran code, but not of Rust code, which is controlled bycovr.llvm_cov. IfNA, covr throws an error if any compiled coverage files (.gcno) exist.covr.gcov_additional_paths: A character vector of directories, in addition to the package itself, whose compiled source files (C, C++, Fortran or Rust) are accepted in the coverage, e.g. the code of another package or library that is compiled into the package from outside of itssrc/directory. Files elsewhere are left out. DefaultNULL.covr.gcov_args: A character vector of additional command line arguments passed to gcov. DefaultNULL.covr.gcov_partial: How to count compiled code lines that gcov reports as only partially executed (e.g.if (x) f();whenf()never runs)."covered"(the default) counts them as covered,"uncovered"counts them as not covered, for requiring every part of a line to run. This works with both gcc's gcov and llvm-cov (the gcov on macOS). llvm-cov can't tell apart code that only runs when a C++ exception is thrown, so lines that create objects with destructors may also count as partially executed.covr.llvm_cov: Path to thellvm-covused to read the coverage of Rust code. Defaults to the one of the Rust toolchain, installed withrustup component add llvm-tools, or for Rust installed without rustup (e.g. withapt-get install cargo rustc), to the one on thePATHwith the same LLVM version as rustc, e.g.llvm-cov-18. If set to""the Rust code isn't instrumented, which turns off its coverage. This is independent ofcovr.gcov, which only controls C, C++ and Fortran code.covr.llvm_profdata: Path to thellvm-profdataused to merge the coverage counts of Rust code. Found likecovr.llvm_cov.covr.report_colors: A named character vector with the colors of covered and missed lines inreport()andfile_report(), and of the coverage percentages printed in the console, e.g.c(covered = "#0072B2", missed = "#D55E00"). colorblind_palette has colors that are easier to tell apart than the default green and red with color vision deficiency. Either element can be left out to keep its default. An optionalpartialelement sets the color of partial coverage (at least 75% but less than 90%), which otherwise followscovered, or is yellow in the console ifcoveredisn't set either, and of lines with a branch that wasn't taken in reports with branch coverage, which are otherwise yellow. Transparency is ignored. In terminals with only 8 colors, the console uses the closest of those colors.covr.rstudio_source_markers: A logical value. IfTRUE(the default behavior), source markers are displayed within the RStudio IDE when usingzero_coverage.covr.record_tests: IfTRUE(defaultFALSE), record a listing of top level test expressions and associate tests withcovrtraces evaluated during the test's execution. For more details, see?covr.record_tests.covr.full_traversal: IfTRUE(defaultFALSE), also trace functions nested inside lists and environments, such asfns <- list(a = function(x) x)or functions defined insidelocal(). This walks every object in the package, so it is off by default. Forpackage_coverage()it can also be enabled for a package by addingConfig/covr/full_traversal: trueto its DESCRIPTION; the option takes precedence when set.covr.install_instrument: IfFALSE(defaultTRUE),package_coverage()installs a package as usual and instruments it each time a process loads it, rather than once after installing it. Can also be set with the environment variableCOVR_INSTALL_INSTRUMENT.
Some options can also be set with an environment variable. They are read in
this order: the option, then the environment variable (ignored if it is
empty), then the default. So an option set in R always takes precedence over
the environment variable, e.g. COVR_FIX_PARALLEL_MCEXIT=false has no
effect if options(covr.fix_parallel_mcexit = TRUE) is set. Logical
settings are TRUE or FALSE, or for the environment variable (and
options set to a string), one of true, t, yes or 1, and false,
f, no or 0, in any case. Other values are ignored with a warning.
Author
Maintainer: Jim Hester james.f.hester@gmail.com
Authors:
Jim Hester james.f.hester@gmail.com
Other contributors:
Willem Ligtenberg [contributor]
Kirill Müller [contributor]
Henrik Bengtsson [contributor]
Steve Peak [contributor]
Kirill Sevastyanenko [contributor]
Jon Clayden [contributor]
Robert Flight [contributor]
Eric Brown [contributor]
Brodie Gaslam [contributor]
Will Beasley [contributor]
Robert Krzyzanowski [contributor]
Markus Wamser [contributor]
Karl Forner [contributor]
Gergely Daróczi [contributor]
Jouni Helske [contributor]
Kun Ren [contributor]
Jeroen Ooms [contributor]
Ken Williams [contributor]
Chris Campbell [contributor]
David Hugh-Jones [contributor]
Qin Wang [contributor]
Doug Kelkhoff [contributor]
Ivan Sagalaev (highlight.js library) [contributor, copyright holder]
