Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run unit tests with testthat without package

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 ?

like image 348
Q. Eude Avatar asked Aug 09 '17 10:08

Q. Eude


1 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.

like image 179
Dylan Avatar answered Oct 19 '22 13:10

Dylan