Test Coverage for Shiny Apps with covr 4.0.0

Branch coverage, shiny_app_coverage(), and coverage_to_markdown()

Testing
Shiny
Author

Martin Frigaard

Published

November 29, 2026

In Testing Non-Package Shiny Apps I wrote unit tests and testServer() tests for a small app that wasn’t an R package. The tests ran, but I had no way to see how much of the app they reached. covr measured coverage for packages only, and even inside a package, the code run by shinytest2 tests didn’t count.

Version 4.0.0 of covr changes both of these. In this post I’ll cover three additions:

  1. shiny_app_coverage() measures coverage for an app that isn’t in a package.
  2. Branch coverage reports which side of each if and switch() our tests take.
  3. coverage_to_markdown() turns a coverage result into a table for a pull request or a GitHub Actions summary.

I’ve also updated the testing chapters in Shiny App-Packages for this release. I’ll point to those sections as we go, since the book covers the package side in more detail.

Which function should you use?

The choice depends on where the app’s code lives. package_coverage() installs an instrumented copy of a package and runs its tests. Every R process that loads the package (including the one a shinytest2 AppDriver starts) loads that instrumented copy, so the system tests now count.

shiny_app_coverage() handles apps outside a package. It instruments the functions in app.R, ui.R, server.R, global.R, and the R/ folder when Shiny loads them. Then it runs the app’s tests (by default with shinytest2::test_app()).

The diagram below shows the two paths:

%%{init: {'theme': 'base', 'themeVariables': { 'fontFamily': 'monospace', 'darkMode':false}}}%%
flowchart TD
    Start(["Shiny app"]) --> Pkg{"Is the app<br>in a package?"}
    Pkg -->|"yes"| PC[["<code>package_coverage()</code>"]]
    Pkg -->|"no"| SAC[["<code>shiny_app_coverage()</code>"]]
    PC --> Inst("Installs an<br>instrumented copy")
    SAC --> Load("Instruments functions<br>as Shiny loads them")
    Inst --> Run("Runs unit, module,<br>and system tests")
    Load --> Run
    Run --> Cov[/"Coverage object"/]
    Cov --> Print("<code>print()</code> or<br><code>report()</code>")
    Cov --> MD("<code>coverage_to_markdown()</code>")

    style Start fill:#FFFFFF,color:#000000,stroke:#333
    style Pkg fill:#FFFFFF,color:#000000,stroke:#333
    style PC fill:#4CBB9D,color:#FFFFFF,rx:5,ry:5
    style SAC fill:#4CBB9D,color:#FFFFFF,rx:5,ry:5
    style Inst fill:#FFFFFF,color:#000000,stroke:#333,rx:5,ry:5
    style Load fill:#FFFFFF,color:#000000,stroke:#333,rx:5,ry:5
    style Run fill:#FFFFFF,color:#000000,stroke:#333,rx:5,ry:5
    style Cov fill:#FFFFFF,color:#000000,stroke:#333
    style Print fill:#FFFFFF,color:#000000,stroke:#333,rx:5,ry:5
    style MD fill:#FFFFFF,color:#000000,stroke:#333,rx:5,ry:5

Measuring coverage with covr 4.0.0

For an app in a package, the System test coverage section of the book walks through package_coverage(). In the book’s sap package, the system tests raise coverage from about 25% to almost 80%. The rest of this post uses shiny_app_coverage() on the app from the previous post.

Moving the ABC app into R/

The ABC app from the previous post kept its module and utility functions in modules.R and utils.R at the top of the project, and app.R loaded them with source(). I rebuilt that layout and ran shiny_app_coverage() on it:

[ FAIL 0 | WARN 0 | SKIP 0 | PASS 6 ]
Coverage: 14.29%
app.R: 14.29%

All six tests passed, but modules.R and utils.R don’t appear in the output. covr only instruments the files Shiny loads itself, and a file read with source() isn’t one of them. Moving both files into an R/ folder fixes this, because Shiny loads R/ automatically:

abc/
├── app.R
├── R/
│   ├── modules.R
│   └── utils.R
└── tests/
    ├── testthat/
    │   ├── setup-shinytest2.R
    │   ├── test-app.R
    │   ├── test-mod_abc_server.R
    │   └── test-num_super_script.R
    └── testthat.R

With the functions in R/, app.R no longer needs the source() calls:

app.R
library(shiny)

launchApp <- function() {
  shinyApp(
    ui = fluidPage(
      h2("ABC App"),
      fluidRow(
        mod_abc_ui("x")
      )
    ),
    server = function(input, output, session) {
      mod_abc_server("x")
    }
  )
}
launchApp()
WarningUse R/, not source()

If an app outside a package loads its functions with source(), shiny_app_coverage() reports coverage for app.R alone. Move the files into R/ before measuring coverage.

Measuring coverage with shiny_app_coverage()

The tests need the app’s functions too. shinytest2 0.6.0 no longer creates a setup file with load_app_env(), so I added tests/testthat/setup-shinytest2.R myself. It loads the files in R/ with shiny::loadSupport():

tests/testthat/setup-shinytest2.R
library(shiny)
shiny::loadSupport("../../", renv = globalenv())

The unit and module tests are the same as in the previous post. I also added one system test with shinytest2. It sets the number to 26 and checks the app returns Z:

tests/testthat/test-app.R
test_that("abcApp shows the 26th letter", {
  app <- shinytest2::AppDriver$new(name = "abc")
  app$set_inputs(`x-num` = 26)
  txt <- app$get_value(output = "x-txt")
  expect_match(txt$html, "Z")
  app$stop()
})
1
The app process saves its coverage when it exits, so stop it with app$stop().

AppDriver$new() skips tests on CRAN unless NOT_CRAN is "true", and covr doesn’t set it. I set it before calling shiny_app_coverage():

Sys.setenv(NOT_CRAN = "true")
cov <- covr::shiny_app_coverage("abc")
cov
[ FAIL 0 | WARN 0 | SKIP 0 | PASS 6 ]
Coverage: 81.25%
app.R: 14.29%
R/utils.R: 80.00%
R/modules.R: 100.00%

What the system test adds

To see the difference, I removed test-app.R and ran it again:

[ FAIL 0 | WARN 0 | SKIP 0 | PASS 5 ]
Coverage: 56.10%
R/modules.R: 42.31%
R/utils.R: 80.00%

The system test raises total coverage from 56% to 81%. Most of the gain is in modules.R. testServer() never builds the UI module, and it never renders output$txt, so only the running app reaches that code.

Why is app.R at 14%?

zero_coverage() lists the lines our tests never ran:

covr::zero_coverage(cov)[, c("filename", "functions", "line")]
    filename        functions line
1      app.R        launchApp    4
2      app.R        launchApp    5
3      app.R        launchApp    6
4      app.R        launchApp    7
5      app.R        launchApp    8
6      app.R        launchApp   11
36 R/utils.R num_super_script    4
38 R/utils.R num_super_script    6
40 R/utils.R num_super_script    8

The body of launchApp() shows as never run, even though the app clearly launched. I think this is because app.R calls launchApp() as it loads, before covr has instrumented the function. The server function inside it was instrumented, which is the single line counted in the 14%. I treat the app.R number as a floor rather than a gap in the tests.

The three lines in R/utils.R are the error branches and the num == 0 branch of num_super_script(). None of our tests pass a number outside 1 to 26, and line coverage alone doesn’t tell us much more than that. Branch coverage does.

Branch coverage

Line coverage tells us whether each line ran. A line with an if counts as covered even if only one side of the condition was ever taken. covr 4.0.0 adds experimental branch coverage, which records the TRUE and FALSE side of every if and each arm of a switch().

Branch coverage is turned on with an option, set before measuring coverage:

Sys.setenv(NOT_CRAN = "true")
options(covr.branch_coverage = TRUE)
cov <- covr::shiny_app_coverage("abc")
cov
[ FAIL 0 | WARN 0 | SKIP 0 | PASS 6 ]
Coverage: 81.25% (branches 75.00%)
app.R: 14.29%
R/utils.R: 80.00%
R/modules.R: 100.00%

The line coverage is the same, and the branch percentage is added to the first line.

Finding untested branches

tally_coverage() with by = "branch" shows how often each branch was taken:

tb <- covr::tally_coverage(cov, by = "branch")
tb[tb$filename == "R/utils.R", ]
    filename        functions line column branch label value
1  R/utils.R num_super_script    3      3      1  TRUE     0
2  R/utils.R num_super_script    3      3      2 FALSE     4
3  R/utils.R num_super_script    5     10      1  TRUE     0
4  R/utils.R num_super_script    5     10      2 FALSE     4
5  R/utils.R num_super_script    7     10      1  TRUE     0
6  R/utils.R num_super_script    7     10      2 FALSE     4
7  R/utils.R num_super_script    9     10      1  TRUE     1
8  R/utils.R num_super_script    9     10      2 FALSE     3
9  R/utils.R num_super_script   11     10      1  TRUE     1
10 R/utils.R num_super_script   11     10      2 FALSE     2
11 R/utils.R num_super_script   13     10      1  TRUE     1
12 R/utils.R num_super_script   13     10      2 FALSE     1

Each if gets two rows. Lines 3, 5, and 7 have a TRUE value of 0: our tests never pass a negative number, a number over 26, or zero. These are the same gaps zero_coverage() found, but now they’re tied to the condition that leads to them. That makes it clear which tests to write next:

tests for the untaken branches
test_that("num_super_script() handles edge cases", {
  expect_error(num_super_script(-1), "not a valid number")
  expect_error(num_super_script(27), "not a valid number")
  expect_equal(num_super_script(0), "")
})

With these three expectations added, every branch is taken:

[ FAIL 0 | WARN 0 | SKIP 0 | PASS 9 ]
Coverage: 87.50% (branches 100.00%)
app.R: 14.29%
R/modules.R: 100.00%
R/utils.R: 100.00%

covr::report() also uses the branches. It marks a line as partly covered when some of its branches weren’t taken. The Branch coverage section of the book shows the same output for a package.

WarningBranches from the app process

The line counts for num_super_script() include the calls made by the running app (6 in total). The branch counts above only include the 4 calls from the test process. In my runs, branches taken inside the shinytest2 app weren’t recorded. Branch coverage is marked experimental, so check the counts before trusting the percentage for code that only the app runs.

Sharing coverage with coverage_to_markdown()

The printed coverage is fine in the console, but it’s awkward to paste into a pull request. coverage_to_markdown() returns the same numbers as a markdown table:

covr::coverage_to_markdown(cov)
| File | Coverage |
| :--- | ---: |
| Total | 81.25% |
| app.R | 14.29% |
| R/modules.R | 100.00% |
| R/utils.R | 80.00% |

The base argument compares two coverage results. I passed the coverage from the run without test-app.R as base:

covr::coverage_to_markdown(cov, base = cov_no_system)
| File | Coverage | Base | Change |
| :--- | ---: | ---: | ---: |
| Total | 81.25% | 56.10% | +25.15% |
| app.R | 14.29% |  |  |
| R/modules.R | 100.00% | 42.31% | +57.69% |
| R/utils.R | 80.00% | 80.00% | +0.00% |

Rendered, the table looks like this:

File Coverage Base Change
Total 81.25% 56.10% +25.15%
app.R 14.29%
R/modules.R 100.00% 42.31% +57.69%
R/utils.R 80.00% 80.00% +0.00%

In practice, base would be the coverage of the main branch, and x the coverage of a pull request. The Change column shows reviewers what the new tests (or new untested code) did to coverage.

In GitHub Actions

GitHub Actions shows any markdown written to the file in GITHUB_STEP_SUMMARY on the run’s summary page. The step below measures the app’s coverage and appends the table there:

- name: Test coverage
  env:
    NOT_CRAN: "true"
  shell: Rscript {0}
  run: |
    cov <- covr::shiny_app_coverage(".")
    md <- covr::coverage_to_markdown(cov)
    cat(md, file = Sys.getenv("GITHUB_STEP_SUMMARY"), sep = "\n", append = TRUE)

For an app in a package, replace shiny_app_coverage(".") with package_coverage(). The GitHub Actions chapter of the book covers the rest of the workflow.

Recap

covr 4.0.0 closes the gap I left in the previous post. shiny_app_coverage() measures an app outside a package, as long as its functions live in R/ and the system tests stop the app with app$stop(). Branch coverage shows which side of each condition the tests never took. That turns a vague percentage into a short list of tests to write. coverage_to_markdown() puts those numbers where reviewers will see them.

If the app is already a package, package_coverage() now counts the system tests too, and the book covers that path.

Resources