Skip to main content

Tests

Published

2026-11-25

Expand the callout boxes below to review what we’ve covered in the previous chapters on debugging:

Testing your app

We’re now ready to test the code and features in our app-package.

New topics

I’ve updated this section for version 4.0.0 of covr and version 0.6.0 of shinytest2. Both releases change how we run and measure tests in an app-package, so I’ve added the topics below:

  • System test coverage: covr::package_coverage() now counts the app code our shinytest2 tests run. In sap, the system tests raise test coverage from 25% to almost 80%.

  • Branch coverage: covr can now report which branches of an if statement our tests take, not just which lines they run.

  • shinytest2 and BDD: our system tests now pass the launch_app function to AppDriver$new(). This lets the tests run against the installed package, which is how R CMD check and covr run them.

  • Setting up shinytest2: use_shinytest2() no longer creates a setup-shinytest2.R file, because load_app_env() has been superseded.

Before covr 4.0.0, only our unit and module tests counted toward test coverage. The diagram below shows how the system tests now reach the code the other tests can’t:

%%{init: {'theme': 'base', 'themeVariables': { 'fontFamily': 'monospace', "fontSize":"16px", "primaryColor":"#D7EBEC", "primaryTextColor":"#1B3B3F", "primaryBorderColor":"#2A6F77", "lineColor":"#5A7F83", "edgeLabelBackground":"#FFFFFF", "clusterBkg":"#F3F8F8", "clusterBorder":"#2A6F77", "darkMode":false}}}%%

flowchart TD
    subgraph Tests["<strong>tests/testthat/</strong>"]
        U("Unit tests<br><code>test_that()</code>")
        M("Module tests<br><code>testServer()</code>")
        S("System tests<br><code>AppDriver</code>")
    end
    subgraph Code["<strong>R/</strong>"]
        F("Utility functions")
        MS("Module server functions")
        APP("<code>launch_app()</code>,<br>UI, and server")
    end
    U --> F
    M --> MS
    S --> MS
    S -->|"new in covr 4.0.0"| APP
    F --> COV[/"<code>covr::package_coverage()</code>"/]
    MS --> COV
    APP --> COV

    classDef term fill:#2A6F77,color:#FFFFFF,stroke:#1B4F55,stroke-width:1px
    classDef step fill:#D7EBEC,color:#1B3B3F,stroke:#2A6F77,stroke-width:1px
    classDef decision fill:#F4C26B,color:#3D2A00,stroke:#B7832B,stroke-width:1px
    classDef io fill:#FFFFFF,color:#1B3B3F,stroke:#2A6F77,stroke-width:1px
    class U,M,S,F,MS,APP step
    class COV io
    style Tests fill:#F3F8F8,color:#1B3B3F,stroke:#2A6F77,stroke-width:1px,stroke-dasharray:4 3
    style Code fill:#F3F8F8,color:#1B3B3F,stroke:#2A6F77,stroke-width:1px,stroke-dasharray:4 3

Tests that count toward coverage with covr 4.0.0

The chapters in this section cover the following topics:

Test suite

Test suite starts by showing how to create the testthat infrastructure then, dives into creating test files, the mechanics unit tests and expectations, and running tests during development:

Specifications

Specifications discusses how to collect and organize information to write tests for your application:

This chapter also gives examples of behavior-driven development tests features and how to track specifications and tests together.

Test tools

Test tools was updated recently to include the new ensure package, which was designed for “drafting testthat unit testing code using LLMs.”

Each tool can contribute to testing your Shiny app-package in a unique way (they’re also relatively recent additions to the testthat framework).

Testing modules

Shiny’s testServer() function can be useful for performing integration tests with module server functions. Testing modules gives examples of using testServer() to verify module communication (i.e., passing data between modules):

System tests

The final chapter in this section covers setting up and running system (or end-to-end tests with shinytest2):

Please open an issue on GitHub