@@ -712,6 +712,109 @@ If the utility actually has a test mode, great. Otherwise simply invoking
712
712
``--help `` or ``--version `` or something will at least test that it is
713
713
installed and can run.
714
714
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
+ will 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 test environment should have ``r-testthat ``
773
+ installed. One may need additional packages here, especially if a package
774
+ 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. Many 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 only copy the tests for the
796
+ testing phase.
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 on test failures, so one needs to pass the argument
802
+ ``stop_on_failure=TRUE `` to ensure that test failures propagate to the CI.
803
+
804
+ There are scenarios where the ``tests/testthat.R `` file does not orchestrate
805
+ the individual tests. In that case, one can instead test the
806
+ ``tests/testthat `` directory with
807
+
808
+ .. code-block :: yaml
809
+
810
+ test :
811
+ commands :
812
+ - $R -e "testthat::test_dir('tests/testthat/', package='PackageName', load_package='installed')" # [not win]
813
+ - " \" %R%\" -e \" testthat::test_dir('tests/testthat/', package='PackageName', load_package='installed')\" " # [win]
814
+
815
+ In this case, the function will error on any failures by default. Again,
816
+ the ``PackageName `` here refers to the R library name.
817
+
715
818
Tests outside of the package
716
819
............................
717
820
0 commit comments