Title: | Utils and 'RStudio' Addins to Make Testing Even More Fun |
---|---|
Description: | Utility functions and 'RStudio' addins for writing, running and organizing automated tests. Integrates tightly with the packages 'testthat', 'devtools' and 'usethis'. Hotkeys can be assigned to the 'RStudio' addins for running tests in a single file or to switch between a source file and the associated test file. In addition, testthis provides function to manage and run tests in subdirectories of the test/testthat directory. |
Authors: | Stefan Fleck [aut, cre] |
Maintainer: | Stefan Fleck <[email protected]> |
License: | MIT + file LICENSE |
Version: | 1.1.1.9000 |
Built: | 2024-11-03 03:56:28 UTC |
Source: | https://github.com/s-fleck/testthis |
Deprecated in favor of test_coverage()
get_test_coverage(from_tags = TRUE, from_desc = TRUE)
get_test_coverage(from_tags = TRUE, from_desc = TRUE)
from_tags , from_desc
|
see |
If the currently open file in the RStudio editor is called ‘myfun.R’ this
opens ‘tests/testthat/test_myfun.R’ in a new tab. This function can also
be used to jump back and forth between a script and the associated test
file. You can modify this behaviour by putting the comment
#* @testfile anotherfile
anywhere in ‘myfun.R’.
open_testfile()
open_testfile()
This determines the test coverage of the target package based on the desc
argument of test_that()
calls. If you require a more comprehensive analysis
of test coverage, try the package covr instead.
test_coverage(from_tags = TRUE, from_desc = TRUE)
test_coverage(from_tags = TRUE, from_desc = TRUE)
from_tags |
|
from_desc |
|
test_coverage
looks in .covrignore
for functions that should be ignored
for coverage analysis (see usethis::use_covr_ignore()
)
A Test_coverage
object. This is a data.frame
containing the
following columns:
fun: Name of the function
exp: Is function is exported?
s3: Is function an S3 method?
tested: Do unit tests exist for function?
ignore: Is function listed in ‘tests/testthat/_testignore’?
## Not run: x <- test_coverage() as.data.frame(x) ## End(Not run)
## Not run: x <- test_coverage() as.data.frame(x) ## End(Not run)
Generates an index the desc
arguments of all test_that()
calls in the
tests/testthat
directory of a package.
test_index( markers = interactive() && requireNamespace("rstudioapi", quietly = TRUE) && rstudioapi::isAvailable() ) ## S3 method for class 'test_index' print(x, ...)
test_index( markers = interactive() && requireNamespace("rstudioapi", quietly = TRUE) && rstudioapi::isAvailable() ) ## S3 method for class 'test_index' print(x, ...)
markers |
|
x |
a |
... |
currently ignored |
A test_index
data.frame
(invisibly if markers == TRUE
)
If the file currently open in the RStudio editor is called my_function.R
,
this creates the file ‘/tests/testthat/test_my_function.R’ and fills it
with a basic test skeleton.
test_skeleton(fname = NULL, open = TRUE, sep = options("testthis.sep"))
test_skeleton(fname = NULL, open = TRUE, sep = options("testthis.sep"))
fname |
|
open |
|
sep |
|
NULL
(invisibly)
Creates an R script file in the file system.
This is a simple wrapper for devtools::test()
, but rather than running
the tests in ‘inst/tests/’ or ‘tests/testthat/’, it runs the tests
in a subdirectory of that folder. For creating such subdirectories,
please also see use_test_subdir()
.
test_subdir(subdir, ...) test_integration(...) test_acceptance(...) test_manual(...) test_all(...)
test_subdir(subdir, ...) test_integration(...) test_acceptance(...) test_manual(...) test_all(...)
subdir |
|
... |
passed on to |
A testthat::testthat_results object (invisibly)
Three preset test subdirs are defined at the moment:
test_integration()
Integration tests, also called component tests.
Put tests here that test if several functions / parts of your program
work together as expected. You can create the relevant subdir
‘testthat/integration_tests/’ with use_integration_tests()
.
test_acceptance()
Acceptance tests. This is the highest levels of
tests. Put tests here that verifies if your package fulfills the
goals/requirements you set out to achieve with your package were met.
You can create the relevant subdir ‘testthat/acceptance_tests/’
with use_acceptance_tests()
.
test_manual()
Manual tests. Put tests here that produce output
that has to be manually verified, such as: console output, pdf files,
plots. It is recommended you collect the output files of such tests in
‘tests/testthat/testout’. You can create the relevant subdir
with ‘testthat/manual_tests/’ with use_manual_tests()
.
You can modify the default paths for manual, acceptance and integration tests
by setting the respective options()
, but it is recommended to create your
own test subdirs instead.
Runs testthat tests in a single .R
file. If the file currently open in the
RStudio editor is called my_function.R
, test_this()
calls
testthat::test_file()
on ‘tests/testthat/test_my_function.R’. If
the filename of the currently open file with starts with test_
it will
call testthat::test_file()
on the current file.
test_this(...)
test_this(...)
... |
passed on to |
This is useful in cases where you don't want to run all tests in a package
via devtools::test()
(CTRL+SHIFT+T in RStudio).
NULL
(invisibly)
RStudio addins for several common testing-related tasks during package development, such as switching between a source file and an associated test file, or running unit tests in a single test file (hotkeyable in RStudio!). testthis also provides utility function to manage tests in subdirectories of the test/testthis directory.
For details please refer to the README
Testthis uses usethis::proj_get()
to detect project root of the current
working directory. You can override the project root with
usethis::proj_set()
.
Package options can be set with options()
. You can add options()
to
your ‘.Rprofile’ to make them permanent across sessions.
testthis.sep
Default separator to use when creating test files with
test_skeleton()
. Defaults to _
, must be either _
or -
; i.e whether
you want your files to be named test_foofunction.R
or test-foofunction.R
testthis.integration_tests_path
, testthis.acceptance_tests_path
,
testthis.manual_tests_path
Default paths used by the functions
testthis::use_integration_tests()
, testthis::test_integration()
, etc...
test_this tags are special comments that modify the behaviour of the
functions supplied by this package. They are of the form #* @tag <value>
.
Please not that only some test_this tags really require a <value>
.
Valid tags for script files in the /R/ dir (pkg/R/*.R
)
@testfile <filename>
: manually specify associated test file. Should
usually start with test_
. This is used by test_this()
, lest_this()
and
open_testfile()
.
Valid tags or test files (pkg/tests/testthat/test_*.R
)
@testing <functionname>
: mark functionname
as tested.
Should usually go next the associated test_that()
call. This is used by
test_coverage()
.
Maintainer: Stefan Fleck [email protected] (ORCID)
Create a subdir in ‘tests/testthat/’ and optionally an R script
containing a helper function to run all tests in that subdir. Useful for
separating long-running tests from your unit tests, or storing tests that
you do not want to run on CRAN or during R CMD Check. For running tests in
‘tests/testthat/’ subdirectories see test_subdir()
.
use_test_subdir(path, make_tester = TRUE, ignore_tester = TRUE) use_integration_tests() use_acceptance_tests() use_manual_tests()
use_test_subdir(path, make_tester = TRUE, ignore_tester = TRUE) use_integration_tests() use_acceptance_tests() use_manual_tests()
path |
|
make_tester |
|
ignore_tester |
|
TRUE
on success (invisibly).
Three preset test subdirs are defined at the moment:
test_integration()
Integration tests, also called component tests.
Put tests here that test if several functions / parts of your program
work together as expected. You can create the relevant subdir
‘testthat/integration_tests/’ with use_integration_tests()
.
test_acceptance()
Acceptance tests. This is the highest levels of
tests. Put tests here that verifies if your package fulfills the
goals/requirements you set out to achieve with your package were met.
You can create the relevant subdir ‘testthat/acceptance_tests/’
with use_acceptance_tests()
.
test_manual()
Manual tests. Put tests here that produce output
that has to be manually verified, such as: console output, pdf files,
plots. It is recommended you collect the output files of such tests in
‘tests/testthat/testout’. You can create the relevant subdir
with ‘testthat/manual_tests/’ with use_manual_tests()
.
You can modify the default paths for manual, acceptance and integration tests
by setting the respective options()
, but it is recommended to create your
own test subdirs instead.
Other infrastructure:
use_testdata()
,
use_tester()
## Not run: use_test_subdir("special_tests", make_tester = TRUE) ## Reload the Package manually... ## Create some tests in tests/testthat/test_special_tests/ test_special_tests() ## End(Not run)
## Not run: use_test_subdir("special_tests", make_tester = TRUE) ## Reload the Package manually... ## Create some tests in tests/testthat/test_special_tests/ test_special_tests() ## End(Not run)
testdata
folder.Save R objects to separate files ‘tests/testthat/testdata’ in the
.rds
format.
use_testdata( ..., subdir = NULL, overwrite = FALSE, ignore = FALSE, compress = TRUE, refhook = NULL, version = NULL ) use_testdata_raw(name = NULL) has_testdata() read_testdata(infile, subdir = NULL) find_testdata(..., path = ".", must_exist = FALSE)
use_testdata( ..., subdir = NULL, overwrite = FALSE, ignore = FALSE, compress = TRUE, refhook = NULL, version = NULL ) use_testdata_raw(name = NULL) has_testdata() read_testdata(infile, subdir = NULL) find_testdata(..., path = ".", must_exist = FALSE)
... |
R objects to save to the ‘testdata’ dir. If empty, an empty directory is created. |
subdir |
|
overwrite |
Logical scalar. Should existing files be overwritten? |
ignore |
Should the newly created file be added to |
compress |
a logical specifying whether saving to a named file is
to use |
refhook |
a hook function for handling reference objects. |
version |
the workspace format version to use. |
name |
a |
infile |
rds file to read (must end in .rds, otherwise .rds ending is automatically added) |
path |
Path of the directory to create, relative to the project. |
must_exist |
|
use_testdata()
returns TRUE
if object was successfully saved.
has_testdata()
returns TRUE
if package has a
‘tests/testthat/testdata’ folder.
read_testdata()
returns a single R object
find_testdata()
returns the normalized path to a file in a
in the testdata directory
use_testdata()
saves an R object to a ‘testdata’ dir in the current package.
Other infrastructure:
use_test_subdir()
,
use_tester()
## Not run: use_testdata(letters, LETTERS) ## End(Not run)
## Not run: use_testdata(letters, LETTERS) ## End(Not run)
Quickly create an R script that contains a function for running all tests
in a predefined directory. This function powers the make_tester
option
of use_test_subdir()
and you will likely not need to run it manually.
use_tester( path, ignore = TRUE, tester_path = file.path("R", "testthis-testers.R") )
use_tester( path, ignore = TRUE, tester_path = file.path("R", "testthis-testers.R") )
path |
|
ignore |
|
tester_path |
|
TRUE
on success (invisibly).
Other infrastructure:
use_test_subdir()
,
use_testdata()