Optimal Placement of Test Data for Automated Testing with Testthat

Optimal Placement of Test Data for Automated Testing with Testthat

21 May 2024 Stephan Petzl Leave a comment Tech-Help

When developing R packages, integrating automated tests is crucial to ensure the reliability and stability of your code. Using the testthat package by Hadley Wickham is a common approach. However, a recurring question among developers is: where should test data files be placed?

Based on best practices, it’s advisable to store your test data in the inst/testdata directory. You can then access these files using system.file("testdata", ..., package="my_package"). This method offers several advantages:

  • Neat File Structure: Especially useful if you have numerous data files and tests.
  • Canonical R Practice: Using system.file is a long-standing convention in R, ensuring reliability during package checks.
  • Robustness: Avoids potential issues with relative file paths during R CMD check.

Example Directory Structure

└── pkg_name/
    ├── DESCRIPTION
    ├── NAMESPACE
    ├── R/
    ├── inst/
    │   └── testdata/
    │       ├── file_1.csv
    │       └── file_2.tif
    └── tests/
        ├── testthat.R
        └── testthat/
            └── test-some_function.R

  

Alternative Approach Using testthat::test_path()

Another effective method is to place your test data within a subdirectory of tests/testthat and access them using testthat::test_path(). This approach ensures compatibility with both interactive testing and R CMD check or devtools::check().

Example Implementation

test_that("testname", {
  expect_equal(
    some_function(
      test_path("testdata", "file_2.tif")
    ),
    ...
  )
})

  

Additional Considerations

Some developers prefer storing test data in files prefixed with helper_ and tests in files prefixed with test_. While this method is valid, it might not offer the same level of organization and robustness as the above approaches.

Example Directory Structure

└── pkg_name/
    ├── DESCRIPTION
    ├── NAMESPACE
    ├── R/
    └── tests/
        ├── testthat.R
        └── testthat/
            ├── helper_myfunc1.R
            ├── helper_myfunc2.R
            └── test_pkg_name.R

  

Conclusion

Choosing the right method to store and access test data is essential for maintaining a clean and effective testing environment. Whether you opt for the inst/testdata directory with system.file or the tests/testthat subdirectory with testthat::test_path(), both approaches have their merits and can be tailored to fit your specific needs.

Enhancing Your Testing Workflow

For developers looking to streamline their testing processes, tools like Repeato can be invaluable. Repeato is a no-code test automation tool for iOS and Android, designed to help you create, run, and maintain automated tests for your apps swiftly. With its intuitive test recorder and AI-based computer vision, Repeato simplifies complex testing scenarios. Additionally, Repeato supports web testing inside Android emulators or devices, with full web testing support coming soon. To learn more about how Repeato can enhance your testing workflow, visit our documentation or download page.

Like this article? there’s more where that came from!