<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/unit-testing-in-r-using-testthat-library-exercises/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://r-exercises.com/2017/03/12/unit-testing-in-r-using-testthat-library/?utm_source=rss&utm_medium=rss&utm_campaign=unit-testing-in-r-using-testthat-library"> R-exercises</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a target="_blank" rel="nofollow" href="http://r-exercises.com/wp-content/uploads/2017/03/board-361516_960_720.jpg?x61433"><img sizes="(max-width: 150px) 100vw, 150px" alt="" class="wp-image-4721" src="https://i0.wp.com/r-exercises.com/wp-content/uploads/2017/03/board-361516_960_720-150x150.jpg?resize=150%2C150" data-recalc-dims="1"></a></p>
<p><b>testthat</b> is a testing framework developed by Hadley Wickham, which makes unit testing easy for developers. </p>
<p>Test scripts developed can be re-run after debugging or making changes to the functions without the hassle of developing the code for testing again. </p>
<p><b>testthat</b> has a heirarchical structure made up of expectations, tests and contexts.<br>
Visit this <a target="_blank" rel="nofollow" href="https://journal.r-project.org/archive/2011-1/RJournal_2011-1_Wickham.pdf">link</a> to know more. </p>
<p>You should be familiar with creation of functions in R to know how this testing framework works. </p>
<p>Answers to the exercises are available <a target="_blank" rel="nofollow" href="http://r-exercises.com/2017/03/12/unit-testing-in-r-using-testthat-library-solutions">here</a>.</p>
<p><strong>Exercise 1</strong></p>
<p>Install and load the package <b>testthat</b> using the appropriate function. </p>
<p><strong>Exercise 2</strong></p>
<p> <code>expect_that()</code> is the function that makes the binary assertion of whether or not the value is as expected.<br>
<code>expect_that(x,equals(y))</code> reads as “it is expected that ‘a’ will be equal to ‘b’”.</p>
<p>Use this function to see if <code>5*2</code> equals <code>10</code> </p>
<div>
<aside><img src="http://r-exercises.com/2017/03/12/unit-testing-in-r-using-testthat-library/?utm_source=rss&utm_medium=rss&utm_campaign=unit-testing-in-r-using-testthat-library"></aside>
<div><strong>Learn more</strong> about Hadley Wickhams usefull packages in the online course <a target="_blank" rel="nofollow" href="http://r-exercises.com/product/r-data-pre-processing-data-management-shape-your-data/">R Data Pre-Processing &amp; Data Management – Shape your Data!</a>. In this course you will learn how to work with:
<ul>
<li>tidyr, cleaning your data</li>
<li>dplyr, shape your data</li>
<li>And much more</li>
</ul>
</div>
</div>
<p><strong>Exercise 3</strong></p>
<p>The function <code>equals()</code> checks for equality with a numerical tolerance. Let’s see what that tolerance level is</p>
<p>Use appropriate function to see if <code>5*2</code> equals <code>10 + (1e-7)</code>.<br>
Does the test fail?<br>
If no, change the value to 1e-6 and see what happens. </p>
<p><strong>Exercise 4</strong></p>
<p>To exactly match the values <code>is_identical_to()</code> can be used instead of <code>equals()</code></p>
<p>Using the appropriate function, check if <code>2*2</code> is identical to <code>4 + (1e-8)</code></p>
<p>Please check the documentation of this package to learn more about the available functions. </p>
<p><strong>Exercise 5</strong></p>
<p>Let us create a function (m) to multiply two numbers ( two arguments ) and check if it throws an error with character input arguments. </p>
<p>Check if m(“2″,”3”) throws an error “non-numeric argument to binary operator”</p>
<p><strong>Exercise 6</strong></p>
<p>Now that we know how to check for expectations, let us create tests.<br>
Test is a collection of expectations, where these expectations test a single item of the functionality of a process. </p>
<p><code>test_that()</code> is the function that encapsulates the description and the code to test each expectation. </p>
<p>The first argument is the description and the second argument is a collection of expectations. </p>
<p>Create a test for function ‘m’ with description “Testing multiplication function” and add a few scenarios to it.<br>
1. Check if m(2,3) equals 6<br>
2. Check if m(2,c(2,3)) equals c(4,6)<br>
3. Check if m(2,”3″) throws an error “non-numeric argument to binary operator”</p>
<p><strong>Exercise 7</strong></p>
<p>The User can write his own expectation using the <code>expect()</code> function. This expectation should compare the input value and the expectation and report the result.<br>
The syntax to write one is as below. </p>
<p>custom_expectation &lt;- function() {function(x) {expectation(condition, &quot;Failure message&quot;)}} </p>
<p>Now, write an expectation <code>is_greater_10()</code> to check if a number is greater than <code>10</code></p>
<p><strong>Exercise 8</strong></p>
<p>Use the expectation defined above to check if 9 is greater than 10. </p>
<p><strong>Exercise 9</strong></p>
<p>tests can be put together in a file and run at once. Write tests of your choice and save them in a file.<br>
Use the function <code>test_file()</code> to run all the tests in the file. </p>
<p><strong>Exercise 10</strong></p>
<p>Test files in a directory can be run at once using the function <code>test_dir()</code>.<br>
Create multiple test files and save them in a directory. Run all the tests at once using the function.</p>
<div>
<h3>Related exercise sets:</h3>
<ol>
<li><a title="Mode exercises" target="_blank" rel="nofollow" href="http://r-exercises.com/2016/02/14/mode-exercises/">Mode exercises </a></li>
<li><a title="Advanced Base Graphics Exercises" target="_blank" rel="nofollow" href="http://r-exercises.com/2016/09/23/advanced-base-graphics-exercises/">Advanced Base Graphics Exercises </a></li>
<li><a title="Complex Tables – Exercises" target="_blank" rel="nofollow" href="http://r-exercises.com/2016/04/26/complex-tables-exercises/">Complex Tables – Exercises </a></li>
<li><a target="_blank" rel="nofollow" href="http://r-exercises.com/start-here-to-learn-r/">Explore all our (>1000) R exercises</a></li>
<li><a target="_blank" rel="nofollow" href="http://r-exercises.com/r-courses/">Find an R course using our R Course Finder</a> directory</li>
</ol>
</div>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://r-exercises.com/2017/03/12/unit-testing-in-r-using-testthat-library/?utm_source=rss&utm_medium=rss&utm_campaign=unit-testing-in-r-using-testthat-library"> R-exercises</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/mIoMWlIHldE"></a></div>