Skip to content

Commit

Permalink
Make test_subdir() work with testthat 3 and parallel testing (closes
Browse files Browse the repository at this point in the history
  • Loading branch information
s-fleck committed Feb 15, 2022
1 parent 9b4b06d commit f64c170
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 147 deletions.
3 changes: 3 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,6 @@ Suggests:
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.1.9000
Config/testthat/edition: 3
Config/testthat/parallel: true

1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* When checking for RStudio, check if RStudio is actually running, and not just
if **rstudioapi** is installed. This fixes bugs with non-interactive use of
`test_subdir()` and co.
* update to work with testthat 3 (#18)


# testthis 1.1.1
Expand Down
11 changes: 6 additions & 5 deletions R/test_subdir.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@ test_subdir <- function(subdir, ...){
rstudioapi::documentSaveAll()
}

devtools::load_all(usethis::proj_get())

testthat::test_dir(file.path(
usethis::proj_get(), testthat::test_path(), subdir
))
project <- usethis::proj_get()
devtools::load_all(project)

testthat::test_dir(
file.path(usethis::proj_get(), testthat::test_path(), subdir),
package = devtools::as.package(project)[["package"]]
)
}


Expand Down
44 changes: 0 additions & 44 deletions tests/testthat/integration_tests/test_Test_coverage.R
Original file line number Diff line number Diff line change
@@ -1,47 +1,3 @@
context("Test_coverage")


test_that("get_pkg_tested_functions_from_desc", {

list_test_files_mock <- function(...) {
paste0("/blah/blubb/tests/testthat/", c("test_foo", "test_bar"))
}

extract_test_that_desc_mock <- function(...){
list(
c("foofun1", "testing foofun2"),
c("barfun1", "barfun2", "foofun1")
) %>%
setNames(list_test_files_mock())
}

get_pkg_functions_mock <- function(...){
c("foofun1", "foofun2", "barfun1", "barfun2")
}


tres <- with_mock(
get_pkg_tested_functions_from_desc(),
list_test_files = list_test_files_mock,
extract_test_that_desc = extract_test_that_desc_mock,
get_pkg_functions = get_pkg_functions_mock
)


expect_identical(
tres,
list(
foofun1 =
c("/blah/blubb/tests/testthat/test_foo", "/blah/blubb/tests/testthat/test_bar"),
foofun2 = "/blah/blubb/tests/testthat/test_foo",
barfun1 = "/blah/blubb/tests/testthat/test_bar",
barfun2 = "/blah/blubb/tests/testthat/test_bar")
)
})




test_that("test_coverage works", {
expect_silent(test_coverage())
expect_silent(test_coverage(from_tags = FALSE))
Expand Down
4 changes: 1 addition & 3 deletions tests/testthat/integration_tests/test_get_pkg_functions.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
context("get_package_functions")


proj <- rprojroot::find_package_root_file()

test_that('get_pkg_tested_functions_from_desc works', {
skip("Update to user mockery")
#* @testing get_pkg_tested_functions_from_desc
tpkg <- file.path(rprojroot::find_testthat_root_file("testdata", "test_pkg", "tests", "testthat"))
usethis::proj_set(tpkg)
Expand Down
35 changes: 0 additions & 35 deletions tests/testthat/integration_tests/test_list_test_files.R

This file was deleted.

30 changes: 0 additions & 30 deletions tests/testthat/integration_tests/test_use_testignore.R

This file was deleted.

1 change: 0 additions & 1 deletion tests/testthat/test_Taglist.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
context("test_Taglist")
#* @skip


Expand Down
25 changes: 14 additions & 11 deletions tests/testthat/test_get_test_index.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
context("get_test_index")




setup({
setup <- function(){
tenv <- parent.env(environment())
proj_old <- tryCatch(usethis::proj_get(), error = function(e) NULL)
assign("proj_old", proj_old, tenv)
Expand All @@ -13,20 +8,22 @@ setup({
dir <- find_testdata("test_pkg", must_exist = TRUE)
fs::dir_copy(dir, proj_test)
usethis::proj_set(proj_test)
})

}



teardown({
teardown <- function(){
usethis::proj_set(proj_old)
unlink(td, recursive = TRUE)
})
}





test_that("extract_testthat_parse_data works as expected", {
setup()
on.exit(teardown())

# works with text
txt <- "test_that(\n\n'blubb')"
x <- extract_testthat_parse_data(txt)
Expand All @@ -47,6 +44,9 @@ test_that("extract_testthat_parse_data works as expected", {


test_that("extract_testthat_desc works as expected", {
setup()
on.exit(teardown())

# works with text
txt <- "test_that(\n\n'blubb')"
x <- extract_testthat_desc(txt)
Expand All @@ -72,6 +72,9 @@ test_that("extract_testthat_desc works as expected", {


test_that("collect_testthat_source_info works as expected", {
setup()
on.exit(teardown())

src <-
find_testdata("test_pkg", "tests", "testthat", "testthat_parse_cases.R")

Expand Down
21 changes: 13 additions & 8 deletions tests/testthat/test_infrastructure.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
context("infrastructure-non cran tests")


setup({
setup <- function(){
tenv <- parent.env(environment())
proj_old <- tryCatch(usethis::proj_get(), error = function(e) NULL)
assign("proj_old", proj_old, tenv)
Expand All @@ -11,20 +9,21 @@ setup({
dir <- find_testdata("test_pkg", must_exist = TRUE)
fs::dir_copy(dir, proj_test)
usethis::proj_set(proj_test)
})


}


teardown({
teardown <- function(){
usethis::proj_set(proj_old)
unlink(td, recursive = TRUE)
})
}




test_that("use_testdata creates testdata dir", {
setup()
on.exit(teardown())

#* @testing has_testdata
#* @testing use_testdata
package_state <- list.files(proj_test, recursive = TRUE)
Expand Down Expand Up @@ -84,6 +83,9 @@ test_that("use_testdata creates testdata dir", {


test_that("use_tester works as expected", {
setup()
on.exit(teardown())

package_state <- list.files(proj_test, recursive = TRUE)

expect_true(dir.exists(proj_test))
Expand Down Expand Up @@ -114,6 +116,9 @@ test_that("use_tester works as expected", {


test_that("use_test_subdir works as expected", {
setup()
on.exit(teardown())

#* @testing use_test_subdir
#* @testing use_acceptance_tests
#* @testing use_integration_tests
Expand Down
20 changes: 10 additions & 10 deletions tests/testthat/test_use_testdata.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
context("use_testdata")



test_that("test_parse_testthis_comments works as expected", {
x <- read_testdata("read_testdata_testfile.rds")
expect_identical(x, "185")
})



setup({
setup <- function(){
tenv <- parent.env(environment())
proj_old <- tryCatch(usethis::proj_get(), error = function(e) NULL)
assign("proj_old", proj_old, tenv)
Expand All @@ -19,20 +15,21 @@ setup({
dir <- find_testdata("test_pkg", must_exist = TRUE)
fs::dir_copy(dir, proj_test)
usethis::proj_set(proj_test)
})

}



teardown({
teardown <- function(){
usethis::proj_set(proj_old)
unlink(td, recursive = TRUE)
})
}




test_that("saving and loading testdata works", {
setup()
on.exit(teardown())

# Check for clean state
package_state <- list.files(proj_test, recursive = TRUE)
efile <- file.path(proj_test, "tests", "testthat", "testdata", "iris.rds")
Expand All @@ -57,6 +54,9 @@ test_that("saving and loading testdata works", {


test_that("use_testdata_raw works", {
setup()
on.exit(teardown())

package_state <- list.files(proj_test, recursive = TRUE)
efile <- file.path(proj_test, "tests", "testthat", "testdata-raw")

Expand Down

0 comments on commit f64c170

Please sign in to comment.