Package 'exampletestr'

Title: Help for Writing Unit Tests Based on Function Examples
Description: Take the examples written in your documentation of functions and use them to create shells (skeletons which must be manually completed by the user) of test files to be tested with the 'testthat' package. Sort of like python 'doctests' for R.
Authors: Rory Nolan [aut, cre] , Sergi Padilla-Parra [ths] , Thomas Quinn [rev] , Laurent Gatto [rev]
Maintainer: Rory Nolan <[email protected]>
License: GPL-3
Version: 1.7.3
Built: 2024-12-07 06:28:18 UTC
Source: CRAN

Help Index


Create test shells.

Description

  • For a given function fun() in a package, make_test_shell_fun() checks if there are examples for that function detailed in the 'man/' directory (in a '.Rd' file) and if so creates a shell (skeleton) of a testthat::test_that() test based on those examples. The created shell is then written to a corresponding file 'test-fun.R' (or if that already exists, 'test-fun-examples.R') in 'tests/testthat'.

  • For a given file 'x.R' in the 'R/' directory of a package, for each function defined in that '.R' file, make_tests_shells_file() checks if there are examples for that function detailed in the 'man/' directory (in a '.Rd' file) and if so creates a shell (skeleton) of a testthat::test_that() test based on those examples via make_test_shell(). The created shells are then written to a corresponding file 'test-x.R' (or 'test-x-examples.R' if 'test-x.R' is already taken) in 'tests/testthat'.

  • make_test_shells_pkg() runs make_test_shells_file() on every '.R' file in the 'R/' directory of a package.

Usage

make_test_shell_fun(
  fun,
  pkg_dir = ".",
  overwrite = FALSE,
  e_e = TRUE,
  roxytest = FALSE,
  open = TRUE,
  document = TRUE
)

make_tests_shells_file(
  r_file_name,
  pkg_dir = ".",
  overwrite = FALSE,
  e_e = TRUE,
  open = TRUE,
  document = TRUE
)

make_tests_shells_pkg(
  pkg_dir = ".",
  overwrite = FALSE,
  e_e = TRUE,
  open = FALSE,
  document = TRUE
)

Arguments

fun

The name of the function to make a test shell for.

pkg_dir

The directory of the R project for this package (defaults to current directory). Note that this is the parent directory of R/.

overwrite

Overwrite if the test file you're trying to create already exists?

e_e

Set this to FALSE to prevent anything from being put in the shell of a testthat::expect_equal() statement.

roxytest

Copy roxytest package ⁠@testexamples⁠ code to clipboard instead of creating file in tests/testthat?

open

Open the created test file in your editor after it is created?

document

Run roxygen2::roxygenize() to update package documentation before starting?

r_file_name

The name of the '.R' file within 'R/'. There's no need to specify the file path (as 'R/x.R', but you can do this if you want), you can just use 'x.R' for whichever file 'x' it is. You can also omit the '.R' for convenience, however using the wrong case (e.g. '.r' when the file actually has the extension '.R') will produce an error.

Value

The shell of the test file is written into tests/testthat. It has the same name as the .R file it was created from except it has "test_" tacked onto the front.

Examples

## Not run: 
pkg_dir <- "~/mylilpkg"
usethis::create_package(pkg_dir, rstudio = FALSE, open = FALSE)
fs::file_copy(
  system.file("extdata", c("detect.R", "match.R"),
    package = "exampletestr"
  ),
  paste0(pkg_dir, "/R")
)
make_test_shell_fun("str_detect()", pkg_dir,
  document = TRUE, roxytest = TRUE
)
make_test_shell_fun("str_detect()", pkg_dir,
  document = TRUE, open = FALSE
)
make_tests_shells_file("detect", pkg_dir,
  document = FALSE, open = FALSE
)
make_tests_shells_pkg(pkg_dir,
  overwrite = TRUE, document = FALSE
)
fs::dir_delete(pkg_dir)

## End(Not run)