{"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,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4,27],"tags":[385,440,450],"aioseo_notices":[],"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}]}}