Title: | Add Tests to Examples |
---|---|
Description: | Add tests in-line in examples. Provides standalone functions for facilitating easier test writing in Rd files. However, a more familiar interface is provided using 'roxygen2' tags. Tools are also provided for facilitating package configuration and use with 'testthat'. |
Authors: | Doug Kelkhoff [aut, cre] |
Maintainer: | Doug Kelkhoff <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.2.0.9000 |
Built: | 2024-11-11 04:14:38 UTC |
Source: | https://github.com/dgkf/testex |
Expect no Error
fallback_expect_no_error(object, ...)
fallback_expect_no_error(object, ...)
object |
An expression to evaluate |
... |
Additional arguments unused |
The value produced by the expectation code
This is a stop-gap implementation, and will only be used for legacy
versions of testthat
before this was properly supported.
A testthat
expectation that the provided code can be evaluated without
producing an error. This is the most basic expectation one should expect of
any example code. Further expectations are provided in subsequent testthat
code.
testthat
testsReads examples from Rd files and constructs testthat
-style tests.
testthat
expectations are built such that
test_examples_as_testthat( package, path, ..., test_dir = file.path(tempdir(), "testex-tests"), clean = TRUE, overwrite = TRUE, roxygenize = !is_r_cmd_check() && uses_roxygen2(path), reporter = testthat::get_reporter() )
test_examples_as_testthat( package, path, ..., test_dir = file.path(tempdir(), "testex-tests"), clean = TRUE, overwrite = TRUE, roxygenize = !is_r_cmd_check() && uses_roxygen2(path), reporter = testthat::get_reporter() )
package |
A package name whose examples should be tested |
path |
Optionally, a path to a source code directory to use. Will only
have an effect if parameter |
... |
Additional argument unused |
test_dir |
An option directory where test files should be written. Defaults to a temporary directory. |
clean |
Whether the |
overwrite |
Whether files should be overwritten if |
roxygenize |
Whether R documentation files should be re-written using
|
reporter |
A |
Each example expression is expected to run without error
Any testex
expectations are expected to pass
Generally, you won't need to use this function directly. Instead, it
is called by a file generated by use_testex_as_testthat()
which will add
any testex
example tests to your existing testthat
testing suite.
The result of testthat::source_file()
, after iterating over
generated test files.
It is assumed that this function is used within a testthat
run, when
the necessary packages are already installed and loaded.
# library(pkg.example) path <- system.file("pkg.example", package = "testex") test_examples_as_testthat(path = path)
# library(pkg.example) path <- system.file("pkg.example", package = "testex") test_examples_as_testthat(path = path)
A wrapper around stopifnot
that allows you to use .
to refer to
.Last.value
and preserve the last non-test output from an example.
testex( ..., srcref = NULL, example_srcref = NULL, value = get_example_value(), envir = parent.frame(), style = "standalone" )
testex( ..., srcref = NULL, example_srcref = NULL, value = get_example_value(), envir = parent.frame(), style = "standalone" )
... |
Expressions to evaluated. |
srcref |
An option |
example_srcref |
An option |
value |
A value to test against. By default, this will use the example's
|
envir |
An environment in which tests should be evaluated. By default the parent environment where tests are evaluated. |
style |
A syntactic style used by the test. Defaults to |
Invisibly returns the .Last.value
as it existed prior to evaluating
the test.
testex
testex
is a simple wrapper around execution that propagates the
.Last.value
returned before running, allowing you to chain tests
more easily.
Rd
files:\examples{ f <- function(a, b) a + b f(3, 4) \testonly{ testex::testex( is.numeric(.), identical(., 7) ) } }
But Rd
files are generally regarded as being a bit cumbersome to author
directly. Instead, testex
provide helpers that generate this style of
documentation, which use this function internally.
roxygen2
Within a roxygen2
@examples
block you can instead use the @test
tag
which will generate Rd code as shown above.
#' @examples #' f <- function(a, b) a + b #' f(3, 4) #' @test is.numeric(.) #' @test identical(., 7)
testex
roxygen2
tagstestex
provides two new roxygen2
tags, @test
and @testthat
.
testex tags are all sub-tags meant to be used within an
@examples
block. They should be considered as tags within the
@examples
block and used to construct blocks of testing code within
example code.
@test
: In-line expectations to test the output of the previous command within an
example. If .
is used within the test expression, it will be used to
refer to the output of the previous example command. Otherwise, the
result of the expression is expected to be identical to the previous
output.
#' @examples #' 1 + 2 #' @test 3 #' @test . == 3 #' #' @examples #' 3 + 4 #' @test identical(., 7)
@testthat
: Similar to @test
, @testthat
can be used to make in-line
assertions using testthat
expectations. testthat
expectations
follow a convention where the first argument is an object to compare
against an expected value or characteristic. Since the value will always
be the result of the previous example, this part of the code is
implicitly constructed for you.
If you want to use the example result elsewhere in your expectation, you
can refer to it with a .
. When used in this way, testex will
not do any further implicit modification of your expectation.
#' @examples #' 1 + 2 #' @testthat expect_equal(3) #' @testthat expect_gt(0) #' #' @examples #' 3 + 4 #' @testthat expect_equal(., 7)
testthat
Expectationstestthat
support is managed through a "style" provided to testex
.
When using the testthat
style (automatically when using the @testthat
tag), expectations are processed such that they always refer to the previous
example. Special care is taken to manage propagation of this value through
your test code, regardless of how testthat
is executed.
# example code 1 + 2 # within `testex` block, test code refers to previous result with `.` testex(style = "testthat", srcref = "abc.R:1:3", { test_that("addition holds up", { expect_equal(., 3) }) })
# example code 1 + 2 # within `testex` block, test code refers to previous result with `.` testex(style = "testthat", srcref = "abc.R:1:3", { test_that("addition holds up", { expect_equal(., 3) }) })
testex
tags and configure package to fully use testex
featuresAdd testex
tags and configure package to fully use testex
features
use_testex(path = getwd(), check = TRUE, quiet = FALSE)
use_testex(path = getwd(), check = TRUE, quiet = FALSE)
path |
A package source code working directory |
check |
A |
quiet |
Whether output should be suppressed |
The result of write.dcf()
upon modifying the package
DESCRIPTION
file.
The testex
roxygen2
tags behave similarly to roxygen2
@examples
tags, with the minor addition of some wrapping code to manage the tests. This
means that they will be integrated into your @examples
and can be
intermixed between @examples
tags
testthat
expectationsRun examples as testthat
expectations
use_testex_as_testthat(path = getwd(), context = "testex", quiet = FALSE)
use_testex_as_testthat(path = getwd(), context = "testex", quiet = FALSE)
path |
A package source code working directory |
context |
A |
quiet |
Whether to emit output messages. |
The result of writeLines()
after writing a new testthat
file.
roxygen2
Checks for use of roxygen2
uses_roxygen2(path)
uses_roxygen2(path)
path |
A file path to a package source code directory |
A logical value indicating whether a package takes roxygen2
as
a dependency.
This function is primarily for managing attaching of namespaces needed for
testing internally. It is exported only because it is needed in code
generated within Rd
files, but is otherwise unlikely to be needed.
with_attached(ns, expr)
with_attached(ns, expr)
ns |
A namespace or namespace name to attach |
expr |
An expression to evaluate while the namespace is attached |
The result of evaluating expr
testthat
Expectations With A Known Source ReferenceRetroactively assigns a source file and location to a expectation. This
allows testthat
to report an origin for any code that raised an example
test failure from the source roxygen2
code, even though the test code is
reconstructed from package documentation files.
with_srcref(src, expr, envir = parent.frame())
with_srcref(src, expr, envir = parent.frame())
src |
A |
expr |
An expression to be evaluated. If an |
envir |
An environment in which to evaluate |
The result of evaluating expr
, or an expectation with appended
srcref
information if an expectation is raised.