Type: | Package |
Title: | Utils and 'RStudio' Addins to Make Testing Even More Fun |
Version: | 1.1.1 |
Maintainer: | Stefan Fleck <stefan.b.fleck@gmail.com> |
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. |
License: | MIT + file LICENSE |
URL: | https://s-fleck.github.io/testthis |
BugReports: | https://github.com/s-fleck/testthis/issues |
Imports: | assertthat, devtools, fs, magrittr, pkgload, rprojroot, stats, stringi, testthat, tools, usethis (≥ 0.1.0), utils |
Suggests: | covr, crayon, knitr, rmarkdown, roxygen2 (≥ 5.0.0), rstudioapi |
Encoding: | UTF-8 |
RoxygenNote: | 7.1.0 |
NeedsCompilation: | no |
Packaged: | 2020-04-12 06:22:22 UTC; fleck |
Author: | Stefan Fleck |
Repository: | CRAN |
Date/Publication: | 2020-04-12 06:30:02 UTC |
Testthis-package
Description
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.
Details
For details please refer to the README
Setting the project path
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
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 namedtest_foofunction.R
ortest-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...
Testthis Tags
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 withtest_
. This is used bytest_this()
,lest_this()
andopen_testfile()
.
Valid tags or test files (pkg/tests/testthat/test_*.R
)
-
@testing <functionname>
: markfunctionname
as tested. Should usually go next the associatedtest_that()
call. This is used bytest_coverage()
.
Author(s)
Maintainer: Stefan Fleck stefan.b.fleck@gmail.com (ORCID)
See Also
Get Test Coverage
Description
Deprecated in favor of test_coverage()
Usage
get_test_coverage(from_tags = TRUE, from_desc = TRUE)
Arguments
from_tags , from_desc |
see |
Open associated test file
Description
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’.
Usage
open_testfile()
Test coverage of package
Description
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.
Usage
test_coverage(from_tags = TRUE, from_desc = TRUE)
Arguments
from_tags |
|
from_desc |
|
Details
test_coverage
looks in .covrignore
for functions that should be ignored
for coverage analysis (see usethis::use_covr_ignore()
)
Value
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’?
Examples
## Not run:
x <- test_coverage()
as.data.frame(x)
## End(Not run)
Generate an index of all test_that calls
Description
Generates an index the desc
arguments of all test_that()
calls in the
tests/testthat
directory of a package.
Usage
test_index(
markers = interactive() && requireNamespace("rstudioapi", quietly = TRUE)
)
## S3 method for class 'test_index'
print(x, ...)
Arguments
markers |
|
x |
a |
... |
currently ignored |
Value
A test_index
data.frame
(invisibly if markers == TRUE
)
Create a test skeleton file for the currently open .R file
Description
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.
Usage
test_skeleton(fname = NULL, open = TRUE, sep = options("testthis.sep"))
Arguments
fname |
|
open |
|
sep |
|
Value
NULL
(invisibly)
Side effects
Creates an R script file in the file system.
See Also
Run tests in subdirectories
Description
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()
.
Usage
test_subdir(subdir, ...)
test_integration(...)
test_acceptance(...)
test_manual(...)
test_all(...)
Arguments
subdir |
|
... |
passed on to |
Value
A testthat::testthat_results object (invisibly)
Test subdirectory presets
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.
See Also
Test this file
Description
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.
Usage
test_this(...)
Arguments
... |
passed on to |
Details
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).
Value
NULL
(invisibly)
Use test subdirectories
Description
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()
.
Usage
use_test_subdir(path, make_tester = TRUE, ignore_tester = TRUE)
use_integration_tests()
use_acceptance_tests()
use_manual_tests()
Arguments
path |
|
make_tester |
|
ignore_tester |
|
Value
TRUE
on success (invisibly).
Test subdirectory presets
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.
See Also
Other infrastructure:
use_testdata()
,
use_tester()
Examples
## 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)
Create testdata
folder.
Description
Save R objects to separate files ‘tests/testthat/testdata’ in the
.rds
format.
Usage
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)
Arguments
... |
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 |
|
Value
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
Side effects
use_testdata()
saves an R object to a ‘testdata’ dir in the current package.
See Also
Other infrastructure:
use_test_subdir()
,
use_tester()
Examples
## Not run:
use_testdata(letters, LETTERS)
## End(Not run)
Use a tester function
Description
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.
Usage
use_tester(
path,
ignore = TRUE,
tester_path = file.path("R", "testthis-testers.R")
)
Arguments
path |
|
ignore |
|
tester_path |
|
Value
TRUE
on success (invisibly).
See Also
Other infrastructure:
use_test_subdir()
,
use_testdata()