-
Couldn't load subscription status.
- Fork 28
Create Testing chapter #73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SaranjeetKaur
wants to merge
14
commits into
main
Choose a base branch
from
testing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
292f665
Create testing_in_R.Rmd
SaranjeetKaur b8ea83f
Merge pull request #83 from r-devel/master
SaranjeetKaur 97a57f3
Update testing_in_R.Rmd
SaranjeetKaur fc68dd4
expand testing R chapter
SaranjeetKaur 386646b
minor edits
SaranjeetKaur 07d3071
Apply suggestions Heather's suggestions
SaranjeetKaur 29fc6b1
Update 08-testing_in_R.Rmd
SaranjeetKaur 03963e0
Merge branch 'main' into testing
SaranjeetKaur f6555db
fix typos
SaranjeetKaur aac9d07
fix formatting
SaranjeetKaur e85618c
add chapter to _quarto.yml
SaranjeetKaur 4d6344e
update quarto.yml
SaranjeetKaur 1abdd6a
fix yml conflicts
SaranjeetKaur 58199e5
move testing_in_r to chapters
SaranjeetKaur File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| # Testing in R {#TestR} | ||
|
|
||
| This chapter discusses about writing tests for R and extending the test suite for R. Usually, when one contributes a patch, one might also want to contribute tests that are able to cover the new code. | ||
|
|
||
| ## When and why to write a test? | ||
|
|
||
| Whenever you add new functionality to the core code or any of the packages distributed with R, you may be asked to add test(s) corresponding to the new code. | ||
|
|
||
| If the new code requires GUI interaction or accesses the Internet, then it is essential to add its name to the `stop list` in `tests/no-segfault.Rin`. | ||
|
|
||
| ## Writing tests for R | ||
|
|
||
| Writing tests for R is much like writing tests for your own code. Tests need to be thorough, fast, isolated, consistently repeatable, and as simple as possible. When you are adding tests to an existing test file, it is also recommended that you study the other tests in that file; it will teach you which precautions you have to take to make your tests robust and portable. | ||
|
|
||
| Tests for base R live in the [`tests` directory of the R sources](https://svn.r-project.org/R/trunk/tests/). Contributors mainly contribute to the "regression tests" which are in the files `reg-tests-1a.R`, `reg-tests-1b.R`, etc. These tests are added after fixing a bug, to ensure that future changes do not cause a regression that reintroduces the bug. If you have been asked to add tests for base functionality (something that is not in one of the add-on packages in the base distribution), you should add it to the latest regression test file, with the highest numbering (currently `reg-tests-3.R`). | ||
| ## Test files | ||
|
|
||
| Most tests in base R are `.R` files with R code implementing one of two types of test: | ||
|
|
||
| 1. Tests that produce an error on failure. These often involve a call to `stopifnot()`. | ||
| 2. Tests that produce output that is compared to reference output. | ||
|
|
||
| If a test file `.R` includes tests that produce output for comparison, running the tests will create an `.Rout` file that should be compared to an `.Rout.save` file with the same name. The tests are usually run as part of the check commands `make check` (for a basic check of R) and `make check-all` or `make check-devel` (for a thorough check with/without the recommended packages). Alternatively you can run a test file directly, for example if you have set the environment variable `TOP_SRCDIR` as the path for the top source directory: | ||
|
|
||
| ```sh | ||
| cd $TOP_SRCDIR/tests | ||
| R CMD BATCH reg-tests-3.R | ||
| ``` | ||
|
|
||
| will run `reg-tests-3.R` and create `reg-tests-3.Rout` in the same directory. If you have added a test or made a change that means you need to updated the reference output you can create or overwrite the reference output with, e.g. | ||
SaranjeetKaur marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```sh | ||
| mv reg-tests-3.Rout reg-tests-3.Rout.save | ||
| ``` | ||
|
|
||
| Note that only substantive changes are important - the check will not fail due to differences in the version of R, the locale, or output from `proc.time()`, so if these are the only differences the `.Rout.save` does not need to be updated. | ||
| ``` | ||
|
|
||
| Sometimes test `.R` files are generated from a `.Rin` file, for example `no-segfault.Rin` generates tests for functions in the base add-on packages, calling each function with a range of inputs (character, logical, `NULL`, etc). | ||
|
|
||
| ## Tests of add-on packages | ||
|
|
||
| The check commands (`make check` etc) run `R CMD check` on the add-on packages in the base distribution, so will run any package-specific tests. If a package has tests, they will be in the `tests` directory of the sources for that package. | ||
|
|
||
|  | ||
|
|
||
| The base add-on packages use the same form of tests that are used for base R as described in the [Test files]{#test-files} section. | ||
|
|
||
| When the package tests are run, the `tests` directory is copied to the check area, and the tests are run with the copy as the working directory and with `R_LIBS` set so that the copy of the package installed during testing will be found by `library(packagename)`. | ||
|
|
||
| All the package-specific tests are run in a vanilla R session without setting the random-number seed. Hence, for tests which use random numbers one needs to set the seed to obtain reproducible results. | ||
|
|
||
| ## Checking R packages | ||
|
|
||
| For more detail on the package checker `R CMD check` see the [Checking packages](https://cran.r-project.org/doc/manuals/r-devel/R-exts.html#Checking-packages-1) section of the Writing R Extensions manual. | ||
|
|
||
| ## Testing practices | ||
|
|
||
| Tests for R should follow the general good practice documented in the [Writing R Extensions](https://cran.r-project.org/doc/manuals/r-devel/R-exts.html) manual. In particular: | ||
|
|
||
| - Testing the time taken by a command is not reasonable. This is because, one cannot know how fast or how heavily loaded an R platform might be. At best one can test a ratio of times, however even that is difficult and not advisable. | ||
|
|
||
| - The exact format of messages from R or from add-on packages can change and may be translated. Hence, it is not a good idea to test their exact format. | ||
|
|
||
| - For tests comparing against reference output, use `options(warn = 1)` to print warnings as they occur or `options(warn = 2)` to turn off warnings. In the latter case `tools::assertWarning()` may be used to assert when a warning is expected, see for example [tests/d-p-q-r-tst-2.R](https://svn.r-project.org/R/trunk/tests/d-p-q-r-tests.R) | ||
|
|
||
| ## See also | ||
|
|
||
| 1. [Testing R Code](https://cran.r-project.org/doc/manuals/r-release/R-ints.html#Testing-R-code) | ||
|
|
||
| 2. [Writing R Extensions](https://cran.r-project.org/doc/manuals/r-release/R-exts.html) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.