Learn R Programming

testdat

Overview

testdat is designed to ease data validation, particularly for complex data processing, inspired by software unit testing. testdat extends the strong and flexible unit testing framework already provided by testthat with a family of functions and reporting tools focused on checking of data frames.

Features include:

  • A fully fledged test framework so you can spend more time specifying tests and less time running them

  • A set of common methods for simply specifying data validation rules

  • Repeatability of data tests (avoid unintentionally breaking your data set!)

  • Data-focused reporting of test results

Installation

You can install the released version of testdat from CRAN with:

install.packages("testdat")

And the development version from GitHub with:

# install.packages("devtools")
devtools::install_github("socialresearchcentre/testdat")

Usage

See the Introduction to testdat vignette for a detailed introduction.

library(testdat, warn.conflicts = FALSE)
#> Loading required package: testthat
library(dplyr, warn.conflicts = FALSE)

x <- tribble(
  ~id, ~pcode, ~state, ~nsw_only,
  1,   2000,   "NSW",  1,
  2,   3123,   "VIC",  NA,
  3,   2123,   "NSW",  3,
  4,   12345,  "VIC",  3
)

with_testdata(x, {
  test_that("id is unique", {
    expect_unique(id)
  })
  
  test_that("variable values are correct", {
    expect_values(pcode, 2000:2999, 3000:3999)
    expect_values(state, c("NSW", "VIC"))
    expect_values(nsw_only, 1:3) # by default expect_values allows NAs
  })
  
  test_that("filters applied correctly", {
    expect_base(nsw_only, state == "NSW")
  })
})
#> Test passed 

Copy Link

Version

Install

install.packages('testdat')

Monthly Downloads

331

Version

0.4.3

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Danny Smith

Last Published

August 19th, 2025

Functions in testdat (0.4.3)

pattern-expectations

Expectations: patterns
label-expectations

Expectations: labels
output_results_excel

Output ListReporter results in Excel format
reexports

Objects exported from other packages
generic-expectations

Expectations: generic helpers
global-data

Get/set test data
proportion-expectations

Expectations: proportions
text-expectations

Expectations: text
testdat-package

testdat: Data Unit Testing for R
reporter-deprec

Deprecated reporter functions
uniqueness-expectations

Expectations: uniqueness
value-expectations

Expectations: values
chk-defunct

Defunct checking functions
chk-dates

Checks: dates
chk-helper

Checks: data frame helpers
chk-uniqueness

Checks: uniqueness
chk-values

Checks: values
chk-text

Checks: text
chk-dummy

Checks: dummy
chk-labels

Checks: labels
chk-patterns

Checks: patterns
chk-deprec

Deprecated checking functions
data-params

Expectation params
expect_depends

Expectations: functional dependency
conditional-expectations

Expectations: consistency
exclusivity-expectations

Expectations: exclusivity
expect_make

Create an expectation from a check function
datacomp-expectations

Expectations: comparisons
expect_custom

Extension of 'expect' to allow inclusion of custom fields
date-expectations

Expectations: dates
expect-defunct

Defunct expectation functions
expect-deprec

Deprecated expectation functions