title: Where to place library calls with testthat?
body: <p>I am looking for best practice help with the brilliant <code>testthat</code>. Where is the best place to put your <code>library(xyzpackage)</code> calls to use all the package functionality?</p>
  
  <p>I first have been setting up a <code>runtest.R</code> setting up paths and packages. 
  I then run <code>test_files(test-code.R)</code> which <strong>only</strong> holds context and tests. Example for structure follows:</p>
  
  <pre><code># runtests.R
  library(testthat)
  library(data.table)
  
  source("code/plotdata-fun.R")
  
  mytestreport &lt;- test_file("code/test-plotdata.R")
  
  # does other stuff like append date and log test report (not shown)
  </code></pre>
  
  <p>and in my test files e.g. <code>test-plotdata.R</code> (a stripped down version):</p>
  
  <pre><code>#my tests for plotdata
  context("Plot Chart")
  
  test_that("Inputs valid", {
  
    test.dt  = data.table(px = c(1,2,3), py = c(1,4,9))
    test.notdt &lt;- c(1,2,3)
  
    # illustrating the check for data table as first param in my code
    expect_error(PlotMyStandardChart(test.notdt, 
                                     plot.me = FALSE),
                  "Argument must be data table/frame")
  
    # then do other tests with test.dt etc...
  })
  </code></pre>
  
  <ol>
  <li><p>Is this the way @hadley intended it to be used? It is not clear from the <a href="http://journal.r-project.org/archive/2011-1/RJournal_2011-1_Wickham.pdf" rel="noreferrer">journal article</a>. <strong>Should I also be duplicating library calls in my test files as well?</strong> Do you need a library set up in each context, or just one at start of file?</p></li>
  <li><p>Is it okay to over-call library(package) in <a href="/questions/tagged/r" class="post-tag" title="show questions tagged 'r'" rel="tag">r</a>? </p></li>
  <li><p>To use the <code>test_dir()</code> and other functionality <strong>what is best way to set up your files</strong>. I do use require() in my functions, but I also set up test data examples in the contexts.  (In above example, you will see I would need data.table package for test.dt to use in other tests). </p></li>
  </ol>
  
  <p>Thanks for your help.</p>