Learn R Programming

covr (version 4.0.0)

current_coverage: Coverage measured so far in the current process

Description

current_coverage() returns the coverage that instrumented code has recorded so far in the current R process. Call it from code that covr runs, such as the code of package_coverage() or with_coverage(), to measure the coverage of parts of a run, e.g. of each test file in turn.

Usage

current_coverage(reset = FALSE)

Value

A coverage object, with no traces if no code was instrumented.

Arguments

reset

If TRUE, set the counts of all traces to zero after reading them, so the next call only includes code run after this one. The coverage returned at the end of the run then also only includes code run after the last reset.

Details

Only the current process is included: code run in other processes, e.g. by parallel::mclapply(), is added to the coverage that covr returns at the end of the run, but not to that of current_coverage(). No exclusions are applied.

Examples

Run this code
env <- new.env()
code <- "f <- function(x) {
  if (x) {
    'yes'
  } else {
    'no'
  }
}"
eval(parse(text = code, keep.source = TRUE), env)
with_coverage(
  {
    env$f(TRUE)
    first <- current_coverage(reset = TRUE)
    env$f(FALSE)
    second <- current_coverage()
  },
  env = env
)
tally_coverage(first)
tally_coverage(second)

Run the code above in your browser using DataLab