R6 class for unit testing a component with code coverage tracking. The code runs in a separate R process (the test session), and the lines executed are counted.
Details
Test session
get_test_component() starts a new R process with 'callr', the test
session. Input data, the component code and its results all live in the
global environment of the test session. Your own R session and its global
environment are never modified.
The test session is closed with $close(), or when the object is garbage
collected. After that, $assign(), $get(), $ls() and $eval() raise
an error.
Workflow
Create the test component with
get_test_component().Assign input data in the test session with
$assign().Run the code in the test session and update coverage with
$eval().Retrieve results from the test session with
$get().Test the results with testthat
expect_*()functions.Close the test session with
$close().
By default, get_test_component() checks coverage with
$check_coverage() when the test ends. Coverage is kept in your R
session, so it can still be checked after $close().
Super classes
mighty_component -> mighty_component_rendered -> mighty_component_test
Active bindings
percent_coveragenumeric(1)Percentage of lines covered (0-100).line_coveragedata.framewith columnslineandvalue(number of times the line has run).
Methods
mighty_component_test$new()
Create a test component from a rendered template. Starts a new R session. Requires the callr and covr packages.
Usage
mighty_component_test$new(template, id)mighty_component_test$assign()
Assign a variable in the global environment of the test session. Your R session is not modified.
mighty_component_test$eval()
Run the code in the global environment of the test session and update the coverage.
mighty_component_test$check_coverage()
Check that every line of the code has run at least once. Raises an error otherwise.
mighty_component_test$close()
Close the test session. Called automatically when the object is garbage collected.
Examples
path <- system.file("examples", "ady.mustache", package = "mighty.component")
x <- get_test_component(
component = path,
params = list(domain = "adae", variable = "ADY", date = "ADT"),
check_coverage = FALSE
)
#> → Found "ady.mustache" in "local::/home/runner/work/_temp/Library/mighty.component/examples"
x$percent_coverage
#> [1] 0
x$assign(
"adae",
data.frame(TRTSDT = as.Date("2024-01-01"), ADT = as.Date("2024-01-05"))
)
x$eval()
x$ls()
#> [1] "adae"
x$get("adae")$ADY
#> [1] 5
x$percent_coverage
#> [1] 100
x$check_coverage()
x$close()
