Skip to contents

Builds a traceability matrix from coverage recorded with options(covr.record_tests = TRUE): which tests ran each line of code. Use it to find the tests to rerun after changing some lines, or the tests exercising a function.

Usage

tally_tests(x)

Arguments

x

a coverage object computed with options(covr.record_tests = TRUE).

Value

A data.frame with one row for each line and each test that ran it, and the columns:

  • filename, functions, line: the line of code, as in tally_coverage().

  • test_file, test_line: where the test call is. For package_coverage(), test files are relative to the package directory, e.g. tests/testthat/test-foo.R. NA if the test has no source references.

  • test: the description of the enclosing test_that() or it() call, or NA.

  • value: the number of times the test ran the line.

Details

Lines are those of tally_coverage() with by = "line", so blank lines, comments, lines with only punctuation and excluded lines are left out. A test is a top level call in a test file that ran package code, such as an expectation; see covr.record_tests.

Examples

cov <- withr::with_options(
  list(covr.record_tests = TRUE),
  code_coverage(
    "f <- function(x) {\n  if (x) 'yes' else 'no'\n}",
    "f(TRUE)\nf(FALSE)"
  )
)
tests <- tally_tests(cov)
tests
#>                               filename functions line            test_file
#> 1 /tmp/RtmpvpKgL4/source.R190e2dc7994c         f    2 ./test.R190e5201222d
#> 2 /tmp/RtmpvpKgL4/source.R190e2dc7994c         f    2 ./test.R190e5201222d
#>   test_line test value
#> 1         1 <NA>     1
#> 2         2 <NA>     1

# The tests that ran lines 1 to 2
unique(tests[tests$line %in% 1:2, c("test_file", "test_line")])
#>              test_file test_line
#> 1 ./test.R190e5201222d         1
#> 2 ./test.R190e5201222d         2