简体   繁体   中英

Run testthat's `test_that`s within a script

When I write simple and quick code, possibly for exploration or helping others in debugging, I would love to test my code and share my tests with my peers.

Setup a complete {testthat} environment is not that hard to do, but it has a high friction in sharing the results. Moreover, for a rapid exploration, having everything in a single script could help understand all the ideas and mental processes leading to the solution. That could also help share snippets in chats, eg, TG/WA/... .

My current solution is to set up the following two scripts during development and share code.R 's content (or file) at the end.

## myproj/code.R
# code ---------------------
if (fun_1() + fun_2() == 3) {
  message("good job")
}

# funs ---------------------
fun_1 <- function() 1  # OK
fun_2 <- function() 3  # KO

# tests --------------------
test_that("fun_1 works", {
  expect_equal(fun_1(), 1)
})
test_that("fun_2 works", {
  expect_equal(fun_2(), 2)
})
## myproj/run_tests.R
testthat::test_file("code.R")

My question is: there is a way to run all the test_that s tests in the local session within the current global environment, so that everything will be self-contained (even the option to run all the tests), and one (eg, me) can run all the tests in a single key-stroke combination resulting in the standard {testthat} reporter's output?

I'm thinking about something like the following.

## myproj/code.R
# code ---------------------
if (fun_1() + fun_2() == 3) {
  message("good job")
}

# funs ---------------------
fun_1 <- function() 1  # OK
fun_2 <- function() 3  # KO

# tests --------------------
with_reporter(default_reporter(), {
  test_that("fun_1 works", {
    expect_equal(fun_1(), 1)
  })
  test_that("fun_2 works", {
    expect_equal(fun_2(), 2)
  })
})

But it works if all tests pass only.

Note: selecting and running only

test_that("fun_2 works", {
  expect_equal(fun_2(), 2)
})

works perfectly fine, providing the information about the error.

Instead of default_reporter , using check_reporter works fine:

## myproj/code.R
library(testthat)

# code ---------------------
if (fun_1() + fun_2() == 3) {
  message("good job")
}
#> Error in fun_1(): could not find function "fun_1"

# funs ---------------------
fun_1 <- function() 1  # OK
fun_2 <- function() 3  # KO

# tests --------------------
with_reporter(check_reporter(), {
  test_that("fun_1 works", {
    expect_equal(fun_1(), 1)
  })
  test_that("fun_2 works", {
    expect_equal(fun_2(), 2)
  })
})
#> == Failed tests ================================================================
#> -- Failure (<text>:19:5): fun_2 works ------------------------------------------
#> fun_2() not equal to 2.
#> 1/1 mismatches
#> [1] 3 - 2 == 1
#> 
#> [ FAIL 1 | WARN 0 | SKIP 0 | PASS 1 ]

Created on 2021-12-27 by the reprex package (v2.0.1)

Session info
sessioninfo::session_info() #> - Session info --------------------------------------------------------------- #> setting value #> version R version 4.1.2 (2021-11-01) #> os Windows 10 x64 (build 22523) #> system x86_64, mingw32 #> ui RTerm #> language (EN) #> collate English_United States.1252 #> ctype English_United States.1252 #> tz Europe/Berlin #> date 2021-12-27 #> pandoc 2.14.0.3 @ C:/Bin/RStudio/bin/pandoc/ (via rmarkdown) #> #> - Packages ------------------------------------------------------------------- #> package * version date (UTC) lib source #> cli 3.1.0 2021-10-27 [1] CRAN (R 4.1.2) #> crayon 1.4.2 2021-10-29 [1] CRAN (R 4.1.2) #> desc 1.4.0 2021-09-28 [1] CRAN (R 4.1.2) #> digest 0.6.29 2021-12-01 [1] CRAN (R 4.1.2) #> evaluate 0.14 2019-05-28 [1] CRAN (R 4.1.2) #> fastmap 1.1.0 2021-01-25 [1] CRAN (R 4.1.2) #> fs 1.5.2 2021-12-08 [1] CRAN (R 4.1.2) #> glue 1.6.0 2021-12-17 [1] CRAN (R 4.1.2) #> highr 0.9 2021-04-16 [1] CRAN (R 4.1.2) #> htmltools 0.5.2 2021-08-25 [1] CRAN (R 4.1.2) #> knitr 1.37 2021-12-16 [1] CRAN (R 4.1.2) #> magrittr 2.0.1 2020-11-17 [1] CRAN (R 4.1.2) #> pkgload 1.2.4 2021-11-30 [1] CRAN (R 4.1.2) #> R6 2.5.1 2021-08-19 [1] CRAN (R 4.1.2) #> reprex 2.0.1 2021-08-05 [1] CRAN (R 4.1.2) #> rlang 0.4.12 2021-10-18 [1] CRAN (R 4.1.2) #> rmarkdown 2.11 2021-09-14 [1] CRAN (R 4.1.2) #> rprojroot 2.0.2 2020-11-15 [1] CRAN (R 4.1.2) #> rstudioapi 0.13 2020-11-12 [1] CRAN (R 4.1.2) #> sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.1.2) #> stringi 1.7.6 2021-11-29 [1] CRAN (R 4.1.2) #> stringr 1.4.0 2019-02-10 [1] CRAN (R 4.1.2) #> testthat * 3.1.1 2021-12-03 [1] CRAN (R 4.1.2) #> withr 2.4.3 2021-11-30 [1] CRAN (R 4.1.2) #> xfun 0.29 2021-12-14 [1] CRAN (R 4.1.2) #> yaml 2.2.1 2020-02-01 [1] CRAN (R 4.1.1) #> #> [1] C:/Bin/R/R-4.1.2/library #> #> ------------------------------------------------------------------------------

Moreover, I have just discovered that if I write tests on a script, in RStudio, a button appears to facilitate the testing:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM