repetion in unit tests

I’m looking for best practices testing functions when there is a lot of repetition in what needs to be tested. I’m working on a package where I have my own versions of dplyr’s mutate and transmute for ungrouped and 7 types of grouped datasets (in lists), and also by reference (a la data.table) and not by reference. And I want to check the same 15 operations that these functions can do where the output is going to be somewhat different but with some overlap.

I’ve been using a function that tests all sort of stuff and then depending on the characteristics of what needs to be tested (grouped or not, by reference or not, etc), it will do the comparisons somewhat differently. The issue is when there are errors, it’s really hard to check what failed.

This is an example of how I test different groupings:

grouped_data <- list()
grouped_data[[1]] <- group_by(data, .sample)
grouped_data[[2]] <- group_by(data, .id)
grouped_data[[3]] <- group_by(data, .recording)
grouped_data[[4]] <- group_by(data, .sample, .recording)
grouped_data[[5]] <- group_by(data, .id, .recording)
grouped_data[[6]] <- group_by(data, .id, .sample, .recording)
grouped_data[[7]] <- group_by(data, .sample, condition)

test_that("mutate functions work correctly on grouped signal_tbl", {
  for (d in grouped_data) {
    test_mutates_sgl(d, transmute = FALSE, by_reference = FALSE)

There test_mutates_sgl checks all sort of operations using testthat.

Anyway, I hope it’s clear… I’m looking for resources in using functions and loops in testing.


1 Like

Good question @bnicenboim

Can you give a small reproducible example so we can better understand the issue?