Testthat: Feature request: native support for parameterized unit tests

Created on 7 Nov 2019  路  7Comments  路  Source: r-lib/testthat

Hi, currently I'm using the patrick package to run parameterized unit tests via the with_parameters_test_that() function. It's incredibly useful in some cases, but the package no longer seems to be actively developed. I'm of the opinion that this functionality is generally useful enough that it should be integrated directly into testthat.

It's been requested as a native feature in testthat for a while -- see related issue #563 .

Thoughts on this @michaelquinn32?

Best,
Mike

All 7 comments

Also somewhat related to #486

Could you please provide a small inline example so I don't have to read a bunch of documentation elsewhere? Because when you say "parameterised unit test", I hear "function" 馃槈

Sure @hadley, here's the patrick implementation code:
https://github.com/google/patrick/blob/master/R/with_parameters.R#L58

And some examples of how I'm currently using this (e.g. in my basejump package):
https://github.com/acidgenomics/basejump/search?q=with_parameters_test_that&unscoped_q=with_parameters_test_that

Can you please put the example in this issue?

Here's a really minimal example. If there's a better way to do this that's already in testthat, I'm all ears:

library(testthat)
library(patrick)

with_parameters_test_that(
    "'mapply()'-style tests are cool", {
        object <- sum(a, b)
        expect_identical(object, expected)
    },
    a = list(1, 2, 3),
    b = list(2, 3, 4),
    expected = list(3, 5, 7)
)

Inside the basejump package, here's a decent example:

context("detectOrganism")

with_parameters_test_that(
    "Homo sapiens", {
        expect_identical(
            object = detectOrganism(object),
            expected = "Homo sapiens"
        )
    },
    object = c(
        "Homo sapiens",
        "hsapiens",
        "GRCh38",
        "hg38",
        "ENSG00000000001",
        "ENST00000000001"
    )
)

Presumably this is how you would do it in testthat

```r
test_that("Homo sapiens", {
for (object in c("Homo sapiens", "hsapiens", "GRCh38", "hg38", "ENSG00000000001", "ENST00000000001")) {
expect_identical(detectOrganism(!!object), expected = "Homo sapiens")
}
})

Hi Michael!

Thanks for pinging me! Patrick is still maintained. There have been no FRS and no reported issues, but I don't have any plans to change anything with the package.

If there's something I can do to improve the package, please let me know.

Best wishes,
Michael

Was this page helpful?
0 / 5 - 0 ratings

Related issues

unDocUMeantIt picture unDocUMeantIt  路  11Comments

lc0 picture lc0  路  8Comments

BroVic picture BroVic  路  3Comments

vincentvanhees picture vincentvanhees  路  3Comments

jennybc picture jennybc  路  16Comments