DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Languages
  4. Unit Testing in R

Unit Testing in R

Gary Sieling user avatar by
Gary Sieling
·
Jan. 30, 13 · Interview
Like (0)
Save
Tweet
Share
5.29K Views

Join the DZone community and get the full member experience.

Join For Free

R is a statistical programming language, with a strong focus on mathematical operations. When writing code that is math-heavy, unit testing becomes very appealing- while equations may look correct on paper, one minor error can ruin the output.

R programming is also different to CRUD or enterprise software in that the R in-memory data structures are often used in a similar fashion to a database. When used this way, it is more self-contained, and integration tests that require specific data set-up can be easier to manage than tests that require pre-configured data in a database (a requirement, for instance, when testing some parts of ETL scripts).

R has a unit testing package called RUnit, based on the JUnit 3.x APIs, which even includes code coverage. Like jUnit, test functions start with the word “test”, and there are startup/teardown methods.

Unlike jUnit and nUnit, there is no IDE integration, nor is there a specialized tool – it is simply run through the R REPL, which gives you some control at the expense of convenience. Unfortunately there are no one-click installs with CI servers like Jenkins- if you wish to run tests automatically and track the results over time, you have to figure out some command line integration.

Unit testing is not designed for fuzzy operations that have natural failures; for a machine learning exercise you ideally may want to track accuracy, recall, precision, etc. This package may be a useful starting point for that, but would require custom development on top to be really valuable for these types of problem.

Implementation

The following code will run a test suite,


source('chords.r')
 
test.suite <- defineTestSuite("example",
                              dirs = file.path("tests"),
                              testFileRegexp = '^.+\\.r


This prints a result like this:


Number of test functions: 168
Number of errors: 2
Number of failures: 166

By default R does not print stack traces when there is an error, so the following may be helpful:


options(error=function() traceback(10))


RUnit provides a series of “check” functions, for test result assertions:


checkTrue(x > 0)

The results are available as text or HTML, or you can inspect the R object, for custom output.

printTextProtocol(test.result)
 
str(test.result)
List of 1
$ example:List of 8
  ..$ nTestFunc        : num 168
  ..$ nDeactivated     : int 0
  ..$ nErr             : num 2
  ..$ nFail            : num 166
  ..$ dirs             : chr "tests"
  ..$ testFileRegexp   : chr "^.+\\.r$"
  ..$ testFuncRegexp   : chr "^test.+"
  ..$ sourceFileResults:List of 1
  .. ..$ tests/wiki-chords.r:List of 168
  .. .. ..$ testA       :List of 4
  .. .. .. ..$ kind     : chr "failure"
  .. .. .. ..$ msg      : chr "Error in checkTrue(cmp) : Test not TRUE\n\n"
  .. .. .. ..$ checkNum : num 1
  .. .. .. ..$ traceBack: NULL
  .. .. ..$ testA7      :List of 4
  .. .. .. ..$ kind     : chr "failure"
  .. .. .. ..$ msg      : chr "Error in checkTrue(cmp) : Test not TRUE\n\n"
  .. .. .. ..$ checkNum : num 1
  .. .. .. ..$ traceBack: NULL

The full test suite for this example is on Github, as it’s a bit long for a blog post.

R (programming language) unit test

Published at DZone with permission of Gary Sieling, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Is DevOps Dead?
  • What Is API-First?
  • Key Elements of Site Reliability Engineering (SRE)
  • Java Code Review Solution

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: