Run unit tests with testthat without package
Asked Answered
M

2

23

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 ?

Malawi answered 9/8, 2017 at 10:9 Comment(2)
why without devtools?Flood
@Flood : i can't use it in my R project and don't know why and how to configure it without creating a package ...Malawi
A
29

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.

Almeta answered 16/8, 2017 at 11:10 Comment(1)
The above link is broken, but I think the new link is likely Test Mechanics and Workflow.Fickle
T
2

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

Toxic answered 9/8, 2017 at 10:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.