testthat.use_catch(dir = getwd())CATCH_ prefix -- see
testthat provides the
following wrappers, to conform with testthat's
Rinterface:context CATCH_TEST_CASE The context of a set of tests.
test_that CATCH_SECTION A test section.
expect_true CATCH_CHECK Test that an expression evaluates to true.
expect_false CATCH_CHECK_FALSE Test that an expression evalutes to false.
expect_error CATCH_CHECK_THROWS Test that evaluation of an expression throws an error.
expect_error_as CATCH_CHECK_THROWS_AS Test that evaluation of an expression throws an error of a specific class.
}
testthat::catchSession() object in a file
with the form:#define TESTTHAT_TEST_RUNNER
#include
void run() { Catch::Session& session = testthat::catchSession(); // interact with the session object as desired }
This can be useful if you'd like to run your unit tests with custom arguments passed to the Catch session.
use_catch() will:src/test-runner.cpp, which ensures that thetestthatpackage will understand how to run your package's
unit tests,src/test-example.cpp, which
showcases how you might use Catch to write a unit test, andtests/testthat/test-cpp.R, which ensures thattestthatwill run your compiled tests.C++ unit tests can be added to C++ source files within the
src/ directory of your package, with a format similar
to Rcode tested with testthat -- for example,
context("C++ Unit Test") { test_that("two plus two is four") { int result = 2 + 2; expect_true(result == 4); } }
When your package is compiled, unit tests alongside a harness
for running these tests will be compiled into your Rpackage,
with the C entry point run_testthat_tests(). testthat
will use that entry point to run your unit tests when detected.