Skip to content

Commit d369373

Browse files
authored
Improvements to BDD (#1763)
* Export `it()` to make interactive testing easier. Fixes #1587. * Call `use_test_context()` in `it()`. Fixes #1731 Adds rlang helpers which need a hack because of the old `is_null()` expectation.
1 parent b54e842 commit d369373

9 files changed

+965
-30
lines changed

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Imports:
3333
processx,
3434
ps (>= 1.3.4),
3535
R6 (>= 2.2.0),
36-
rlang (>= 1.0.1),
36+
rlang (>= 1.1.0),
3737
utils,
3838
waldo (>= 0.4.0),
3939
withr (>= 2.4.3)

NAMESPACE

+1
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ export(is_null)
147147
export(is_parallel)
148148
export(is_testing)
149149
export(is_true)
150+
export(it)
150151
export(local_edition)
151152
export(local_mock)
152153
export(local_mocked_bindings)

NEWS.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# testthat (development version)
22

3+
* `it()` now calls `local_test_context()` so that it behaves more
4+
similarly to `test_that()` (#1731), and is now exported so that you
5+
can more easily run BDD tests interactively (#1587)
6+
37
* `with_mocked_bindings()` and `local_mocked_bindings()` can now bind in the
48
imports namespace too.
59

R/describe.R

+27-20
Original file line numberDiff line numberDiff line change
@@ -56,32 +56,39 @@
5656
#' })
5757

5858
describe <- function(description, code) {
59-
is_invalid_description <- function(description) {
60-
!is.character(description) || length(description) != 1 ||
61-
nchar(description) == 0
62-
}
63-
64-
if (is_invalid_description(description)) {
65-
stop("description must be a string of at least length 1")
66-
}
59+
check_string(description, allow_empty = FALSE)
60+
describe_description <- description
6761

6862
# prepares a new environment for each it-block
6963
describe_environment <- new.env(parent = parent.frame())
70-
describe_environment$it <- function(it_description, it_code = NULL) {
71-
if (is_invalid_description(it_description)) {
72-
stop("it-description must be a string of at least length 1")
73-
}
74-
if (missing(it_code)) return()
64+
describe_environment$it <- function(description, code = NULL) {
65+
check_string(description, allow_empty = FALSE)
66+
code <- substitute(code)
7567

76-
test_description <- paste0(description, ": ", it_description)
77-
test_code(
78-
test_description,
79-
substitute(it_code),
80-
env = describe_environment,
81-
skip_on_empty = FALSE
82-
)
68+
description <- paste0(describe_description, ": ", description)
69+
describe_it(description, code, describe_environment)
8370
}
8471

8572
eval(substitute(code), describe_environment)
8673
invisible()
8774
}
75+
76+
describe_it <- function(description, code, env = parent.frame()) {
77+
local_test_context()
78+
79+
test_code(
80+
description,
81+
code,
82+
env = env,
83+
skip_on_empty = FALSE
84+
)
85+
}
86+
87+
#' @export
88+
#' @rdname describe
89+
it <- function(description, code = NULL) {
90+
check_string(description, allow_empty = FALSE)
91+
92+
code <- substitute(code)
93+
describe_it(description, code)
94+
}

0 commit comments

Comments
 (0)