rtestingshinytestthat

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 ?


Solution

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