R code coverage for the testthat package

coverage r
covr

Is there any tools to evaluate code coverage for R scripts using the testthat package? I found nothing by Google except a mention of the topic in the Future work section of an RJournal article.

There is the newly-arrived covr package which seems to do everything you want, and more! It provides integration with various CI services and shiny. It works with any kind of testing infrastructure (testthat, RUnit, anything else) and also works with compiled code.


What follows is just a very simple demo case I compiled quickly to get you started.

install.packages("covr")

Add a file testcovr/R/doublefun.r containing

doublefun <- function(x, superfluous_option) {
    if (superfluous_option) {
        2*x
    } else {
        3*x
    }
}

and a file testcovr/tests/testthat/test.doublefun.r containing

context("doublefun")

test_that("doublefun doubles correctly", {

    expect_equal(doublefun(1, TRUE), 2)
})

and then run e.g.

test("testcovr")
## Testing testcovr
## doublefun : .

library(covr)
package_coverage("testcovr")
## doublefun : .
##
## Package Coverage: 66.67%
## R/doublefun.r: 66.67%
zero_coverage(package_coverage("testcovr"))
## doublefun : .
##
##        filename first_line last_line first_column last_column value
## 3 R/doublefun.r          5         5            9          11     0

How does covr work anyway?, Any piece of R code can be thought of as a software application with a Unit testing is commonly facilitated by frameworks such as testthat and RUnit. package_coverage() performs coverage calculation on an R package. A better way. In the words of Hadley Wickham, author of the testthat package, in The R Journal: “It’s not that we don’t test our code, it’s that we don’t store our tests so they can be re-run automatically.” So now we are going to see how to accomplish this using automated unit testing, leveraging the testthat package.

The I'll answer my own question :-)

I asked the same question at the project site. It seems at the moment there is no such support for the testthat library, but the possibility recently opened by using the development version of R and exploiting some new features of the profiler. Unfortunately, it seems a huge work to do that, but hopefully someone will take the lead on that.

Find the details in this ticket.

r-lib/covr: Test coverage reports for R, Test coverage reports for R - https://covr.r-lib.org View code Jump to file Track test coverage for your R package and view reports locally or (optionally)  Code coverage is a measure of the amount of code being exercised by a set of tests. It is an indirect measure of test quality and completeness. This package is compatible with any testing methodology or framework and tracks coverage of both R code and compiled C/C++/FORTRAN code.

Here's an attempt at calculating test coverage for a set of R files at the function level:

https://gist.github.com/cannin/819e73426b4ebd5752d5

It depends on using regular expressions to find where functions are created and when they are called.

Code coverage? · Issue #45 · r-lib/testthat · GitHub, Is there any tools to evaluate code coverage for R scripts using the testthat package? I found nothing by Google except a mention of the topic in  Provided that you’ve already written an R package, the next step is to create tests. Luckily, devtools package makes setting up both testing and code coverage a breeze. After running this code you will get a code that can be added to your README file to display a codecov badge.

You can use the following solution to evaluate code coverage for R scripts using the testthat package:

library(covr)
coverage_to_list()

testCoverage package, Clear box unit test coverage utility for R code. install.packages("devtools") # Install a unit test framework install.packages("testthat") # Install testCoverage  This package uses the alternate parser of R-3.0 to instrument R code, and record whether the code is run by tests. The package replaces symbols in the code to be tested with a unique identifier. This is then injected into a tracing function that will report each time the symbol is called. The first symbol at each level of the expression tree is traced, allowing the coverage of code branches to be checked. Clear Box.

Unit Testing for R • testthat, Testing your code can be painful and tedious, but it greatly increases the quality of unit testing package for R and is used by thousands of CRAN packages. Testing is a vital aspect of software development which is often overlooked by R package authors. testCoverage provides a tool for assessing the level of test coverage within an R package. As the commercial adoption of R continues to increase, both the importance and the level of testing are also set to increase.

Automated testing with 'testthat' in practice, We'll cover (pun intended) how much of your code went untested, with Unit Testing in R: testthat Library Setup # Install package from CRAN  Working Group: Code Coverage Tool for R Code coverage helps to ensure greater software quality by reporting how thoroughly test suites cover the various code paths. Having a tool that supports the breadth of the R language across multiple platforms, and that is used by R package developers and R core teams, helps to improve software quality for the R Community.

testthat 2.1.0, easily. testthat is the most popular unit-testing package for R, and is used by over You can learn more about unit testing at https://r-pkgs.org/tests.html. a very flexible mapping between your R code and the corresponding tests. active file, run just the tests for that file, and report the coverage results. Don't forget to add covr to the Suggests: field of your package's DESCRIPTION file; possibly also to Remotes: for r-lib/covr. For further details regarding Appveyor CI integration, also have a look at r-appveyor. To use a different CI service or call codecov() locally you can set the environment variable CODECOV_TOKEN

Comments
  • The testthat package itself uses covr to measure code coverage. This is visible in testthat's travis.yml file.
  • It seems this answer is kind of obsolete now; see my answer.