Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provide a custom testthat skipper #195

Merged
merged 10 commits into from
Nov 20, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export(real_http_connections_allowed)
export(request_summary)
export(response_summary)
export(serializer_fetch)
export(skip_if_vcr_off)
export(turn_off)
export(turn_on)
export(turned_off)
Expand Down
26 changes: 26 additions & 0 deletions R/skip-vcr-off.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#' Skip tests if vcr is off
maelle marked this conversation as resolved.
Show resolved Hide resolved
#'
#' Custom testthat skippers to skip tests if vcr is turned off via the
#' environment variable `VCR_TURN_OFF`.
#'
#' @details This might be useful if your test will fail when the cassette was e.g.
#' edited (a real request produced a 200 status code but you made it a 502
#' status code for testing the behavior of your code when the API errors)
#' or if the tests are very specific (e.g. testing
#' a date was correctly parsed, but making a real request would produce a different
#' date).
#'
#' @return Nothing, skip test.
#' @export
#'
#' @seealso lightswitch
sckott marked this conversation as resolved.
Show resolved Hide resolved
skip_if_vcr_off <- function() {

if (is(try(find.package("testthat"), silent = TRUE), "try-error")) {
sckott marked this conversation as resolved.
Show resolved Hide resolved
maelle marked this conversation as resolved.
Show resolved Hide resolved
stop("This function is meant to be use within testthat tests. Install testthat.")
}

if (nzchar(Sys.getenv("VCR_TURN_OFF")) && as.logical(Sys.getenv("VCR_TURN_OFF"))) {
testthat::skip("Not run when vcr is off")
sckott marked this conversation as resolved.
Show resolved Hide resolved
}
}
26 changes: 26 additions & 0 deletions man/skip_if_vcr_off.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions tests/testthat/test-skip-vcr-off.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
test_that("skip_if_vcr_off works when vcr on", {
expect_silent(skip_if_vcr_off())
})

test_that("skip_if_vcr_off works when vcr off", {
withr::local_envvar("VCR_TURN_OFF" = TRUE)
expect_error(skip_if_vcr_off(), class = "skip")
maelle marked this conversation as resolved.
Show resolved Hide resolved
sckott marked this conversation as resolved.
Show resolved Hide resolved
})