1

If I have tests/testhat/testhat.R, devtools::test() finds it and runs it, but covr:package_coverage() and R CMD check does not find it.

If I have it as tests/testhat.R, devtools::test() doesn't find it but covr:package_coverage() and R CMD check does.

What's the best way to do this?

R 4.0.0; testthat 2.3.2; covr 3.5.0

mindlessgreen
  • 11,059
  • 16
  • 68
  • 113
  • You can specify a path as the first argument to `devtools::test()`. Without testing it, my guess is use the second directory structure. Besides, from the manual: *`test()` is a shortcut for `testthat::test_dir()`*. – Rui Barradas Jul 12 '20 at 19:16
  • Have you tried using `tests` instead of `test`? That's the directory I get automatically when adding tests via `usethis::use_test` or `usethis::use_testthat`. – stefan Jul 12 '20 at 19:38
  • Ah sorry! I am using `tests` folder as the parent. I mistyped here. Correcting it now. – mindlessgreen Jul 12 '20 at 19:40

1 Answers1

2

Your directory structure of the test folder should look like this:

.
├── testthat
│   ├── test-1.R
│   ├── test-2.R
│   ├── test-3.R
│   ├── test-4.R
│   └── test-5.R
└── testthat.R

And testthat.R contains

library(testthat)
library(mypackage)

test_check("mypackage")

This works with R CMD check, covr and devtools::check().

mindlessgreen
  • 11,059
  • 16
  • 68
  • 113
novica
  • 655
  • 4
  • 11
  • How does this help? All the tests inside testthat folder do not run during R CMD check or for code coverage. – mindlessgreen Jul 12 '20 at 19:36
  • Maybe there is something wrong with the tests? The structure is that one. See http://r-pkgs.had.co.nz/tests.html for more or see how it's done here: https://github.com/tidyverse/ggplot2/tree/master/tests/testthat – novica Jul 12 '20 at 20:14
  • Ah! I see. In the example you linked, `testthat.R` has this code `test_check("ggplot2")` which I suppose runs all the individual tests inside the `testthat` folder. – mindlessgreen Jul 12 '20 at 20:34