I have a shiny application which uses like 4 functions. I would like to test these functions but it's not a package. How am i supposed to structure my code ? and execute these tests without devtools ?
2 Answers
You can execute tests with testthat::test_dir()
or testthat::test_file()
. Neither relies on the code being in a package, or using devtools, just the testthat package.
There are few requirements on how to structure your code.
If it were me, I would create a tests
directory and add my test scripts under there, which would look something like:
|- my_shiny_app
| |- app.R
| |- tests
| |- test_foo.R
| |- test_bar.R
Then you can run your tests with test_dir('tests')
, assuming you're in the my_shiny_app
directory.
Your test scripts will have they same structure they have for packages but you'd replace the library()
call with source()
referencing the file where your functions are defined.

- 745
- 8
- 10
-
The above link is broken, but I think the new link is likely [Test Mechanics and Workflow](https://r-pkgs.org/testing-basics.html#sec-tests-mechanics-workflow). – steveb Aug 11 '22 at 19:20
If you have few functions without a package structure, it is better to write single test files manually (so with some simple if/error catching system) that you call with Rscript test_file1.R
.
If you start to use the package format instead (which would be advisable for further 'safe' developing) and you still do not want to use testthat
, I advise you to follow this blog post: here

- 1,088
- 16
- 29