R/Shiny Unit Testing

Hi there,

I'm making a Shiny application for my University project and was wondering if anyone had a project structure for unit tests within a Shiny application? Most guides I find online explain testing with testthat in terms of testing packages and not a Shiny app which in my case is just a ui.R, server.R and some helper functions in other R files.

removed pipelines question

Sorry for 2 slightly different topics in one post, they both rely on the testing.

Many thanks!

Ciaran

Hi @ciaranevans, I am going to talk about tests only, I've never used bitbucket pipelines, and after your mention I am quite curious to find out what this is all about.

I think it is really hard to test shiny applications. But I do believe that writing tests is only way to have my head resting easily in my pillow after I deploy some code. Like the Uncle Bob Martin says: the tests eliminate fear (link).

That said, I strongly suggest you to write a package with the functionalities you want to expose and build your shiny app as a client of that package.

Packages in R are easy and reliable to test with testthat and you can skip all that CHECK process, given that this is to be used as a support of a shiny app.

From my point of view, testing the application is tough, I would suggest using selenium so that you could check the widgets response directly. But it does not invalidate the tests applied to your internal package.

I hope this helps.

3 Likes

Take a look at shinytest package. I think, it is exactly what you are looking for (as far as first question is concerned).

2 Likes

Hi @wilsonfreitas,

Thanks for the reply! Good suggestion on the extraction of the functionality into a separate package. I'll take a look into it! I noticed shinytest has a recording function but it probably won't be much use to me as my app relies on 3rd party data which can change at any point so I'd have failing frontend tests left right and centre.

In terms of Pipelines, I find it a nice lightweight alternative to TravisCI and Jenkins: Pipelines

For example, to build my Dockerised Shiny Server image and push it to Dockerhub on commit I just need a bitbucket-pipelines.yml file in the base of the repo, all it contains is:

options:
  docker: true

pipelines:
  default:
    - step:
        script:
          - docker login -u $DOCKER_HUB_USERNAME -p $DOCKER_HUB_PASSWORD
          - docker build -t $DOCKER_HUB_REPO:$PROJECT-$BITBUCKET_COMMIT .
          - docker push $DOCKER_HUB_REPO

@mishabalyasin thanks for the suggestion, gonna mark this as solved. I'm not sure shinytest will be much use because of the constant changing data returned from 3rd parties. But I'll certainly split out my functionality so it can be tested separately from the main Shiny code.

Going to move the Pipelines section to a separate post.

Thanks,

Ciaran

1 Like