{"id":652,"date":"2012-10-23T12:50:51","date_gmt":"2012-10-23T12:50:51","guid":{"rendered":"http:\/\/garysieling.com\/blog\/?p=652"},"modified":"2012-10-23T12:50:51","modified_gmt":"2012-10-23T12:50:51","slug":"unit-testing-with-r","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/unit-testing-with-r\/","title":{"rendered":"Unit testing in R"},"content":{"rendered":"<h3>Commentary<\/h3>\n<p>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. <\/p>\n<p>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).<\/p>\n<p>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 &#8220;test&#8221;, and there are startup\/teardown methods.<\/p>\n<p>Unlike jUnit and nUnit, there is no IDE integration, nor is there a specialized tool &#8211; 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.<\/p>\n<p>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.<\/p>\n<h3>Implementation<\/h3>\n<p>The following code will run a test suite, <\/p>\n<pre lang=\"r\">\nsource('chords.r')\n\ntest.suite <- defineTestSuite(\"example\",\n                              dirs = file.path(\"tests\"),\n                              testFileRegexp = '^.+\\\\.r$')\n\ntest.result <- runTestSuite(test.suite)\n\nprintTextProtocol(test.result)\n\ntest.result\n<\/pre>\n<p>This prints a result like this:<\/p>\n<pre>\nNumber of test functions: 168\nNumber of errors: 2\nNumber of failures: 166 \n<\/pre>\n<p>By default R does not print stack traces when there is an error, so the following may be helpful:<\/p>\n<pre lang=\"r\">\noptions(error=function() traceback(10))\n\n<\/pre>\n<p>RUnit provides a series of \"check\" functions, for test result assertions:<\/p>\n<pre lang=\"R\">\ncheckTrue(x > 0)\n<\/pre>\n<p>The results are available as text or HTML, or you can inspect the R object, for custom output.<\/p>\n<pre lang=\"r\">\nprintTextProtocol(test.result)\n\nstr(test.result)\nList of 1\n$ example:List of 8\n  ..$ nTestFunc        : num 168\n  ..$ nDeactivated     : int 0\n  ..$ nErr             : num 2\n  ..$ nFail            : num 166\n  ..$ dirs             : chr \"tests\"\n  ..$ testFileRegexp   : chr \"^.+\\\\.r$\"\n  ..$ testFuncRegexp   : chr \"^test.+\"\n  ..$ sourceFileResults:List of 1\n  .. ..$ tests\/wiki-chords.r:List of 168\n  .. .. ..$ testA       :List of 4\n  .. .. .. ..$ kind     : chr \"failure\"\n  .. .. .. ..$ msg      : chr \"Error in checkTrue(cmp) : Test not TRUE\\n\\n\"\n  .. .. .. ..$ checkNum : num 1\n  .. .. .. ..$ traceBack: NULL\n  .. .. ..$ testA7      :List of 4\n  .. .. .. ..$ kind     : chr \"failure\"\n  .. .. .. ..$ msg      : chr \"Error in checkTrue(cmp) : Test not TRUE\\n\\n\"\n  .. .. .. ..$ checkNum : num 1\n  .. .. .. ..$ traceBack: NULL\n<\/pre>\n<p>The full test suite for this example is <a href=\"https:\/\/github.com\/garysieling\/chords\/blob\/master\/chords.r \">on Github<\/a>, as it's a bit long for a blog post.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Commentary 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 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/unit-testing-with-r\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Unit testing in R&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"footnotes":""},"categories":[4,27],"tags":[385,440,450],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 4.9.9 - aioseo.com -->\n\t<meta name=\"description\" content=\"Commentary 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\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"gary\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/www.garysieling.com\/blog\/unit-testing-with-r\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 4.9.9\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"Gary Sieling - Software Engineer\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"Unit testing in R - Gary Sieling\" \/>\n\t\t<meta property=\"og:description\" content=\"Commentary 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\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/www.garysieling.com\/blog\/unit-testing-with-r\/\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2012-10-23T12:50:51+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2012-10-23T12:50:51+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Unit testing in R - Gary Sieling\" \/>\n\t\t<meta name=\"twitter:description\" content=\"Commentary 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\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"BlogPosting\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/unit-testing-with-r\\\/#blogposting\",\"name\":\"Unit testing in R - Gary Sieling\",\"headline\":\"Unit testing in R\",\"author\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/author\\\/gary\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/#organization\"},\"datePublished\":\"2012-10-23T12:50:51+00:00\",\"dateModified\":\"2012-10-23T12:50:51+00:00\",\"inLanguage\":\"en-US\",\"commentCount\":1,\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/unit-testing-with-r\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/unit-testing-with-r\\\/#webpage\"},\"articleSection\":\"Code Examples, Statistics, nlp, programming techniques, R\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/unit-testing-with-r\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.garysieling.com\\\/blog\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/category\\\/code-examples\\\/#listItem\",\"name\":\"Code Examples\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/category\\\/code-examples\\\/#listItem\",\"position\":2,\"name\":\"Code Examples\",\"item\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/category\\\/code-examples\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/unit-testing-with-r\\\/#listItem\",\"name\":\"Unit testing in R\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/unit-testing-with-r\\\/#listItem\",\"position\":3,\"name\":\"Unit testing in R\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/category\\\/code-examples\\\/#listItem\",\"name\":\"Code Examples\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/#organization\",\"name\":\"Gary Sieling\",\"description\":\"Software Engineer\",\"url\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/author\\\/gary\\\/#author\",\"url\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/author\\\/gary\\\/\",\"name\":\"gary\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/unit-testing-with-r\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/0be925276d848ffe98a6a9dc8cf33e67?s=96&d=identicon&r=g\",\"width\":96,\"height\":96,\"caption\":\"gary\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/unit-testing-with-r\\\/#webpage\",\"url\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/unit-testing-with-r\\\/\",\"name\":\"Unit testing in R - Gary Sieling\",\"description\":\"Commentary 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\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/unit-testing-with-r\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/author\\\/gary\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/author\\\/gary\\\/#author\"},\"datePublished\":\"2012-10-23T12:50:51+00:00\",\"dateModified\":\"2012-10-23T12:50:51+00:00\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/\",\"name\":\"Gary Sieling\",\"description\":\"Software Engineer\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Unit testing in R - Gary Sieling","description":"Commentary 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","canonical_url":"https:\/\/www.garysieling.com\/blog\/unit-testing-with-r\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"BlogPosting","@id":"https:\/\/www.garysieling.com\/blog\/unit-testing-with-r\/#blogposting","name":"Unit testing in R - Gary Sieling","headline":"Unit testing in R","author":{"@id":"https:\/\/www.garysieling.com\/blog\/author\/gary\/#author"},"publisher":{"@id":"https:\/\/www.garysieling.com\/blog\/#organization"},"datePublished":"2012-10-23T12:50:51+00:00","dateModified":"2012-10-23T12:50:51+00:00","inLanguage":"en-US","commentCount":1,"mainEntityOfPage":{"@id":"https:\/\/www.garysieling.com\/blog\/unit-testing-with-r\/#webpage"},"isPartOf":{"@id":"https:\/\/www.garysieling.com\/blog\/unit-testing-with-r\/#webpage"},"articleSection":"Code Examples, Statistics, nlp, programming techniques, R"},{"@type":"BreadcrumbList","@id":"https:\/\/www.garysieling.com\/blog\/unit-testing-with-r\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/www.garysieling.com\/blog#listItem","position":1,"name":"Home","item":"https:\/\/www.garysieling.com\/blog","nextItem":{"@type":"ListItem","@id":"https:\/\/www.garysieling.com\/blog\/category\/code-examples\/#listItem","name":"Code Examples"}},{"@type":"ListItem","@id":"https:\/\/www.garysieling.com\/blog\/category\/code-examples\/#listItem","position":2,"name":"Code Examples","item":"https:\/\/www.garysieling.com\/blog\/category\/code-examples\/","nextItem":{"@type":"ListItem","@id":"https:\/\/www.garysieling.com\/blog\/unit-testing-with-r\/#listItem","name":"Unit testing in R"},"previousItem":{"@type":"ListItem","@id":"https:\/\/www.garysieling.com\/blog#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/www.garysieling.com\/blog\/unit-testing-with-r\/#listItem","position":3,"name":"Unit testing in R","previousItem":{"@type":"ListItem","@id":"https:\/\/www.garysieling.com\/blog\/category\/code-examples\/#listItem","name":"Code Examples"}}]},{"@type":"Organization","@id":"https:\/\/www.garysieling.com\/blog\/#organization","name":"Gary Sieling","description":"Software Engineer","url":"https:\/\/www.garysieling.com\/blog\/"},{"@type":"Person","@id":"https:\/\/www.garysieling.com\/blog\/author\/gary\/#author","url":"https:\/\/www.garysieling.com\/blog\/author\/gary\/","name":"gary","image":{"@type":"ImageObject","@id":"https:\/\/www.garysieling.com\/blog\/unit-testing-with-r\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/0be925276d848ffe98a6a9dc8cf33e67?s=96&d=identicon&r=g","width":96,"height":96,"caption":"gary"}},{"@type":"WebPage","@id":"https:\/\/www.garysieling.com\/blog\/unit-testing-with-r\/#webpage","url":"https:\/\/www.garysieling.com\/blog\/unit-testing-with-r\/","name":"Unit testing in R - Gary Sieling","description":"Commentary 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","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/www.garysieling.com\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/www.garysieling.com\/blog\/unit-testing-with-r\/#breadcrumblist"},"author":{"@id":"https:\/\/www.garysieling.com\/blog\/author\/gary\/#author"},"creator":{"@id":"https:\/\/www.garysieling.com\/blog\/author\/gary\/#author"},"datePublished":"2012-10-23T12:50:51+00:00","dateModified":"2012-10-23T12:50:51+00:00"},{"@type":"WebSite","@id":"https:\/\/www.garysieling.com\/blog\/#website","url":"https:\/\/www.garysieling.com\/blog\/","name":"Gary Sieling","description":"Software Engineer","inLanguage":"en-US","publisher":{"@id":"https:\/\/www.garysieling.com\/blog\/#organization"}}]},"og:locale":"en_US","og:site_name":"Gary Sieling - Software Engineer","og:type":"article","og:title":"Unit testing in R - Gary Sieling","og:description":"Commentary 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","og:url":"https:\/\/www.garysieling.com\/blog\/unit-testing-with-r\/","article:published_time":"2012-10-23T12:50:51+00:00","article:modified_time":"2012-10-23T12:50:51+00:00","twitter:card":"summary_large_image","twitter:title":"Unit testing in R - Gary Sieling","twitter:description":"Commentary 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"},"aioseo_meta_data":{"post_id":"652","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"","isEnabled":true},"graphs":[]},"schema_type":"default","schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"limit_modified_date":false,"created":"2023-02-04 16:10:02","updated":"2026-07-06 00:48:50","ai":null,"breadcrumb_settings":null,"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/www.garysieling.com\/blog\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/www.garysieling.com\/blog\/category\/code-examples\/\" title=\"Code Examples\">Code Examples<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tUnit testing in R\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/www.garysieling.com\/blog"},{"label":"Code Examples","link":"https:\/\/www.garysieling.com\/blog\/category\/code-examples\/"},{"label":"Unit testing in R","link":"https:\/\/www.garysieling.com\/blog\/unit-testing-with-r\/"}],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/652"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=652"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/652\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=652"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=652"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=652"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}