### Run package examples as tests with test_examples() Source: https://github.com/r-lib/testthat/blob/main/NEWS.md Use test_examples() to execute the examples provided in a package's documentation as tests. Each example is treated as a single expectation, succeeding if the code runs without errors. ```R test_examples("pkg", "pkg.R") ``` -------------------------------- ### Install testthat Source: https://github.com/r-lib/testthat/blob/main/README.md Commands to install the package from CRAN or the development version from GitHub. ```r # Install the released version from CRAN install.packages("testthat") # Or the development version from GitHub: # install.packages("pak") pak::pak("r-lib/testthat") ``` -------------------------------- ### Installation failure log Source: https://github.com/r-lib/testthat/blob/main/revdep/failures.md Log output indicating a failure during package installation. ```text Installation failed. See ‘/tmp/workdir/bayesdfa/new/bayesdfa.Rcheck/00install.out’ for details. ``` ```text Installation failed. See ‘/tmp/workdir/caugi/new/caugi.Rcheck/00install.out’ for details. ``` -------------------------------- ### View installation error log Source: https://github.com/r-lib/testthat/blob/main/revdep/failures.md Displays the location of the installation output file for troubleshooting failed package builds. ```text Installation failed. See ‘/tmp/workdir/gpboost/new/gpboost.Rcheck/00install.out’ for details. ``` -------------------------------- ### SQLFormatteR Installation Error Source: https://github.com/r-lib/testthat/blob/main/revdep/failures.md Generic installation failure message pointing to an external log file. ```text Installation failed. See ‘/tmp/workdir/SQLFormatteR/new/SQLFormatteR.Rcheck/00install.out’ for details. ``` -------------------------------- ### Include test environment setup Source: https://github.com/r-lib/testthat/blob/main/tests/testthat/_snaps/extract.md Extracts test lines including the environment setup block. ```R base::writeLines(extract_test_lines(exprs, 2, "test")) ``` -------------------------------- ### Package installation error (caugi) Source: https://github.com/r-lib/testthat/blob/main/revdep/failures.md Error log showing an unsupported Rust version during installation. ```text * installing *source* package ‘caugi’ ... ** this is package ‘caugi’ version ‘0.3.1’ ** package ‘caugi’ successfully unpacked and MD5 sums checked ** using staged installation Error in eval(ei, envir) : ------------------ [UNSUPPORTED RUST VERSION]------------------ - Minimum supported Rust version is 1.80.0. - Installed Rust version is 1.75.0. --------------------------------------------------------------- Calls: source -> withVisible -> eval -> eval Execution halted ERROR: configuration failed for package ‘caugi’ * removing ‘/tmp/workdir/caugi/new/caugi.Rcheck/caugi’ ``` ```text * installing *source* package ‘caugi’ ... ** this is package ‘caugi’ version ‘0.3.1’ ** package ‘caugi’ successfully unpacked and MD5 sums checked ** using staged installation Error in eval(ei, envir) : ------------------ [UNSUPPORTED RUST VERSION]------------------ - Minimum supported Rust version is 1.80.0. - Installed Rust version is 1.75.0. --------------------------------------------------------------- Calls: source -> withVisible -> eval -> eval Execution halted ERROR: configuration failed for package ‘caugi’ * removing ‘/tmp/workdir/caugi/old/caugi.Rcheck/caugi’ ``` -------------------------------- ### Skip if Package Not Installed Source: https://github.com/r-lib/testthat/blob/main/tests/testthat/_snaps/skip.md Illustrates skipping tests if a package is not installed or if the installed version does not meet the requirement. ```R skip_if_not_installed("{doesntexist}") ``` ```R skip_if_not_installed("testthat", "9999.9999.999") ``` -------------------------------- ### Test Examples Functionality in testthat Source: https://github.com/r-lib/testthat/blob/main/tests/testthat/_snaps/examples.md Demonstrates the expected error when `test_examples()` is called with a non-existent example. This function is used to check if examples in R code can be found and executed. ```R test_examples("asdf") ``` ```R Error in `test_examples()`: ! Could not find examples. ``` -------------------------------- ### Galvanizer Package Installation Error Source: https://github.com/r-lib/testthat/blob/main/revdep/failures.md Installation failure due to a missing exported object in the purrr namespace. ```text * installing *source* package ‘galvanizer’ ... ** this is package ‘galvanizer’ version ‘0.5.3’ ** package ‘galvanizer’ successfully unpacked and MD5 sums checked ** using staged installation ** R ** byte-compile and prepare package for lazy loading Error: object ‘at_depth’ is not exported by 'namespace:purrr' Execution halted ERROR: lazy loading failed for package ‘galvanizer’ * removing ‘/tmp/workdir/galvanizer/new/galvanizer.Rcheck/galvanizer’ ``` ```text * installing *source* package ‘galvanizer’ ... ** this is package ‘galvanizer’ version ‘0.5.3’ ** package ‘galvanizer’ successfully unpacked and MD5 sums checked ** using staged installation ** R ** byte-compile and prepare package for lazy loading Error: object ‘at_depth’ is not exported by 'namespace:purrr' Execution halted ERROR: lazy loading failed for package ‘galvanizer’ * removing ‘/tmp/workdir/galvanizer/old/galvanizer.Rcheck/galvanizer’ ``` -------------------------------- ### Check package installation status Source: https://github.com/r-lib/testthat/blob/main/revdep/failures.md Use this command to retrieve detailed information about the installation status of a specific package. ```R revdepcheck::cloud_details(, "gpboost") ``` -------------------------------- ### Package installation error (bayesdfa) Source: https://github.com/r-lib/testthat/blob/main/revdep/failures.md Error log showing a missing 'rstantools' dependency during installation. ```text * installing *source* package ‘bayesdfa’ ... ** this is package ‘bayesdfa’ version ‘1.3.4’ ** package ‘bayesdfa’ successfully unpacked and MD5 sums checked ** using staged installation Error in loadNamespace(x) : there is no package called ‘rstantools’ Calls: loadNamespace -> withRestarts -> withOneRestart -> doWithOneRestart Execution halted ERROR: configuration failed for package ‘bayesdfa’ * removing ‘/tmp/workdir/bayesdfa/new/bayesdfa.Rcheck/bayesdfa’ ``` ```text * installing *source* package ‘bayesdfa’ ... ** this is package ‘bayesdfa’ version ‘1.3.4’ ** package ‘bayesdfa’ successfully unpacked and MD5 sums checked ** using staged installation Error in loadNamespace(x) : there is no package called ‘rstantools’ Calls: loadNamespace -> withRestarts -> withOneRestart -> doWithOneRestart Execution halted ERROR: configuration failed for package ‘bayesdfa’ * removing ‘/tmp/workdir/bayesdfa/old/bayesdfa.Rcheck/bayesdfa’ ``` -------------------------------- ### tidyjson Installation Error Source: https://github.com/r-lib/testthat/blob/main/revdep/failures.md Generic installation failure message pointing to an external log file. ```text Installation failed. See ‘/tmp/workdir/tidyjson/new/tidyjson.Rcheck/00install.out’ for details. ``` -------------------------------- ### Installation error log Source: https://github.com/r-lib/testthat/blob/main/revdep/failures.md Log output indicating a failure to install the ctsem package. ```text Installation failed. See ‘/tmp/workdir/ctsem/new/ctsem.Rcheck/00install.out’ for details. ``` -------------------------------- ### Gllvm Installation Status Source: https://github.com/r-lib/testthat/blob/main/revdep/failures.md Generic installation failure notification for the gllvm package. ```text Installation failed. See ‘/tmp/workdir/gllvm/new/gllvm.Rcheck/00install.out’ for details. ``` -------------------------------- ### R Package Installation Check Failure Source: https://github.com/r-lib/testthat/blob/main/revdep/failures.md This message indicates that a package failed the initial installation check. The specific reason for failure is detailed in the installation logs, such as the '00install.out' file. ```R Installation failed. See ‘/tmp/workdir/fio/new/fio.Rcheck/00install.out’ for details. ``` ```R Installation failed. See ‘/tmp/workdir/gadjid/new/gadjid.Rcheck/00install.out’ for details. ``` ```R Installation failed. See ‘/tmp/workdir/galvanizer/new/galvanizer.Rcheck/00install.out’ for details. ``` -------------------------------- ### Devel installation log Source: https://github.com/r-lib/testthat/blob/main/revdep/failures.md Detailed compilation log showing a fatal error during the installation of the development version of ctsem. ```text * installing *source* package ‘ctsem’ ... ** this is package ‘ctsem’ version ‘3.10.4’ ** package ‘ctsem’ successfully unpacked and MD5 sums checked ** using staged installation ** libs using C++ compiler: ‘g++ (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0’ using C++17 g++ -std=gnu++17 -I"/opt/R/4.5.1/lib/R/include" -DNDEBUG -I"../inst/include" -I"/usr/local/lib/R/site-library/StanHeaders/include/src" -DBOOST_DISABLE_ASSERTS -DEIGEN_NO_DEBUG -DBOOST_MATH_OVERFLOW_ERROR_POLICY=errno_on_error -DUSE_STANC3 -D_HAS_AUTO_PTR_ETC=0 -I'/usr/local/lib/R/site-library/BH/include' -I'/usr/local/lib/R/site-library/Rcpp/include' -I'/usr/local/lib/R/site-library/RcppEigen/include' -I'/usr/local/lib/R/site-library/RcppParallel/include' -I'/usr/local/lib/R/site-library/rstan/include' -I'/usr/local/lib/R/site-library/StanHeaders/include' -I/usr/local/include -I'/usr/local/lib/R/site-library/RcppParallel/include' -D_REENTRANT -DSTAN_THREADS -fpic -g -O2 -c RcppExports.cpp -o RcppExports.o ... /usr/local/lib/R/site-library/StanHeaders/include/src/stan/mcmc/hmc/hamiltonians/dense_e_metric.hpp:22:0: required from ‘double stan::mcmc::dense_e_metric::T(stan::mcmc::dense_e_point&) [with Model = model_ctsm_namespace::model_ctsm; BaseRNG = boost::random::additive_combine_engine, boost::random::linear_congruential_engine >]’ /usr/local/lib/R/site-library/StanHeaders/include/src/stan/mcmc/hmc/hamiltonians/dense_e_metric.hpp:21:0: required from here /usr/local/lib/R/site-library/RcppEigen/include/Eigen/src/Core/DenseCoeffsBase.h:654:74: warning: ignoring attributes on template argument ‘Eigen::internal::packet_traits::type’ {aka ‘__m128d’} [-Wignored-attributes] 654 | return internal::first_aligned::alignment),Derived>(m); | ^~~~~~~~~ g++: fatal error: Killed signal terminated program cc1plus compilation terminated. make: *** [/opt/R/4.5.1/lib/R/etc/Makeconf:209: stanExports_ctsm.o] Error 1 ERROR: compilation failed for package ‘ctsem’ * removing ‘/tmp/workdir/ctsem/new/ctsem.Rcheck/ctsem’ ``` -------------------------------- ### SQLFormatteR Rust Version Error Source: https://github.com/r-lib/testthat/blob/main/revdep/failures.md Installation failure caused by an incompatible Rust compiler version. ```text * installing *source* package ‘SQLFormatteR’ ... ** this is package ‘SQLFormatteR’ version ‘0.0.2’ ** package ‘SQLFormatteR’ successfully unpacked and MD5 sums checked ** using staged installation Error in eval(ei, envir) : ------------------ [UNSUPPORTED RUST VERSION]------------------ - Minimum supported Rust version is 1.78.0. - Installed Rust version is 1.75.0. --------------------------------------------------------------- Calls: source -> withVisible -> eval -> eval Execution halted ERROR: configuration failed for package ‘SQLFormatteR’ * removing ‘/tmp/workdir/SQLFormatteR/new/SQLFormatteR.Rcheck/SQLFormatteR’ ``` ```text * installing *source* package ‘SQLFormatteR’ ... ** this is package ‘SQLFormatteR’ version ‘0.0.2’ ** package ‘SQLFormatteR’ successfully unpacked and MD5 sums checked ** using staged installation Error in eval(ei, envir) : ------------------ [UNSUPPORTED RUST VERSION]------------------ - Minimum supported Rust version is 1.78.0. - Installed Rust version is 1.75.0. --------------------------------------------------------------- Calls: source -> withVisible -> eval -> eval Execution halted ERROR: configuration failed for package ‘SQLFormatteR’ * removing ‘/tmp/workdir/SQLFormatteR/old/SQLFormatteR.Rcheck/SQLFormatteR’ ``` -------------------------------- ### SpaDES.tools Compilation Log Source: https://github.com/r-lib/testthat/blob/main/revdep/failures.md Log output showing a failed lazy loading process due to a missing object during package installation. ```text * installing *source* package ‘SpaDES.tools’ ... ** this is package ‘SpaDES.tools’ version ‘2.0.9’ ** package ‘SpaDES.tools’ successfully unpacked and MD5 sums checked ** using staged installation ** libs using C++ compiler: ‘g++ (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0’ g++ -std=gnu++17 -I"/opt/R/4.5.1/lib/R/include" -DNDEBUG -I'/usr/local/lib/R/site-library/Rcpp/include' -I/usr/local/include -fpic -g -O2 -c RcppExports.cpp -o RcppExports.o g++ -std=gnu++17 -I"/opt/R/4.5.1/lib/R/include" -DNDEBUG -I'/usr/local/lib/R/site-library/Rcpp/include' -I/usr/local/include -fpic -g -O2 -c duplicated.cpp -o duplicated.o g++ -std=gnu++17 -I"/opt/R/4.5.1/lib/R/include" -DNDEBUG -I'/usr/local/lib/R/site-library/Rcpp/include' -I/usr/local/include -fpic -g -O2 -c runif.cpp -o runif.o g++ -std=gnu++17 -shared -L/opt/R/4.5.1/lib/R/lib -L/usr/local/lib -o SpaDES.tools.so RcppExports.o duplicated.o runif.o -L/opt/R/4.5.1/lib/R/lib -lR ... installing to /tmp/workdir/SpaDES.tools/old/SpaDES.tools.Rcheck/00LOCK-SpaDES.tools/00new/SpaDES.tools/libs ** R ** inst ** byte-compile and prepare package for lazy loading Error in get(x, envir = ns, inherits = FALSE) : object 'isGridded' not found Error: unable to load R code in package ‘SpaDES.tools’ Execution halted ERROR: lazy loading failed for package ‘SpaDES.tools’ * removing ‘/tmp/workdir/SpaDES.tools/old/SpaDES.tools.Rcheck/SpaDES.tools’ ``` -------------------------------- ### CRAN installation log Source: https://github.com/r-lib/testthat/blob/main/revdep/failures.md Detailed compilation log showing a fatal error during the installation of the CRAN version of ctsem. ```text * installing *source* package ‘ctsem’ ... ** this is package ‘ctsem’ version ‘3.10.4’ ** package ‘ctsem’ successfully unpacked and MD5 sums checked ** using staged installation ** libs using C++ compiler: ‘g++ (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0’ using C++17 g++ -std=gnu++17 -I"/opt/R/4.5.1/lib/R/include" -DNDEBUG -I"../inst/include" -I"/usr/local/lib/R/site-library/StanHeaders/include/src" -DBOOST_DISABLE_ASSERTS -DEIGEN_NO_DEBUG -DBOOST_MATH_OVERFLOW_ERROR_POLICY=errno_on_error -DUSE_STANC3 -D_HAS_AUTO_PTR_ETC=0 -I'/usr/local/lib/R/site-library/BH/include' -I'/usr/local/lib/R/site-library/Rcpp/include' -I'/usr/local/lib/R/site-library/RcppEigen/include' -I'/usr/local/lib/R/site-library/RcppParallel/include' -I'/usr/local/lib/R/site-library/rstan/include' -I'/usr/local/lib/R/site-library/StanHeaders/include' -I/usr/local/include -I'/usr/local/lib/R/site-library/RcppParallel/include' -D_REENTRANT -DSTAN_THREADS -fpic -g -O2 -c RcppExports.cpp -o RcppExports.o ... /usr/local/lib/R/site-library/StanHeaders/include/src/stan/mcmc/hmc/hamiltonians/dense_e_metric.hpp:22:0: required from ‘double stan::mcmc::dense_e_metric::T(stan::mcmc::dense_e_point&) [with Model = model_ctsm_namespace::model_ctsm; BaseRNG = boost::random::additive_combine_engine, boost::random::linear_congruential_engine >]’ /usr/local/lib/R/site-library/StanHeaders/include/src/stan/mcmc/hmc/hamiltonians/dense_e_metric.hpp:21:0: required from here /usr/local/lib/R/site-library/RcppEigen/include/Eigen/src/Core/DenseCoeffsBase.h:654:74: warning: ignoring attributes on template argument ‘Eigen::internal::packet_traits::type’ {aka ‘__m128d’} [-Wignored-attributes] 654 | return internal::first_aligned::alignment),Derived>(m); | ^~~~~~~~~ g++: fatal error: Killed signal terminated program cc1plus compilation terminated. make: *** [/opt/R/4.5.1/lib/R/etc/Makeconf:209: stanExports_ctsm.o] Error 1 ERROR: compilation failed for package ‘ctsem’ * removing ‘/tmp/workdir/ctsem/old/ctsem.Rcheck/ctsem’ ``` -------------------------------- ### Verify output with headers using verify_output() Source: https://github.com/r-lib/testthat/blob/main/NEWS.md Use `verify_output()` to test human-readable output. Insert headers into output files by including a string starting with `"# "`, similar to Markdown. ```R verify_output(output_file, { # Header print("Some output") }) ``` -------------------------------- ### Expect equal with differing list structures Source: https://github.com/r-lib/testthat/blob/main/tests/testthat/_snaps/expect-equality.md This example demonstrates how expect_equal provides detailed feedback on differences in list structures, including lengths and element types. ```R expect_equal(list(a = 1), list(a = "b", b = 10)) ``` -------------------------------- ### Handle Missing Test Directory with test_path Source: https://github.com/r-lib/testthat/blob/main/tests/testthat/_snaps/test-path.md Use `test_path()` to locate test files. This example shows the error generated when the 'tests/testthat' directory cannot be found. ```R test_path("empty") ``` -------------------------------- ### Gllvm Compilation Failure Source: https://github.com/r-lib/testthat/blob/main/revdep/failures.md C++ compilation error log during gllvm package installation. ```text * installing *source* package ‘gllvm’ ... ** this is package ‘gllvm’ version ‘2.0.5’ ** package ‘gllvm’ successfully unpacked and MD5 sums checked ** using staged installation ** libs using C++ compiler: ‘g++ (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0’ g++ -std=gnu++17 -I"/opt/R/4.5.1/lib/R/include" -DNDEBUG -DTMBAD_FRAMEWORK -I'/usr/local/lib/R/site-library/TMB/include' -I'/usr/local/lib/R/site-library/RcppEigen/include' -I/usr/local/include -fopenmp -fpic -g -O2 -c gllvm.cpp -o gllvm.o In file included from /usr/local/lib/R/site-library/RcppEigen/include/Eigen/Core:205, from /usr/local/lib/R/site-library/RcppEigen/include/Eigen/Dense:1, from /usr/local/lib/R/site-library/TMB/include/TMB.hpp:92, ... /usr/local/lib/R/site-library/TMB/include/tiny_ad/atomic.hpp:30:1: required from ‘void atomic::bessel_kOp::reverse(TMBad::ReverseArgs&) [with Type = double; int order = 3; int ninput = 2; int noutput = 8; long int mask = 9]’ /usr/local/lib/R/site-library/TMB/include/TMBad/global.hpp:1762:28: required from ‘void TMBad::global::AddForwardMarkReverseMark::reverse(TMBad::ReverseArgs&) [with Type = double; OperatorBase = TMBad::global::AddIncrementDecrement > > >]’ /usr/local/lib/R/site-library/TMB/include/TMBad/global.hpp:2155:57: required from ‘void TMBad::global::Complete::reverse(TMBad::ReverseArgs&) [with OperatorBase = atomic::bessel_kOp<3, 2, 8, 9>]’ /usr/local/lib/R/site-library/TMB/include/TMBad/global.hpp:2155:10: required from here /usr/local/lib/R/site-library/RcppEigen/include/Eigen/src/Core/DenseCoeffsBase.h:56:30: warning: ignoring attributes on template argument ‘Eigen::internal::packet_traits::type’ {aka ‘__m128d’} [-Wignored-attributes] g++: fatal error: Killed signal terminated program cc1plus compilation terminated. make: *** [/opt/R/4.5.1/lib/R/etc/Makeconf:211: gllvm.o] Error 1 ERROR: compilation failed for package ‘gllvm’ * removing ‘/tmp/workdir/gllvm/new/gllvm.Rcheck/gllvm’ ``` -------------------------------- ### Show failure details with show_failure Source: https://github.com/r-lib/testthat/blob/main/tests/testthat/_snaps/expect-self-test.md The show_failure function displays detailed information about a failed expectation. This example demonstrates its output for a failed expect_true. ```R show_failure(expect_true(FALSE)) ``` -------------------------------- ### tidyjson Lazy Loading Failure Source: https://github.com/r-lib/testthat/blob/main/revdep/failures.md Installation failure due to a missing exported object from the purrr namespace. ```text * installing *source* package ‘tidyjson’ ... ** this is package ‘tidyjson’ version ‘0.3.2’ ** package ‘tidyjson’ successfully unpacked and MD5 sums checked ** using staged installation ** R ** data *** moving datasets to lazyload DB ** inst ** byte-compile and prepare package for lazy loading Error: object ‘at_depth’ is not exported by 'namespace:purrr' Execution halted ERROR: lazy loading failed for package ‘tidyjson’ * removing ‘/tmp/workdir/tidyjson/new/tidyjson.Rcheck/tidyjson’ ``` ```text * installing *source* package ‘tidyjson’ ... ** this is package ‘tidyjson’ version ‘0.3.2’ ** package ‘tidyjson’ successfully unpacked and MD5 sums checked ** using staged installation ** R ** data *** moving datasets to lazyload DB ** inst ** byte-compile and prepare package for lazy loading Error: object ‘at_depth’ is not exported by 'namespace:purrr' Execution halted ERROR: lazy loading failed for package ‘tidyjson’ * removing ‘/tmp/workdir/tidyjson/old/tidyjson.Rcheck/tidyjson’ ``` -------------------------------- ### Expect equal with attribute differences Source: https://github.com/r-lib/testthat/blob/main/tests/testthat/_snaps/expect-equality.md This example shows expect_equal reporting differences in attributes when comparing objects, indicating that while the base values might be equal, their metadata differs. ```R expect_equal(oops, 0) ``` -------------------------------- ### Failure messages for expect_setequal Source: https://github.com/r-lib/testthat/blob/main/tests/testthat/_snaps/expect-setequal.md Examples of error messages generated when sets do not match. ```R expect_setequal("actual", "expected") ``` ```R expect_setequal(x, y) ``` ```R expect_setequal(x, y) ``` ```R expect_setequal(x, y) ``` ```R expect_setequal(x, y) ``` ```R expect_setequal(x, c("a", "b", "c", "d")) ``` -------------------------------- ### Configuring devtools::test() in DESCRIPTION Source: https://github.com/r-lib/testthat/blob/main/NEWS.md Configure the implicit `devtools::load_all()` call within `devtools::test()` by adding a `Config/testthat/load-all` entry to your package's DESCRIPTION file. This example disables the export of internal functions and testthat helpers. ```R Config/testthat/load-all: list(export_all = FALSE, helpers = FALSE) ``` -------------------------------- ### Informative Snapshot Hint Source: https://github.com/r-lib/testthat/blob/main/tests/testthat/_snaps/snapshot.md Provides a helpful hint message when snapshot changes occur, guiding the user on how to accept or review the changes. ```R Code snapshot_hint("bar.R", reset_output = FALSE) Output * Run `testthat::snapshot_accept("bar.R")` to accept the change. * Run `testthat::snapshot_review("bar.R")` to review the change. ``` -------------------------------- ### Load testthat and mggd Packages Source: https://github.com/r-lib/testthat/blob/main/revdep/problems.md Loads the 'testthat' and 'mggd' R packages. A message indicates that 'mggd' will no longer be maintained and suggests installing 'multvardiv' instead. The 'test_check' function is then called. ```R library(testthat) library(mggd) The multvardiv package contains the same tools as mggd, and similar tools for other multivariate distributions. mggd will no longer be maintained. You may wish to install multvardiv instead. test_check("mggd") Killed ``` -------------------------------- ### Validate ignore.case input Source: https://github.com/r-lib/testthat/blob/main/tests/testthat/_snaps/expect-named.md This example demonstrates that the 'ignore.case' argument in expect_named must be TRUE or FALSE. Providing a string like 'yes' will result in an error. ```r expect_named(c(a = 1), "a", ignore.case = "yes") ``` -------------------------------- ### Verify specific name with order sensitivity Source: https://github.com/r-lib/testthat/blob/main/tests/testthat/_snaps/expect-named.md Use expect_named(x2, "a", ignore.order = FALSE) to check if 'x2' has the name "a" in the first position. This example shows feedback when the actual names do not match the expected order. ```r expect_named(x2, "a", ignore.order = FALSE) ``` -------------------------------- ### Validate ignore.order input Source: https://github.com/r-lib/testthat/blob/main/tests/testthat/_snaps/expect-named.md This example shows that the 'ignore.order' argument in expect_named must be TRUE or FALSE. Providing a string like 'yes' will result in an error. ```r expect_named(c(a = 1), "a", ignore.order = "yes") ``` -------------------------------- ### Validate `ok` argument in expect() Source: https://github.com/r-lib/testthat/blob/main/tests/testthat/_snaps/expectation.md The `ok` argument in `expect()` must be TRUE or FALSE. This example shows the error when a number is provided. ```R expect(1) ``` -------------------------------- ### Skip tests if a package is not installed with skip_if_not_installed() Source: https://github.com/r-lib/testthat/blob/main/NEWS.md Use skip_if_not_installed() to prevent tests from running if a specified R package is not available. This ensures tests only execute when their dependencies are met. ```R skip_if_not_installed("some_package") ``` -------------------------------- ### Test expect_success with success and failure Source: https://github.com/r-lib/testthat/blob/main/tests/testthat/_snaps/expect-self-test.md Use expect_success to assert that a condition results in exactly one success. This example shows the error message when both a success and a failure occur. ```R expect_success(expect_both_wrong()) ``` -------------------------------- ### Test expect_failure with success and failure Source: https://github.com/r-lib/testthat/blob/main/tests/testthat/_snaps/expect-self-test.md Use expect_failure to assert that a condition results in exactly one failure. This example shows the error message when both a success and a failure occur. ```R expect_failure(expect_both_wrong()) ``` -------------------------------- ### Expect equal with unquoted variable Source: https://github.com/r-lib/testthat/blob/main/tests/testthat/_snaps/expect-equality.md Demonstrates the use of unquoting (!!) within expect_equal for dynamic expectations. This example fails when the actual value (1.0) does not equal the expected value (2.0). ```R expect_equal(x, !!y) ``` -------------------------------- ### Test expect_success with no successes Source: https://github.com/r-lib/testthat/blob/main/tests/testthat/_snaps/expect-self-test.md Use expect_success to assert that a condition results in exactly one success. This example shows the error message when no successes occur. ```R expect_success(expect_no_success()) ``` -------------------------------- ### Generate snapshot acceptance hints Source: https://github.com/r-lib/testthat/blob/main/tests/testthat/_snaps/snapshot-file.md Displays instructions for accepting or reviewing snapshot changes. ```R snapshot_hint("lala", reset_output = FALSE) ``` -------------------------------- ### Test expect_success with a failure Source: https://github.com/r-lib/testthat/blob/main/tests/testthat/_snaps/expect-self-test.md Use expect_success to assert that a condition results in exactly one success. This example shows the error message when a failure occurs instead of a success. ```R expect_success(expect_has_failure()) ``` -------------------------------- ### Controlling snapshot value details with complex binary data Source: https://github.com/r-lib/testthat/blob/main/tests/testthat/_snaps/snapshot-value.md Provides an example of complex binary data used for snapshotting, likely representing serialized objects with specific attributes and versioning. This is for advanced internal use. ```binary WAoAAAACAAQCAwACAwAAAAMGAAAEAgAAAAEABAAJAAAABWNsYXNzAAAAEAAAAAEABAAJAAAA B2Zvcm11bGEAAAQCAAAAAQAEAAkAAAAMLkVudmlyb25tZW50AAAABAAAAAAAAAAEAAAAAAAA AAQAAAAAAAAA+QAAAAAAAAACAAQACQAAAAh0ZXN0dGhhdAAEAAkAAAAFMy4yLjAAAAD+AAAA EwAAAB0AAAD+AAAA/gAAAP4AAAD+AAAA/gAAAP4AAAD+AAAA/gAAAP4AAAD+AAAA/gAAAP4A AAD+AAAA/gAAAP4AAAD+AAAA/gAAAP4AAAD+AAAA/gAAAP4AAAD+AAAA/gAAAP4AAAD+AAAA /gAAAP4AAAD+AAAAEwAAAB0AAAD+AAAA/gAAAP4AAAD+AAAA/gAAAP4AAAD+AAAA/gAAAP4A AAD+AAAA/gAAAP4AAAD+AAAA/gAAAP4AAAD+AAAA/gAAAP4AAAD+AAAA/gAAAP4AAAD+AAAA /gAAAP4AAAD+AAAA/gAAAP4AAAD+AAAA/gAAAP4AAAD+AAAAEwAAAB0AAAD+AAAA/gAAAP4A AAD+AAAA/gAAAP4AAAD+AAAA/gAAAP4AAAD+AAAA/gAAAP4AAAD+AAAA/gAAAP4AAAQCAAAAAQAEAAkAAAABZgAAAwYAAAQCAAAB/wAAABAAAAABAAQACQAAAAdmb3JtdWxhAAAEAgAAAv8AAAP/AAAA/gAAAAEABAAJAAAAAX4AAAACAAAADgAAAAE/8AAAAAAAAAAA AP4AAAD+AAAA/gAAAP4AAAD+AAAA/gAAAP4AAAD+AAAA/gAAAP4AAAD+AAAA/gAAAP4AAAD+ AAAA/gAAAP4AAAD+AAAI/wAAAAIAAAAOAAAAAT/wAAAAAAAAAAAA/g== ``` -------------------------------- ### Initialize C++ unit testing infrastructure Source: https://github.com/r-lib/testthat/blob/main/tests/testthat/_snaps/test-compiled-code.md Configures the project for C++ testing and provides necessary package dependency instructions. ```R use_catch(path) ``` -------------------------------- ### expect_snapshot_value() input validation errors Source: https://github.com/r-lib/testthat/blob/main/tests/testthat/_snaps/snapshot-value.md Shows examples of input validation errors for `expect_snapshot_value()`. The first demonstrates an error when `cran` is not a boolean, and the second shows an error when `tolerance` is not a number. ```R expect_snapshot_value(123, cran = "yes") ``` ```R expect_snapshot_value(123, tolerance = "high") ``` -------------------------------- ### Test expect_failure with success Source: https://github.com/r-lib/testthat/blob/main/tests/testthat/_snaps/expect-self-test.md Use expect_failure to assert that a condition results in exactly one failure. This example shows the error message when a success occurs instead of a failure. ```R expect_failure(expect_has_success()) ``` -------------------------------- ### Snapshotting a list of values Source: https://github.com/r-lib/testthat/blob/main/tests/testthat/_snaps/snapshot-value.md Demonstrates how to snapshot a simple list containing various data types. This is useful for verifying the structure and content of basic data collections. ```R list("a", 1.5, 1L, TRUE) ``` -------------------------------- ### Handle snapshot_file_equal_ operations Source: https://github.com/r-lib/testthat/blob/main/tests/testthat/_snaps/snapshot-file.md Demonstrates warnings for new file snapshots and errors for missing files. ```R out <- snapshot_file_equal_(path) ``` ```R snapshot_file_equal_("doesnt-exist.txt") ``` -------------------------------- ### R Package Installation Error: Unsupported Rust Version Source: https://github.com/r-lib/testthat/blob/main/revdep/failures.md This error occurs when the installed Rust version is older than the minimum required version for the R package. Ensure your Rust toolchain is updated to at least version 1.77. ```R * installing *source* package ‘fio’ ... ** this is package ‘fio’ version ‘0.1.6’ ** package ‘fio’ successfully unpacked and MD5 sums checked ** using staged installation Error in eval(ei, envir) : ------------------ [UNSUPPORTED RUST VERSION]------------------ - Minimum supported Rust version is 1.77. - Installed Rust version is 1.75.0. --------------------------------------------------------------- Calls: source -> withVisible -> eval -> eval Execution halted ERROR: configuration failed for package ‘fio’ * removing ‘/tmp/workdir/fio/new/fio.Rcheck/fio’ ``` ```R * installing *source* package ‘fio’ ... ** this is package ‘fio’ version ‘0.1.6’ ** package ‘fio’ successfully unpacked and MD5 sums checked ** using staged installation Error in eval(ei, envir) : ------------------ [UNSUPPORTED RUST VERSION]------------------ - Minimum supported Rust version is 1.77. - Installed Rust version is 1.75.0. --------------------------------------------------------------- Calls: source -> withVisible -> eval -> eval Execution halted ERROR: configuration failed for package ‘fio’ * removing ‘/tmp/workdir/fio/old/fio.Rcheck/fio’ ``` -------------------------------- ### R Package Installation Error: Cargo Lock File Parsing Source: https://github.com/r-lib/testthat/blob/main/revdep/failures.md This error indicates a problem with the Cargo lock file, specifically that the lock file version is too new for the installed Cargo version. This often requires updating Cargo or adjusting build flags. ```R * installing *source* package ‘gadjid’ ... ** this is package ‘gadjid’ version ‘0.1.0’ ** package ‘gadjid’ successfully unpacked and MD5 sums checked ** using staged installation Using cargo 1.75.0 Using rustc 1.75.0 (82e1608df 2023-12-21) (built from a source tarball) Building for CRAN. Writing `src/Makevars`. `tools/config.R` has finished. ** libs ... export CARGO_HOME=/tmp/workdir/gadjid/new/gadjid.Rcheck/00_pkg_src/gadjid/src/.cargo && \ export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/root/.cargo/bin" && \ RUSTFLAGS=" --print=native-static-libs" cargo build -j 2 --offline --lib --release --manifest-path=./rust/Cargo.toml --target-dir ./rust/target error: failed to parse lock file at: /tmp/workdir/gadjid/new/gadjid.Rcheck/00_pkg_src/gadjid/src/rust/Cargo.lock Caused by: lock file version 4 requires `-Znext-lockfile-bump` make: *** [Makevars:26: rust/target/release/libgadjid.a] Error 101 ERROR: compilation failed for package ‘gadjid’ * removing ‘/tmp/workdir/gadjid/new/gadjid.Rcheck/gadjid’ ``` ```R * installing *source* package ‘gadjid’ ... ** this is package ‘gadjid’ version ‘0.1.0’ ** package ‘gadjid’ successfully unpacked and MD5 sums checked ** using staged installation Using cargo 1.75.0 Using rustc 1.75.0 (82e1608df 2023-12-21) (built from a source tarball) Building for CRAN. Writing `src/Makevars`. `tools/config.R` has finished. ** libs ... export CARGO_HOME=/tmp/workdir/gadjid/old/gadjid.Rcheck/00_pkg_src/gadjid/src/.cargo && \ export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/root/.cargo/bin" && \ RUSTFLAGS=" --print=native-static-libs" cargo build -j 2 --offline --lib --release --manifest-path=./rust/Cargo.toml --target-dir ./rust/target error: failed to parse lock file at: /tmp/workdir/gadjid/old/gadjid.Rcheck/00_pkg_src/gadjid/src/rust/Cargo.lock Caused by: lock file version 4 requires `-Znext-lockfile-bump` make: *** [Makevars:26: rust/target/release/libgadjid.a] Error 101 ERROR: compilation failed for package ‘gadjid’ * removing ‘/tmp/workdir/gadjid/old/gadjid.Rcheck/gadjid’ ``` -------------------------------- ### Skip on Multiple OS Source: https://github.com/r-lib/testthat/blob/main/tests/testthat/_snaps/skip.md Shows how to skip tests on specific operating systems. ```R skip_on_os("windows") ``` ```R skip_on_os(c("windows", "mac")) ``` -------------------------------- ### Failure messages for expect_mapequal Source: https://github.com/r-lib/testthat/blob/main/tests/testthat/_snaps/expect-setequal.md Examples of error messages when names or values do not match in expect_mapequal. ```R expect_mapequal(x, y) ``` ```R expect_mapequal(y, x) ``` ```R expect_mapequal(x, y) ``` -------------------------------- ### Failure messages for expect_contains Source: https://github.com/r-lib/testthat/blob/main/tests/testthat/_snaps/expect-setequal.md Examples of error messages when expected values are missing from the actual object. ```R expect_contains(x1, x2) ``` ```R expect_contains(x1, x3) ``` -------------------------------- ### Set Local Edition Source: https://github.com/r-lib/testthat/blob/main/tests/testthat/_snaps/edition.md Use `local_edition()` to temporarily set the test edition. It requires a whole number as input. Invalid inputs will raise an error. ```r local_edition("x") ``` ```r local_edition(5) ``` -------------------------------- ### Initialize and use SlowReporter Source: https://github.com/r-lib/testthat/blob/main/tests/testthat/_snaps/reporter-slow.md Instantiates a SlowReporter with a minimum time threshold and applies it to a test execution context. ```R show_all <- SlowReporter$new(min_time = 0) with_reporter(show_all, n_tests(10)) ``` -------------------------------- ### Deprecated expect_no_success Source: https://github.com/r-lib/testthat/blob/main/tests/testthat/_snaps/expect-self-test.md The expect_no_success function is deprecated and should be replaced with expect_failure. This example shows the deprecation warning. ```R expect_no_success(fail()) ``` -------------------------------- ### Deprecated expect_no_failure Source: https://github.com/r-lib/testthat/blob/main/tests/testthat/_snaps/expect-self-test.md The expect_no_failure function is deprecated and should be replaced with expect_success. This example shows the deprecation warning. ```R expect_no_failure(pass()) ``` -------------------------------- ### Set Up Reproducible Test Environment with local_test_context Source: https://context7.com/r-lib/testthat/llms.txt Configure options for reproducible test output, such as disabling colors, Unicode, and setting a fixed width. These settings are automatically applied by test_that() in the 3rd edition but can be manually controlled for debugging. ```r library(testthat) # Simulate test environment interactively local({ local_test_context() # Now running in test-like environment: # - Colors disabled # - Unicode disabled # - Fixed width (80) # - Non-interactive mode print(is_testing()) # TRUE }) ``` ```r library(testthat) # Customize reproducible output settings test_that("test with custom output settings", { # Enable Unicode for this test local_reproducible_output(unicode = TRUE) expect_equal(cli::symbol$tick, "\u2714") # Different width local_reproducible_output(width = 40) # Enable colors local_reproducible_output(crayon = TRUE) }) ``` -------------------------------- ### Snapshotting with Preserved Empty Lines Source: https://github.com/r-lib/testthat/blob/main/tests/testthat/_snaps/snapshot.md Demonstrates that empty lines within the output are preserved in the snapshot. This ensures exact output matching. ```R Code f() Output 1 Message 2 Condition Warning in `f()`: 3 Error in `f()`: ! 4 ``` -------------------------------- ### Propagate Missing Arguments with expect_null Source: https://github.com/r-lib/testthat/blob/main/tests/testthat/_snaps/quasi-label.md Use `expect_null` to assert that an expression results in NULL. This example shows how missing arguments are propagated. ```R expect_null(x$missing) ``` -------------------------------- ### Basic snapshot test in describe Source: https://github.com/r-lib/testthat/blob/main/tests/testthat/_snaps/describe.md Executes a simple addition operation within a describe block. ```R 1 + 1 ``` -------------------------------- ### Validate `failure_message` argument in expect() Source: https://github.com/r-lib/testthat/blob/main/tests/testthat/_snaps/expectation.md The `failure_message` argument in `expect()` must be a character vector. This example shows the error when a number is provided. ```R expect(TRUE, 1) ``` -------------------------------- ### Skip on OS with Architecture Source: https://github.com/r-lib/testthat/blob/main/tests/testthat/_snaps/skip.md Allows refining OS skips by specifying the architecture. ```R skip_on_os(c("windows", "linux"), arch = "x86_64") ``` ```R skip_on_os(c("windows", "linux"), arch = c("i386", "x86_64")) ``` -------------------------------- ### Accept Specific Snapshot Variants Source: https://github.com/r-lib/testthat/blob/main/tests/testthat/_snaps/snapshot-manage.md This demonstrates accepting snapshot updates for specific files, including those in subdirectories. It can work with variants of snapshot files. ```r snapshot_accept(path = path) ``` ```r snapshot_accept("foo/a", path = path) ``` -------------------------------- ### Snapshotting Simple Output Source: https://github.com/r-lib/testthat/blob/main/tests/testthat/_snaps/snapshot.md Captures and snapshots basic output. Empty lines are preserved in the snapshot. ```R y ``` ```R y ``` ```R y ``` ```R y ``` -------------------------------- ### Test Context Utilities Source: https://context7.com/r-lib/testthat/llms.txt Functions to configure reproducible environments for testing. ```APIDOC ## local_test_context / local_reproducible_output ### Description Configure global options to ensure test output is reproducible, such as disabling colors, setting fixed widths, and controlling Unicode output. ### Parameters - **unicode** (boolean) - Optional - Enable/disable Unicode. - **width** (integer) - Optional - Set output width. - **crayon** (boolean) - Optional - Enable/disable color output. ``` -------------------------------- ### Triggering unused argument errors in expect_condition Source: https://github.com/r-lib/testthat/blob/main/tests/testthat/_snaps/expect-condition.md These examples demonstrate various scenarios where providing incorrect or extra arguments to expect_condition results in an error. ```R expect_condition(stop("Hi!"), foo = "bar") ``` ```R expect_condition(stop("Hi!"), , , "bar") ``` ```R expect_condition(stop("Hi!"), , , "bar", fixed = TRUE) ``` ```R expect_condition(stop("Hi!"), "x", foo = "bar") ``` ```R expect_condition(stop("Hi!"), pattern = "bar", fixed = TRUE) ``` -------------------------------- ### Expect no warning with custom message and class Source: https://github.com/r-lib/testthat/blob/main/tests/testthat/_snaps/expect-no-condition.md Illustrates how to use expect_no_warning with specific message patterns and condition classes. This allows for more precise testing of expected warning behaviors. ```R expect_no_warning(foo()) ``` ```R expect_no_warning(foo(), message = "problem") ``` ```R expect_no_warning(foo(), class = "test") ``` ```R expect_no_warning(foo(), message = "problem", class = "test") ``` -------------------------------- ### Use expectation shortcut functions Source: https://github.com/r-lib/testthat/blob/main/NEWS.md Shortcut functions provide a more concise syntax for common expectations. ```R expect_that(a, is_identical_to(b)) expect_identical(a, b) ``` -------------------------------- ### Detailed output from expect_equal() Source: https://github.com/r-lib/testthat/blob/main/NEWS.md Detailed output from expect_equal() now correctly distinguishes between expected and actual values, preventing confusion and improving debugging. ```R expect_equal(object, expected) ``` -------------------------------- ### Test expect_success with an error Source: https://github.com/r-lib/testthat/blob/main/tests/testthat/_snaps/expect-self-test.md Use expect_success to assert that a condition runs without errors. This example shows the error message when an unexpected error occurs. ```R expect_success(abort("error")) ``` -------------------------------- ### BDD-style testing with describe and it Source: https://context7.com/r-lib/testthat/llms.txt Use describe() and it() for a behavior-driven development style, making tests read like natural sentences. Group related tests under describe blocks. ```r library(testthat) # Define functions to test addition <- function(a, b) a + b division <- function(a, b) { if (b == 0) stop("Cannot divide by zero") a / b } # BDD-style testing describe("math library", { describe("addition()", { it("can add two positive numbers", { expect_equal(addition(1, 2), 3) }) it("can add negative numbers", { expect_equal(addition(-1, -2), -3) }) it("handles zero correctly", { expect_equal(addition(5, 0), 5) }) }) describe("division()", { it("can divide two numbers", { expect_equal(division(10, 2), 5) }) it("throws error for division by zero", { expect_error(division(10, 0), "Cannot divide by zero") }) it("returns decimal results") # Not yet implemented - auto-skipped }) }) ``` -------------------------------- ### Test expect_failure with no failures Source: https://github.com/r-lib/testthat/blob/main/tests/testthat/_snaps/expect-self-test.md Use expect_failure to assert that a condition results in exactly one failure. This example shows the error message when no failures occur. ```R expect_failure(expect_no_failure()) ``` -------------------------------- ### Run Single Test File with test_file Source: https://context7.com/r-lib/testthat/llms.txt Execute tests from a single R file. This is useful for rapid development and debugging specific test files, allowing targeted execution with options for reporters and description filtering. ```r library(testthat) # Run a specific test file test_file("tests/testthat/test-utils.R") # Run with minimal output test_file("tests/testthat/test-utils.R", reporter = "minimal") # Run only tests with specific description test_file("tests/testthat/test-utils.R", desc = "handles edge cases") ``` -------------------------------- ### Expect identical with type mismatch Source: https://github.com/r-lib/testthat/blob/main/tests/testthat/_snaps/expect-equality.md Use expect_identical to check for exact matches, including type. This example shows a failure when comparing a double to a character. ```R expect_identical(x, "a") ``` -------------------------------- ### Filter test files using grepl() with test_dir(), test_package(), test_check() Source: https://github.com/r-lib/testthat/blob/main/NEWS.md The test functions now accept a '...' argument that passes additional arguments to grepl(). This allows filtering of test files using regular expressions or fixed character matching. ```R test_dir("test", pattern = ".*_test.R$", \dots) ``` -------------------------------- ### Skip on OS Source: https://github.com/r-lib/testthat/blob/main/tests/testthat/_snaps/skip.md Demonstrates skipping tests based on the operating system. An error occurs if an invalid OS name is provided. ```R skip_on_os("amiga") ``` -------------------------------- ### Test expect_success with multiple successes Source: https://github.com/r-lib/testthat/blob/main/tests/testthat/_snaps/expect-self-test.md Use expect_success to assert that a condition results in exactly one success. This example shows the error message when multiple successes occur. ```R expect_success(expect_many_successes()) ``` -------------------------------- ### Match output against a string pattern Source: https://github.com/r-lib/testthat/blob/main/tests/testthat/_snaps/expect-output.md Provide a string to match the output against a regular expression. ```R expect_output(g(), "x") ``` ```R expect_output("a", "x") ``` -------------------------------- ### Snapshotting All Output Components Source: https://github.com/r-lib/testthat/blob/main/tests/testthat/_snaps/snapshot.md Captures code, output, messages, and conditions (warnings/errors) in a single snapshot. This is useful for comprehensive testing. ```R Code f() Output [1] "1" Message 2 Condition Warning in `f()`: 3 Error in `f()`: ! 4 ```