MyNixOS website logo
Description

Utils and 'RStudio' Addins to Make Testing Even More Fun.

Utility functions and 'RStudio' addins for writing, running and organizing automated tests. Integrates tightly with the packages 'testthat', 'devtools' and 'usethis'. Hotkeys can be assigned to the 'RStudio' addins for running tests in a single file or to switch between a source file and the associated test file. In addition, testthis provides function to manage and run tests in subdirectories of the test/testthat directory.

testthis

CRANstatus Travis buildstatus Codecov testcoverage

testthis makes unit testing in R more comfortable. It is designed to complement the packages testthat, devtools and usethis.

Overview

testthis provides RStudio addins for working with files that contain tests. These can be called like normal R function, but it is also possible to assigned them to hotkeys RStudio (Tools/Modify Keyboard Shortcuts).

  • test_this(): Reloads the package and runs tests associated with the currently open R script file.
  • open_testfile(): Opens the associated test file in an editor window. If the currently open file already is a test file, it opens the associated file in the /R directory. Can be used to jump back and forth between both.

The associated test file for a file R/foo.R is usually /tests/testhat/test_foo.R (or test-foo.R). You can modify this behavior by putting the comment #* @testfile anotherfile anywhere in R/foo.R.

testthis also provides functions for managing tests in subdirectories of tests/testthat:

  • use_testdata() places a single R object in the tests/testhat/testdata directory. It is analogous to usethis::use_data(), except that it saves the object in the .rds format, which is more appropriate for single R objects than .rda or .Rdata (see ?readRDS).

  • use_testdata_raw() creates a script-file in the directory tests/testhat/testdata-raw. The testdata-raw directory should contain all script files used to generate the data in tests/testhat/testdata, analogous to the relationship between /data and /data-raw as recommended by devtools/usethis.

  • find_testdata() and read_testdata() are simple helpers to read files saved with use_testdata().

  • use_test_subdir() and test_subdir() for putting/running tests in subdirectories of tests/testhat/. These tests will not be run on CRAN or by devtools::test(). This is useful for tests that take a long time to execute, or that require external resources (web, databases) that may not always be available.

    test_acceptance(), test_manual() and test_integration() are presets to run tests in the integration_tests, acceptance_tests and manual_tests subdirectories of test/testthat.

Testthis also provides some simple code analysis tools

  • test_coverage() lists all functions of package and shows whether they are mentioned in any test_that() call’s desc argument; e.g. if you have a test file containing test_that("testing that function foo works", <...>), foo() will show up marked as tested. This can be used as a simple todo list for testing, but does not replace a proper test coverage analyzer like covr.
  • test_index() produces an index of all test_that() calls. Supports RStudio Markers when run interactively from within RStudio.

For more details see the function reference

Installation

# Testthis is on CRAN:
install.packages("testthis")

# You can also install the development version from GitHub:
# install.packages("remotes")
remotes::install_github("s-fleck/testthis")

Usage

library(testthis)

RStudio addins (can be assigned to hotkeys):

open_testfile()
## The file `tests/testthat/test_myscript.R` does not exist. 
## You can create it with testthis::test_skeleton().

test_skeleton()
## * Creating `tests/testthat/test_myscript.R`

open_testfile()
## > Opens `tests/testthat/test_myscript.R`

test_this() 
## > Runs tests in `tests/testthat/test_myscript.R`
## > works from the original .R file as well as from the file containing the tests

Code analysis:

test_index()

test-index

test_coverage()

## > Package /data/home/fleck/rpkgs/rotor, Test Coverage: 17.8%
## >
## > exported functions .................... 
## > u fun          
## > + backup       
## > + backup_date  
## > + backup_time  
## > ...

# 'u' stands for 'unit tests', more columns are possible if you use the 
# test_subdir() feature of testthis
Metadata

Version

1.1.1

License

Unknown

Platforms (77)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-freebsd
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64-windows
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows