18

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
  • why without devtools?
    – drmariod
    Aug 9, 2017 at 10:13
  • @drmariod : i can't use it in my R project and don't know why and how to configure it without creating a package ...
    – Q. Eude
    Aug 9, 2017 at 10:14

2 Answers 2

Reset to default

Trending sort

Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.

It falls back to sorting by highest score if no posts are trending.

21

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.

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.