testthat

repository·main·Indexed 21 days ago

https://github.com/r-lib/testthat

The most popular unit testing framework for R. It provides tools to describe expected function behavior, catch errors and warnings, and integrate into R package development workflows with support for R CMD check and visual feedback in RStudio.

Tokens
1.8K
Snippets
14
Records
20
Agent score
76%

What's inside testthat

  1. Overview of testthat

    main

    testthat is the leading unit testing framework for R, designed to make testing intuitive and visually informative.

    Key features include:

    • Descriptive Expectations: Functions to describe expected behavior, including catching errors, warnings, and messages.
    • Workflow Integration: Works with informal command-line testing, full test suites, and R CMD check.
    • Visual Feedback: Provides clear pass/fail/error indicators, with color support in modern terminals and RStudio.
  2. Quickstart: Setting up tests in an R package

    main

    The most efficient way to start testing an R package is using the usethis package.

    1. Create a test file: While inside your package directory, run usethis::use_test("name"). This creates a new test file and configures the necessary infrastructure.
    2. Run your tests:
      • In RStudio: Press Cmd/Ctrl + Shift + T.
      • Via command line/code: Run devtools::test() to execute all tests in the package.
    # Create a new test file named 'name_test.R'
    usethis::use_test("name")
    
    # Run all tests in the package
    devtools::test()
  3. Install the development version of testthat from GitHub

    main

    To install the latest development version from GitHub, it is recommended to use the pak package.

    # Install the development version from GitHub:
    install.packages("pak")
    pak::pak("r-lib/testthat")
  4. Interpret revdepcheck results

    main

    When reviewing revdepcheck reports, results are categorized into two main sections: New problems and Failed to check.

    New problems

    This section lists the first line of error messages for reverse dependencies that are failing in the development version of the package but were passing in the CRAN version. This indicates a regression introduced by your changes.

    Failed to check

    This section lists packages that could not be checked at all. These are typically marked with (NA), indicating that the check process for that specific dependency was interrupted or could not be completed, rather than a specific error being identified within the package code.

  5. Get detailed cloud information for a package using revdepcheck

    main

    When investigating dependency or reverse dependency issues for a specific package, you can retrieve detailed information from the cloud using the revdepcheck::cloud_details() function. This is useful for debugging errors reported in the revdep/problems.md file.

    revdepcheck::cloud_details(, "PACKAGE_NAME")
  6. Troubleshoot ctsem (3.10.4) compilation failure

    main

    The package ctsem (version 3.10.4) fails during the compilation of C++ code.

    Error: g++: fatal error: Killed signal terminated program cc1plus

    Resolution: This error typically indicates that the compiler was killed because the system ran out of memory (OOM) during the heavy compilation process of Stan/C++ headers. Increasing the available system memory or reducing parallel compilation jobs may resolve this.

  7. Troubleshoot caugi (0.3.1) unsupported Rust version

    main

    The package caugi (version 0.3.1) fails installation because the system's Rust version is too old.

    Error:

    ------------------ [UNSUPPORTED RUST VERSION]------------------
    - Minimum supported Rust version is 1.80.0.
    - Installed Rust version is 1.75.0.
    ---------------------------------------------------------------

    Resolution: Upgrade the installed Rust version to at least 1.80.0.

  8. Troubleshoot bayesdfa (1.3.4) installation failures

    main

    The package bayesdfa (version 1.3.4) fails installation in both Devel and CRAN environments due to a missing dependency.

    Error: Error in loadNamespace(x) : there is no package called ‘rstantools’

    Resolution: Ensure that the rstantools package is installed before attempting to install bayesdfa.

  9. Troubleshoot fio (0.1.6) unsupported Rust version

    main

    The package fio (version 0.1.6) fails installation due to an outdated Rust version.

    Error:

    ------------------ [UNSUPPORTED RUST VERSION]------------------
    - Minimum supported Rust version is 1.77.
    - Installed Rust version is 1.75.0.
    ---------------------------------------------------------------

    Resolution: Upgrade the installed Rust version to at least 1.77.

  10. Get dependency failure details for rstanarm

    main

    If you are investigating installation or compilation failures for the rstanarm package, you can retrieve detailed information from the cloud using the revdepcheck::cloud_details() function.

    revdepcheck::cloud_details(, "rstanarm")
  11. Get dependency failure details for loon.tourr

    main

    If you are investigating dependency errors for the loon.tourr package, you can retrieve detailed information from the cloud using the revdepcheck::cloud_details() function.

    Common errors for this package include missing dependencies such as loon and loon.ggplot during the dependency check phase.

    revdepcheck::cloud_details(, "loon.tourr")