Fuzz-Test R Functions

Test function arguments with a wide array of inputs, and produce reports summarizing messages, warnings, errors, and returned values.

fuzzr implements some simple "fuzz tests" for your R functions, passing in a wide array of inputs and returning a report on how your function reacts.


Tests are set by passing functions that return named lists of input values. These values will be passed as function arguments. Several default suites are provided with this package, such as test_char, however you may implement your own by passing a function that returns a similarly-formatted list.

#> character(0)
#> $char_single
#> [1] "a"
#> $char_single_blank
#> [1] ""
#> $char_multiple
#> [1] "a" "b" "c"
#> $char_multiple_blank
#> [1] "a" "b" "c" "" 
#> $char_with_na
#> [1] "a" "b" NA

Evaluate a function argument by supplying it's quoted name along with the tests to run to fuzz_function, along with any other required static values. fuzz_function returns a "fuzz_results" object that stores conditions raised by a function (message, warning, or error) along with any value returned by that function.

fuzz_results <- fuzz_function(fun = lm, arg_name = "subset", data = iris, 
                              formula = Sepal.Length ~ Petal.Width + Petal.Length, 
                              tests = test_all())

You can render these results as a data frame:

fuzz_df <- as.data.frame(fuzz_results)
char_emptyirisSepal.Length ~ Petal.Width + Petal.LengthNANANA0 (non-NA) casesNA1
char_singleirisSepal.Length ~ Petal.Width + Petal.LengthNANANA0 (non-NA) casesNA2
char_single_blankirisSepal.Length ~ Petal.Width + Petal.LengthNANANA0 (non-NA) casesNA3
char_multipleirisSepal.Length ~ Petal.Width + Petal.LengthNANANA0 (non-NA) casesNA4
char_multiple_blankirisSepal.Length ~ Petal.Width + Petal.LengthNANANA0 (non-NA) casesNA5
char_with_nairisSepal.Length ~ Petal.Width + Petal.LengthNANANA0 (non-NA) casesNA6

You can also access the value returned by any one test by calling the index that test with value_returned:

model <- fuzz_value(fuzz_results, which.max(fuzz_df$subset == "int_multiple"))
#>  (Intercept)  Petal.Width Petal.Length 
#>          0.8           NA          3.0

Specify multiple-argument tests with p_fuzz_function, passing a named list of arguments and tests to run on each. p_fuzz_function will test every combination of argument and variable.

fuzz_p <- p_fuzz_function(agrep, list(pattern = test_char(), x = test_char()))
char_emptychar_emptyNANANAinvalid 'pattern' argumentNA1
char_single_blankchar_emptyNANANA'pattern' must be a non-empty character stringNA3
char_multiplechar_emptyNANAargument 'pattern' has length > 1 and only the first element will be usedNAinteger4
char_multiple_blankchar_emptyNANAargument 'pattern' has length > 1 and only the first element will be usedNAinteger5
char_with_nachar_emptyNANAargument 'pattern' has length > 1 and only the first element will be usedNAinteger6

Matthew Lincoln


Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.


0.2.1 by Matthew Lincoln, 7 months ago


Report a bug at https://github.com/mdlincoln/fuzzr/issues

Browse source code at https://github.com/cran/fuzzr

Authors: Matthew Lincoln [aut, cre]

Documentation:   PDF Manual  

MIT + file LICENSE license

Imports assertthat, progress, purrr

Suggests testthat, knitr, rmarkdown

See at CRAN