-
Notifications
You must be signed in to change notification settings - Fork 24
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 4 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,59 @@ | ||
# 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, it is beneficial to add test(s) corresponding to the new code. While doing so it is essential to check whether `make test-Specific` still works with the new code included. In particular, check whether `cd tests; make no-segfault.Rout` work on a standalone computer without the requirement of an interactive user intervention. | ||
|
||
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. | ||
SaranjeetKaur marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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. We try to have tests both for normal behaviour and for error conditions. Tests live in the `tests` directory. | ||
SaranjeetKaur marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
SaranjeetKaur marked this conversation as resolved.
Show resolved
Hide resolved
|
||
## Benchmarks | ||
|
||
Benchmarking is useful to test that a change does not degrade performance. | ||
SaranjeetKaur marked this conversation as resolved.
Show resolved
Hide resolved
SaranjeetKaur marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## The `tests` directory in a R package | ||
SaranjeetKaur marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
When one looks at the package structure of R or the sources of an R package, it consists of the directory called `tests` among other files and directories. The image below shows the structure of the R package `spatial` with the `tests` directory highlighted with yellow colour. | ||
SaranjeetKaur marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
 | ||
|
||
The `tests` directory consists of additional package-specific test code, similar to the specific tests that come with a R distribution. The `tests` directory contains test code in a `.R` file (or `.r` file from R 3.4.0) or `.Rin` file which contains code that in turn creates the corresponding `.R` file. The test code can be provided directly to the `tests` directory in `.R`, `.r`, or `.Rin` as may be appropriate. | ||
SaranjeetKaur marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Whenever the `.R` file from the `tests` directory is run, the results are written to a `.Rout` file. In some cases there exists a subdirectory called `Examples` within the `tests` directory, which contains a corresponding `packagename-Ex.Rout.save` file. The output of a new run of the `.R` file and/or from a new run of the examples is compared with the existing `.Rout` file or `packagename-Ex.Rout.save` file, respectively, with differences being reported but not causing an error. 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. This ensures that the copy of the package installed during testing will be found by `library(packagename)`. | ||
SaranjeetKaur marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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. Although it would be helpful to do so in all cases in order to avoid occasional failures whenever tests are run. | ||
SaranjeetKaur marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## Checking R packages | ||
|
||
The R package checker `R CMD check` can be used to test whether the `source` R packages are working correctly. It can be run either on one or more directories, or on compressed package `tar` archives with extension `.tar.gz`, `.tgz`, `.tar.bz2`, or `.tar.xz`. The final checks of a R package should be run on a `tar` archive prepared by `R CMD build`. This results into running a series of [checks](https://cran.r-project.org/doc/manuals/r-release/R-exts.html#Checking-and-building-packages), including running the `tests` directory, if it is available: | ||
|
||
- The examples provided by the package’s documentation are also run. If there is a file `tests/Examples/packagename-Ex.Rout.save`, the output of running the examples is compared to that file. Released packages are able to run their own examples. Each example is run in a clean environment. Hence, earlier examples cannot be assumed to have been run. The variables T and F are also redefined to generate an error unless they are set in the example. | ||
|
||
- If there are other tests specified in the `tests` directory then they are also run. Typically they will be a set of `.R` (or `.r` or `.Rin`) source files and target output files `.Rout.save`. | ||
|
||
The comparison between the existing file and that generated newly is in the end user\’s locale, hence, the target output files should be ASCII, if at all possible. To specify tests in a non-standard location, the command line option `--test-dir=foo` may be used. For example, unusually slow tests could be placed in `inst/slowTests` and can be run using `R CMD check --test-dir=inst/slowTests`. Similarly tests that require Oracle to be installed could be placed in `inst/testWithOracle`, tests which use random values and may occasionally fail by chance could be placed in `inst/randomTests`, etc.. | ||
|
||
While performing these checks, the code in package vignettes is also executed, and the vignette PDFs re-made from their sources as a check of completeness of the sources, unless the `BuildVignettes` field in the package’s `DESCRIPTION` file has a false value. If there is a target output file `.Rout.save` in the vignette source directory, the output from running the code in that vignette is compared with the target output file and any differences are reported, but not recorded in the log file. | ||
SaranjeetKaur marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## Testing practices | ||
|
||
One must be careful of what their tests (and examples) actually test. Some bad practice seen in distributed packages include: | ||
SaranjeetKaur marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
- 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 difficulty and not advisable. | ||
SaranjeetKaur marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
- The exact format of R messages (from R itself or from other packages) change and can be translated. Hence, it is not a good idea to test their exact format. | ||
SaranjeetKaur marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
- For automated checking systems, it is not worthwhile for tests to use `options(warn = 1)` for reporting. | ||
SaranjeetKaur marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## 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.