Learn R Programming

⚠️There's a newer version (1.4.22) of this package.Take me there.

unitizeR - Interactive R Unit Tests

TL;DR

unitizer simplifies creation, review, and debugging of tests in R. It automatically stores R expressions and the values they produce, so explicit expectations are unnecessary. Every test is easy to write with unitizer because testing and using a function are the same. This encourages non-trivial tests that better represent actual usage.

Tests fail when the value associated with an expression changes. In interactive mode you are dropped directly into the failing test environment so you may debug it.

unitizer is on CRAN:

install.packages('unitizer')

It bakes in a lot of contextual help so you can get started without reading all the documentation. Try the demo to get an idea:

library(unitizer)
demo(unitizer)

Or check out the screencast to see unitizer in action.

Why Another Testing Framework?

Automated Test Formalization

Are you tired of the deparse/dput then copy-paste R objects into test file dance, or do you use testthat::expect_equal_to_reference or other snapshot testing a lot?

With unitizer you interactively review your code, as you would when informally testing it by typing it at the R prompt. Then, with a single keystroke, unitizer stores the code, the value and any conditions (warnings, errors, etc.) it produced, and turns the lot into a formal regression test.

Streamlined Debugging

Do you wish the nature of a test failure was more immediately obvious?

When tests fail, you are shown a proper diff so you can clearly identify how the test failed:

Do you wish that you could start debugging your failed tests without additional set-up work?

unitizer drops you in the test environment so you can debug why the test failed without further ado:

Fast Test Updates

Do you avoid improvements to your functions because that would require painstakingly updating many tests?

The diffs for the failed tests let you immediately confirm only what you intended changed. Then you can update each test with a single keystroke.

Usage

unitizer stores R expressions and the result of evaluating them so that it can detect code regressions. This is akin to saving test output to a .Rout.save file as documented in Writing R Extensions, except that we’re storing the actual R objects and it is much easier to review them.

To use unitizer:

  • Write test expressions as you would when informally testing code on the command line, and save them to a file (e.g. “my_file_name.R”).
  • Run unitize("my_file_name.R") and follow the prompts.
  • Continue developing your package.
  • Re-run unitize("my_file_name.R"); if any tests fail you will be able to review and debug them in an interactive prompt.

unitizer can run in a non-interactive mode for use with R CMD check.

Documentation

Related Packages

Acknowledgments

Thank you to:

About the Author

Brodie Gaslam is a hobbyist programmer based in the US East Coast.

Copy Link

Version

Install

install.packages('unitizer')

Monthly Downloads

1,615

Version

1.4.11

License

GPL (>= 2)

Issues

Pull Requests

Stars

Forks

Maintainer

Brodie Gaslam

Last Published

November 13th, 2020

Functions in unitizer (1.4.11)

as.character,unitizerChanges-method

Print Out A Summary Of the Changes
unitizer_demo

Demo Details and Helper Functions
all.equal.condition

Compare Conditions
cap_first

Captalizes or Decapitalizes First Letter
capture_output

Capture Both StdOut and StdErr
infer_unitizer_location

Infers Possible Unitizer Path From Context
filename_to_storeid

Create a Store ID from a Test File Name
as.character.bullet

Produce Character Vector Representation of Bullet Lists
set_unitizer

Set and Retrieve Store Contents
invalidateLs

Clears ls Info and Marks as Invalid
show,unitizerItemTestsErrorsDiff-method

Show Method for unitizerItemTestsErrorsDiff objects
$,unitizerItemTestsErrorsDiffs-method

Subsetting Methods for unitizerItemTestsErrorsDiffs objects
repair_environments

Repair Environment Chains
mock_item

Generates a Dummy Item For Use in Examples
nextItem,unitizerList-method

show,unitizerChanges-method

Print Out A Summary Of the Changes
unitize

Unitize an R Test Script
run_ls

Worker function to actually execute the `ls` work
text_wrap

Text Wrapping Utilities
editCalls

Edit Calls In Unitizer
desc

One Line Description of Object
show,unitizerItemTestsErrorsDiffs-method

Show Method for unitizerItemTestsErrorsDiffs objects
unitizerGlobalBase-class

Structures For Tracking Global Options
flattenUntz

Reduce S4 objects Into Lists
unitizer

unitizer
healEnvs

Fix Environment Ancestries
show.conditionList

Prints A list of Conditions
$.unitizerItem

Retrieve Test Contents From Test Item
unitizer.opts

Unitizer Options
validate_pre_post

Helper function for validations
valid_names

Make Valid Names
unitizer_result

Return Values and Related Methods for unitize Functions
testthat_translate_file

Convert a testthat Test File to a unitizer
sizeRDS

Measure object size as an RDS
testthat_transcribe_file

Transcribes a testtaht File Into unitizer Format
options_zero

Set Options to Initial Zero State
unitizer_prompt

Interactively Retrieve User Input
unitizerSection-class

Contains Representation For a Section of Tests
getFun

Utility Function
length,unitizerChanges-method

Return Sum of Total Changes
unitizerState

Tests and Session State
print.bullet

Print Methods for UL and OL objects
testFuns

Store Functions for New vs. Reference Test Comparisons
unitizerList

S4 Object To Implement Base List Methods
sizeUntz

Utility To Examine Object Size
length,unitizerSection-method

Compute Length of a unitizerSection-class
unitizerChanges-class

Summary of Changes
unitizer_sect

Define a unitizer Section
unitizerTests-class

Collections of Calls For Testing
show,unitizerDummy-method

Documentation Block for Internal S4 Methods
unitizer_ls

An `ls` Like Function
conditionList

Contains A List of Conditions
as.expression,unitizerList-method

Coerce to expression by returning items coerced to expressions
all_eq

Like all.equal but Returns Empty String If Not all.equal