@@ -712,6 +712,110 @@ If the utility actually has a test mode, great. Otherwise simply invoking
712712``--help `` or ``--version `` or something will at least test that it is
713713installed and can run.
714714
715+ Testing R packages
716+ ..................
717+
718+ R packages should be tested for successful library loading. All
719+ recipes for CRAN packages should begin from `conda_r_skeleton_helper
720+ <https://github.com/bgruening/conda_r_skeleton_helper> `_ and will
721+ automatically include library loading tests. However, many R packages
722+ also include ``testthat `` tests that can potentially be run. While
723+ optional, additional testing is encouraged when packages:
724+
725+ - provide interaces to other (compiled) libraries (e.g., ``r-curl ``,
726+ ``r-xml2 ``)
727+ - extend functionality of or integrate many other R libraries
728+ (e.g., ``r-vetiver ``)
729+ - are cornerstone R packages that provide often-used functions
730+ (e.g., ``r-rmarkdown ``)
731+
732+ Testing R library loading
733+ ^^^^^^^^^^^^^^^^^^^^^^^^^
734+
735+ The minimal test of an R package should ensure that the delivered library
736+ can be successfully imported. This is accomplished in the ``meta.yaml ``
737+ with:
738+
739+ .. code-block :: yaml
740+
741+ test :
742+ commands :
743+ - $R -e "library('PackageName')" # [not win]
744+ - " \" %R%\" -e \" library('PackageName')\" " # [win]
745+
746+ Note that ``PackageName `` is the name imported by R; not necessarily
747+ the name of the conda package (e.g., ``r-matrix `` delivers ``Matrix ``).
748+
749+ Running ``testthat `` tests
750+ ^^^^^^^^^^^^^^^^^^^^^^^^^^
751+
752+ A typical ``test `` section for an R package with ``testthat `` testing
753+ will look like
754+
755+ .. code-block :: yaml
756+
757+ test :
758+ requires :
759+ - r-testthat
760+ source_files :
761+ - tests/
762+ commands :
763+ - $R -e "library('PackageName')" # [not win]
764+ - $R -e "testthat::test_file('tests/testthat.R', stop_on_failure=TRUE)" # [not win]
765+ - " \" %R%\" -e \" library('PackageName')\" " # [win]
766+ - " \" %R%\" -e \" testthat::test_file('tests/testthat.R', stop_on_failure=TRUE)\" " # [win]
767+
768+ .. note ::
769+ We recommend including a library loading check *before * the ``testthat ``
770+ tests.
771+
772+ First, one needs to declare that the test environment have ``r-testthat ``
773+ installed. One may need additional requirements here, especially if a
774+ package has optional functionality that is tested.
775+
776+ .. note ::
777+ If any ``testthat `` tests fail due to missing packages, maintainers
778+ are encouraged to communicate this to the upstream repository. Some R
779+ packages have optional functionality that usually involves packages
780+ listed under the ``Suggests: `` section of the ``DESCRIPTION `` file.
781+ Developers should be using ``testthat::skip_if_not_installed() ``
782+ functions to guard against test failures when optional packages are
783+ not installed. Posting an Issue or Pull Request when this is not
784+ done will help improve testing practices in the R ecosystem.
785+
786+ Second, one needs to declare where to source the tests. R package tests will
787+ be found in the ``tests/ `` directory of the tarball. This will typically
788+ include a ``tests/testthat.R `` file and additional tests under
789+ ``tests/testthat/test_*.R ``. Auxiliary directories and files may also be
790+ present and needed for specific tests.
791+
792+ The default R build procedure on conda-forge will not include the
793+ ``tests/ `` directory in the final build. While it is possible to do this
794+ (via an ``--install-tests `` flag), it is preferable to use the
795+ ``tests.source_files `` in the ``meta.yaml `` to copy the tests for the
796+ testing phase only.
797+
798+ Finally, one uses the ``testthat::test_file() `` function to test the
799+ ``tests/testthat.R `` file, which for most packages serves as the main entry
800+ point for all the other tests. By default, this function does not return
801+ an error value on test failures, so one needs to pass the argument
802+ ``stop_on_failure=TRUE `` to ensure that test failures propagate to
803+ conda-build.
804+
805+ There are scenarios where the ``tests/testthat.R `` file does not orchestrate
806+ the individual tests. In that case, one can instead test the
807+ ``tests/testthat `` directory with
808+
809+ .. code-block :: yaml
810+
811+ test :
812+ commands :
813+ - $R -e "testthat::test_dir('tests/testthat/', package='PackageName', load_package='installed')" # [not win]
814+ - " \" %R%\" -e \" testthat::test_dir('tests/testthat/', package='PackageName', load_package='installed')\" " # [win]
815+
816+ In this case, the function will error on any failures by default. Again,
817+ the ``PackageName `` here refers to the R library name.
818+
715819Tests outside of the package
716820............................
717821
0 commit comments