Learn R Programming

covr (version 4.0.0)

tally_tests: Tally which tests ran each line

Description

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)

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.

Arguments

x

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

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

Run this code
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

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

Run the code above in your browser using DataLab