title: How can I apply a testthat test to all functions in my package that match a certain naming pattern?
body: <p>I have a package that contains a number of functions that are named <code>viz_foo()</code>, <code>viz_bar()</code> and so on.</p>
  <p>I want to test that each of these functions produces an object of class <code>gg</code>. This would be simple with two functions, as in:</p>
  <pre><code>testthat::test_that(&quot;viz_ functions produce a plot&quot;, {
      testthat::expect_s3_class(viz_foo(), &quot;gg&quot;)
      testthat::expect_s3_class(viz_bar(), &quot;gg&quot;)
  })
  </code></pre>
  <p>But in practice I have many functions with this naming convention, and new functions are added all the time by package contributors. I want to make sure that any new functions are tested in this way, without having to explicitly add them to the unit test.</p>
  <p>The janky way I had thought to deal with this was:</p>
  <pre><code>testthat::test_that(&quot;viz_ funcs produce a plot&quot;, {
    viz_funcs &lt;- ls(&quot;package:mypackage&quot;, pattern = &quot;viz_&quot;)
    
    eval_func &lt;- function(func_name) {
      suppressMessages(
        eval(str2lang(paste0(func_name, &quot;()&quot;)))
      )
    }
    
    plots &lt;- lapply(viz_funcs, eval_func)
    
    lapply(plots, testthat::expect_s3_class, &quot;gg&quot;)
    
  })
  </code></pre>
  <p>This works when running the test with <code>devtools::test()</code> but not in <code>R CMD check</code>.</p>
  <p>Is there a way to apply a test to all functions within a package that contain a certain pattern in their name?</p>