1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004
|
%\VignetteIndexEntry{Using tinytest}
\documentclass[11pt]{article}
\usepackage{enumitem}
\usepackage{xcolor} % for color definitions
\usepackage{sectsty} % to modify heading colors
\usepackage{fancyhdr}
\setlist{nosep}
% simpler, but issue with your margin notes
\usepackage[left=1cm,right=3cm, bottom=2cm, top=1cm]{geometry}
%\usepackage{vmargin}
%\setpapersize{USletter} % or a4 for you
% Left Top Right Bottom headheight?? headsep footheight footsep
%\setmarginsrb{0.75in}{0.25in}{1.1in}{0.25in}{15pt}{0pt}{10pt}{20pt}
\usepackage{hyperref}
\definecolor{bluetext}{RGB}{0,101,165}
\definecolor{graytext}{RGB}{80,80,80}
\hypersetup{
pdfborder={0 0 0}
, colorlinks=true
, urlcolor=blue
, linkcolor=bluetext
, linktoc=all
, citecolor=blue
}
\sectionfont{\color{bluetext}}
\subsectionfont{\color{bluetext}}
\subsubsectionfont{\color{bluetext}}
% no serif=better reading from screen.
\renewcommand{\familydefault}{\sfdefault}
% header and footers
\pagestyle{fancy}
\fancyhf{} % empty header and footer
\renewcommand{\headrulewidth}{0pt} % remove line on top
\rfoot{\color{bluetext} tinytest \Sexpr{packageVersion("tinytest")}}
\lfoot{\color{black}\thepage} % side-effect of \color{}: lowers the printed text a little(?)
\usepackage{fancyvrb}
% custom commands make life easier.
\newcommand{\code}[1]{\texttt{#1}}
\newcommand{\pkg}[1]{\textbf{#1}}
\let\oldmarginpar\marginpar
\renewcommand{\marginpar}[1]{\oldmarginpar{\color{bluetext}\raggedleft\scriptsize #1}}
% skip line at start of new paragraph
\setlength{\parindent}{0pt}
\setlength{\parskip}{1ex}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\title{Using tinytest}
\author{Mark van der Loo}
\date{\today{} | Package version \Sexpr{packageVersion("tinytest")}}
\begin{document}
\DefineVerbatimEnvironment{Sinput}{Verbatim}{fontshape=n,formatcom=\color{graytext}}
\DefineVerbatimEnvironment{Soutput}{Verbatim}{fontshape=sl,formatcom=\color{graytext}}
\newlength{\fancyvrbtopsep}
\newlength{\fancyvrbpartopsep}
\makeatletter
\FV@AddToHook{\FV@ListParameterHook}{\topsep=\fancyvrbtopsep\partopsep=\fancyvrbpartopsep}
\makeatother
\setlength{\fancyvrbtopsep}{0pt}
\setlength{\fancyvrbpartopsep}{0pt}
\maketitle{}
\thispagestyle{empty}
\tableofcontents{}
<<echo=FALSE>>=
options(prompt="R> ",
continue = " ",
width=75,
tt.pr.color=FALSE)
@
\subsection*{Reading guide}
Readers of this document are expected to know how to write R functions
and have a basic understanding of a package source directory structure.
\newpage{}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Purpose of this package: unit testing}
The purpose of \emph{unit testing} is to check whether a function gives the
output you expect, when it is provided with certain input. So unit testing is
all about comparing \emph{desired} outputs with \emph{realized} outputs. The
purpose of this package is to facilitate writing, executing and analyzing unit
tests.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Expressing tests}
Suppose we define a function translating pounds (lbs) to kilograms inaccurately.
<<>>=
lbs2kg <- function(x){
if ( x < 0 ){
stop(sprintf("Expected nonnegative weight, got %g",x))
}
x/2.20
}
@
We like to check a few things before we trust it.
<<>>=
library(tinytest)
expect_equal(lbs2kg(1), 1/2.2046)
expect_error(lbs2kg(-3))
@
The value of an \code{expect\_*} function is a \code{logical}, with some
attributes that record differences, if there are any. These attributes are used
to pretty-print the results.
<<>>=
isTRUE( expect_true(2 == 1 + 1) )
@
\subsection{Test functions}
Currently, the following expectations are implemented.
\begin{center}
\begin{tabular}{ll}
\textbf{Function} & \textbf{what it expects}\\
\code{expect\_equal(current, target)} & equality (using \code{all.equal})\\
\code{expect\_equivalent(current, target)} & equality, ignoring attributes\\
\code{expect\_identical(current, target)} & equality, (using, \code{identical})\\
\code{expect\_length(current, length)} & check lenght of object\\
\code{expect\_true(current)} & \code{current} evaluates to \code{TRUE}\\
\code{expect\_false(current)} & \code{current} evaluates to \code{FALSE}\\
\code{expect\_match(current, pattern)} & All strings in \code{current} match \code{pattern}\\
\code{expect\_inherits(current, class)} & \code{current} inherits from \code{class}\\
\code{expect\_null(current)} & \code{current} evaluates to \code{NULL}\\
\code{expect\_error(current, pattern)} & error message matching \code{pattern}\\
\code{expect\_warning(current, pattern)} & warning message matching \code{pattern}\\
\code{expect\_message(current, pattern)} & message matching \code{pattern}\\
\code{expect\_silent(current)} & expect no warnings or errors (just run)\\
\code{expect\_stdout(current, pattern)} & expect output to \code{stdout} matching \code{pattern}\\
\code{expect\_equal\_to\_reference(current, file)} & expect object equal to object stored in RDS file\\
\code{expect\_equivalent\_to\_reference(current, file)} & expect object equivalent to object stored in RDS file\\
\end{tabular}
\end{center}
Here, \code{target} is the intended outcome and \code{current} is the observed
outcome. Also, \code{pattern} is interpreted as a regular expression.
<<>>=
expect_error(lbs2kg(-3), pattern="nonnegative")
expect_error(lbs2kg(-3), pattern="foo")
@
\subsection{Alternative syntax}
The syntax of the test functions should be familiar to users of the
\code{testthat} package\cite{wickham2016testthat}. In test files only, you can
use equivalent functions in the style of \code{RUnit}\cite{burger2016RUnit}. To
be precise, for each function of the form \code{expect\_lol} there is a
function of the form \code{checkLol}.
\subsection{Interpreting the output and print options}
Let's have a look at an example again.
<<>>=
expect_false( 1 + 1 == 2, info="My personal message to the tester" )
@
The output of these functions is pretty self-explanatory, nevertheless we see
that the output of these expect-functions consist of
\begin{itemize}
\item The result: \code{FAILED}, \code{PASSED} or \code{SIDEFX}. The latter only occurs
when side effects are monitored (see \S\ref{sect:side})
\item The type of failure (if any) between square brackets. Current options
are as follows.
\begin{itemize}
\item \code{[data]} there are differences between observed and expected values.
\item \code{[attr]} there are differences between observed and expected attributes, such as column names.
\item \code{[xcpt]} an exception (warning, error) was expected but not observed.
\end{itemize}
When side effects are monitored, and the result is \code{SIDEFX}, a side
effect was observed. The type of side effect is reported between square brackets.
\begin{itemize}
\item \code{[envv]} An environmental variable was created, changed, or deleted.
\item \code{[wdir]} The working directory has changed.
\item \code{[file]} A file operation occurred in the test directory or one of its subdirectories.
\end{itemize}
\item When relevant (see \S\ref{sect:testfiles}), the location of the test file and the relevant line numbers.
\item The test call.
\item When relevant, a summary of the differences between observed and expected
values or attributes, or a summary of the observed side effect.
\item When present, a user-defined information message.
\end{itemize}
The result of an \code{expect\_} function is a \code{tinytest} object. You can
print them in long format (default) or in short, one-line format like so.
<<>>=
print(expect_equal(1+1, 3), type="short")
@
\marginpar{\code{print} method}
Functions that run multiple tests return an object of class \code{tinytests}
(notice the plural). Since there may be a lot of test results, \pkg{tinytest}
tries to be smart about printing them. The user has ultimate control over this
behaviour. See \code{?print.tinytests} for a full specification of the options.
\section{Test files}
\label{sect:testfiles}
In \pkg{tinytest}, tests are scripts, interspersed with statements that perform
checks. An example test file in tinytest can look like this.
\begin{verbatim}
# contents of test_addOne.R
addOne <- function(x) x + 2
expect_true(addOne(0) > 0)
hihi <- 1
expect_equal(addOne(hihi), 2)
\end{verbatim}
A particular file can be run using\marginpar{\code{run\_test\_file}}
<<eval>>=
run_test_file("test_addOne.R", verbose=0)
@
We use \code{verbose=0} to avoid cluttering the output in this vignette. By
default, verbosity is turned on, and a counter is shown while tests are run.
The counter is colorized on terminals supporting ANSI color extensions. If you
are uncomfortable reading these colors or prefer colorless output, use
\code{color=FALSE} or set \code{options(tt.pr.color=FALSE)}.
The numbers between \code{<-->} indicate at what lines in the file the failing
test can be found.
By default only failing tests are printed. You can store the output and print
all of them.
<<>>=
test_results <- run_test_file("test_addOne.R", verbose=0)
print(test_results, passes=TRUE)
@
Or you can set
<<eval=FALSE>>=
options(tt.pr.passes=TRUE)
@
to print all results during the active R session.
To run all test files in a certain directory, we can
use\marginpar{\code{run\_test\_dir}}
<<eval=FALSE>>=
run_test_dir("/path/to/your/test/directory")
@
By default, this will run all files of which the name starts with
\code{test\_}, but this is customizable.
\subsection{Summarizing test results, getting the data}
To create some results, run the tests in this package.
<<>>=
out <- run_test_dir(system.file("tinytest", package="tinytest")
, verbose=0)
@
The results can be turned into data using \code{as.data.frame}.
\marginpar{\code{as.data.frame}}
<<>>=
head(as.data.frame(out), 3)
@
The last two columns indicate the line numbers where the test was defined.
A `summary` of the output gives a table with passes and fails per file.
\marginpar{\code{summary}}
<<>>=
summary(out)
@
\subsection{Programming over tests, ignoring test results, exiting early}
Test scripts are just R scripts interspersed with tests. The test runners make
sure that all test results are caught, unless you tell them not to. For
example, since the result of a test is a \code{logical} you can use them as a
condition.
<<eval=FALSE>>=
if ( expect_equal(1 + 1, 2) ){
expect_true( 2 > 0)
}
@
Here, the second test (\code{expect\_true(2 > 0)}) is only executed if the
first test results in \code{TRUE}. In any case the result of the first test
will be caught in the test output, when this is run with \code{run\_test\_file}
\code{run\_test\_dir}, \code{test\_all}, \code{build\_install\_test} or through
\code{R CMD check} using \code{test\_package}.
If you want to perform the test, but not record the test result you can do the
following \marginpar{\code{ignore}} (note the placement of the brackets).
<<eval=TRUE>>=
if ( ignore(expect_equal)(1+1, 2) ){
expect_true(2>0)
}
@
Other cases where this may be useful is to perform tests in a loop, e.g. when
there is a systematic set of cases to test.
It is possible to exit a test file prematurely. For example when there
are a number of tests that are not relevant or possible on some OS,
you can do the following. \marginpar{\code{exit\_file}}
<<>>=
if ( Sys.info()[['sysname']] == "Windows"){
exit_file("Cannot test this on Windows")
}
@
This will cause \code{run\_test\_file} to stop file execution, print the
message, and report the information gathered up to where \code{exit} was
called. A function like \code{test\_all} will then continue with the next file,
so testing is not aborted completely.
There is also a convenience function called \code{exit\_if\_not} that functions
similarly to base R's \code{stopifnot}. It accepts a comma-separated list
of expressions, and if any one of them does not result in a single \code{TRUE}
the execution of the test file is stopped with a message. So you can
do things like this.
<<eval=FALSE>>=
exit_if_not(requireNamespace("slartibartfast", quietly=TRUE)
, packageVersion("slartibartfast") >= "1.0.0")
@
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Running order and side effects}
\label{sect:running}
It is a generally a good idea to write test files that are independent from
each other. This means that the order of running them is unimportant for the
test results and test files can be maintained independently. The function
\code{run\_test\_file} and by extension \code{run\_test\_dir},
\code{test\_all}, and \code{test\_package} encourage this by resetting
\begin{itemize}
\item options, set with \code{options()};
\item environment variables, set with \code{Sys.setenv()}
\end{itemize}
after a test file is executed.
To escape this behavior, use \code{base::Sys.setenv()} respectively
\code{base::options()}. Alternatively use
<<eval=FALSE>>=
run_test_dir("/path/to/my/testdir"
, remove_side_effects = FALSE)
test_all("/path/to/my/testdir"
, remove_side_effects = FALSE)
# Only in tests/tinytest.R:
test_package("PACKAGENAME", remove_side_effects=FALSE)
@
Test files are sorted and run based on the current locale. This means
that the order of execution is in general not platform-independent.
You can control the sorting behavior interactively or by setting
\code{options(tt.collate)}. To be precise, adding
<<eval=FALSE>>=
options(tt.collate="C")
@
to \code{/tests/tinytest.R} before running \code{test\_package} will
ensure bytewise sorting on most systems. See also
\code{help("run\_test\_dir")}.
\subsection{Monitoring side effects}
\label{sect:side}
The term 'side effect' is the technical expression for the situation where a
function or expression changes something outside of its scope. Examples include
creating, removing, or changing variables in R's global work space, R options,
or environment variables of your operating system. We will call such variables
or options \emph{external variables}.
To test for side-effects once, use the \code{side\_effects} argument
to any of the test runners. For example
<<eval=FALSE>>=
test_package("pkg", side_effects=TRUE)
@
There is control over which side-effects to track. For example to prevent
tracking changes in the working directory, do the following.
<<eval=FALSE>>=
test_package("pkg", side_effects=list(pwd=FALSE))
@
If you add \code{report\_side\_effects()} anywhere in a test file, certain
external variables are monitored from that point on, and for that file only.
It can be switched off again by calling \code{report\_side\_effects(FALSE)}
anywhere in the file. The reporting functionality will compare the external
state before and after every expression in the test file is run and report any
changes.
At the moment, effects that can be monitored include environment
variables, locale settings, the present working directory, and file operations
in the test directory.
Below is an example of a test file where side effects are recorded. The third
line creates an explicit side effect by creating a new environment variable
called \code{hihi} with the value \code{"lol"}.
\begin{verbatim}
# contents of test_se.R
report_side_effects()
expect_equal(1+1, 2)
Sys.setenv(hihi="lol")
expect_equal(1+1, 3)
Sys.setenv(hihi="lulz ftw")
\end{verbatim}
Running the test file yields an object of class \code{tinytests} as usual, only
now changes in environment variables are reported.
<<>>=
run_test_file("test_se.R", verbose=1)
@
Note that as discussed in Section~\S\ref{sect:running}, \pkg{tinytest} will
unset the environment variable \code{hihi} automatically after running the file
because it was set directly by the author of the test file using
\code{Sys.setenv}. The real value of the reporting functionality is that it
also reports on external variables that are touched by other functions than
those you call explicitly in the file.
Reading and comparing versions of external variables takes some time.
Especially when it requires a call to the operating service such as a request
for values of environment variables. We therefore recommend this to be used
only when you suspect a side effect. Or, for example to execute
\code{report\_side\_effects()} conditional on \code{at\_home()}.
It is not possible to catch all types of side effects, even in principle, using
the \pkg{tinytest} reporting functionality. Examples include: packages that
keep a global variable or environment within their namespace to store some
state, and packages that rely on compiled code where there are global objects
within the shared object.
Side effects are to be avoided as a general and strong principle, but sometimes
there is little or no choice. In Section~\ref{sect:devil} we give some tips on
how to properly handle such situations.
\section{Testing packages}
Using \pkg{tinytest} for your package is pretty easy.
\begin{enumerate}
\item Testfiles are placed in \code{/inst/tinytest}. The testfiles all have
names starting with \code{test} (for example \code{test\_haha.R}).
\item In the file \code{/tests/tinytest.R} you place the code
\begin{verbatim}
if ( requireNamespace("tinytest", quietly=TRUE) ){
tinytest::test_package("PACKAGENAME")
}
\end{verbatim}
\item In your \code{DESCRIPTION} file, add \pkg{tinytest} to \code{Suggests:}.
\end{enumerate}
You can automatically create a minimal running test infrastructure
with the \code{setup\_tinytest} function. \marginpar{\code{setup\_tinytest}}
<<eval=FALSE>>=
setup_tinytest("/path/to/your/package")
@
In a terminal, you can now do
\begin{verbatim}
R CMD build /path/to/your/package
R CMD check PACKAGENAME_X.Y.Z.tar.gz
\end{verbatim}
and all tests will run.
To run all the tests interactively, make sure that all functions of your
new package are loaded. After that, run\marginpar{\code{test\_all}}
<<eval=FALSE>>=
test_all("/path/to/your/package")
@
where the default package directory is the current working directory.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Build--install--test interactively}
The most realistic way to unit-test your package is to build it, install it
and then run all the tests. The function
<<eval=FALSE>>=
build_install_test("/path/to/your/package")
@
does exactly that. It builds and installs the package in a temporary directory,
starts a fresh R session, loads the newly installed package and runs all tests.
The return value is a \code{tinytests} object.
The package is built without manual or vignettes to speed up the
whole process.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Testing functions that are not exported: use `:::`}
In Section~\ref{sect:spherical} it is argued that unit tests should as a rule
of thumb focus on the functions that are visible to the user. However, there
are cases where it may be preferred to test an internal function. For example
when there are two user-visible functions that call the same underlying,
unexported function with different arguments. Or when one of the internal
functions implements a numerical algorithm that requires thorough testing.
To test functions in your package that are not visible to users that load
your package, use the triple-colon operator like so.
<<eval=FALSE>>=
output = pkg:::some_internal_function(1)
expect_equal(output, 2)
@
This is perfectly ok, and is also accepted by \code{R CMD check --as-cran}.
\pkg{Tinytest} does not make those internal functions directly callable like some
other unit testing packages do. Making internal functions callable means that
\begin{enumerate}
\item \pkg{tinytest} needs to simulate loading a package, except for the namespace
restrictions;
\item there may be significant differences between the environment in which you
test the functions, and the environment which a user sees when loading the
package;
\item the way the package is loaded during testing may differ from the way it is loaded
when a user loads it;
\item exported functions are not clearly distinguished from internal functions in
the test code.
\end{enumerate}
accurately simulating how R loads a package is no small matter. It would
require a significant epansion of \code{tinytest}'s code base that would have
to be kept synchronised with the way R loads packages (possibly with backport
options when R would change in that area).
So in short: let's keep things simple and let R do what it knows how to do.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Using data stored in files}
\label{sect:comparefiles}
When your package is tested with \code{test\_package}, \pkg{tinytest} ensures
that your working directory is the testing directory (by default
\code{tinytest}). This means you can read files that are stored in your folder
directly.
Suppose that your package directory structure looks like this (default):
\begin{itemize}
\item[]\code{/inst}
\begin{itemize}
\item[]\code{/tinytest}
\begin{itemize}
\item[]\code{/test.R}
\item[]\code{/women.csv}
\end{itemize}
\end{itemize}
\end{itemize}
Then, to check whether the contents of \code{women.csv} is equal to the
built-in \code{women} dataset, the content of \code{test.R} looks as follows.
<<eval=FALSE>>=
dat <- read.csv("women.csv")
expect_equal(dat, women)
@
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Skipping tests on \code{CRAN}}
It is not possible to detect whether a test is running on CRAN. This means we
are forced to detect that we are running tests in our own environment.
In the following example we use the host name to detect if we are running on
our own machine and explicitly pass this information to \code{test\_package}.
<<echo=FALSE>>=
options(prompt=" ", continue=" ")
@
<<eval=FALSE>>=
# contents of pkgdir/tests/tinytest.R
if ( requireNamespace("tinytest", quietly=TRUE) ){
home <- identical( Sys.info()["nodename"], "YOURHOSTNAME" )
tinytest::test_package("PKGNAME", at_home = home)
}
@
Other ways to detect whether you are running `at home' include
\begin{itemize}
\item Set a custom environment variable (from your OS) and detect it with \code{Sys.getenv}.
<<>>=
home <- identical( Sys.getenv("HONEYIMHOME"), "TRUE" )
@
\item Use 4-number package versioning while developing and 3-number versioning
for CRAN releases\footnote{As \href{https://stackoverflow.com/questions/36166288/skip-tests-on-cran-but-run-locally}{recommended here} by Dirk Eddelbuettel.}.
<<eval=FALSE>>=
home <- length(unclass(packageVersion("PKGNAME"))[[1]]) == 4
@
\end{itemize}
<<echo=FALSE>>=
options(prompt="R> ", continue=" ")
@
\subsubsection*{When tests are run interactively}
All the interactive test runners have \code{at\_home=TRUE} by default, so while
you are developing all tests are run, unless you exclude them explicitly.
<<>>=
run_test_file("test_hehe.R", verbose=0)
run_test_file("test_hehe.R", verbose=0, at_home=FALSE)
@
Here is an overview of test runners and their default setting for \code{at\_home}.
\begin{center}
\begin{tabular}{rll}
Function & Default \code{at\_home} & Intended use \\
\hline
\code{run\_test\_file} & \code{TRUE} & Interactive by developer\\
\code{run\_test\_dir} & \code{TRUE} & Interactive by developer\\
\code{test\_all} & \code{TRUE} & Interactive by developer\\
\code{build\_install\_test} & \code{TRUE} & Interactive by developer\\
\code{test\_package} & \code{FALSE} & \code{R CMD check}, or after installation by user.
\end{tabular}
\end{center}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Testing your package after installation}
Supposing your package is called \pkg{hehe} and the \pkg{tinytest}
infrastructure is used. If the package is installed, the following command runs
\pkg{hehe}'s tests.
<<eval=FALSE>>=
tinytest::test_package("hehe")
@
This can come in handy when a user of \pkg{hehe} reports a bug and you want to
make sure all tested functionality works on the user's system.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Using extension packages}
It is possible for other packages to add custom assertions
(\code{expect}-functions). To use such a package:
\begin{enumerate}
\item Add the extension package to the \code{Suggests:} field in the \code{DESCRIPTION} file.
\item Add \code{using(pkg)} to \emph{each} test file that use the extensions (see \code{?using}). \marginpar{\code{using}}
\end{enumerate}
When multiple extension packages are loaded, and when there are name
collisions, the packages loaded later takes precedence over the ones loaded
earlier (as usual in R). This includes assertions exported by \pkg{tinytest}.
\textbf{Note.} Other than in regular R, it is not possible to disambiguate
functions using namespace resolution as in \code{pkg::expect\_something},
because in that case the test result will not be caught by \pkg{tinytest}.
The API for building extension packages is described in \code{?register\_tinytest\_extension}.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Mocking databases}
The \pkg{dittodb} package\cite{keane2020dittodb} is capable of mocking
pre-recorded database requests.
To use extensions like \code{dittodb}, put the package in \code{Suggests:} in
the \code{DESCRIPTION} and load it in the test file. The package captures
responses from SQL connections and saves them to R files. Therefore, capture
the requested response prior to testing by wrapping your request in
\code{start\_db\_capturing()} and \code{stop\_db\_capturing()}. Optionally, specify
the file path you want your mocks to be saved. For examples using
\code{dittodb}, see \href{https://dittodb.jonkeane.com/}{here}.
In the testing scripts, load the package and wrap your tests in
\code{with\_mock\_path(path, with\_mock\_db())} like
<<eval=FALSE>>=
require(dittodb)
with_mock_path(
system.file("<path-to-your-mocks>", package = "myPackage"),
with_mock_db({
# <unit tests which rely on database connections>
})
)
@
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Testing with environmental variables}
\label{sect:envvar}
In \pkg{tinytest} you can run tests with custom environment variable settings
easily. Just add the \code{set\_env} argument to any of \code{run\_test\_file()},
\code{run\_test\_dir()} or \code{test\_package()}. Here is an example.
<<eval=FALSE>>=
test_package("tinytest", set_env = list(WA_BABALOOBA="BA_LA_BAMBOO"))
@
With this option, the environment variable will be set during testing and unset
afterwards. Setting and unsetting environment variables like this will not be
recorded as a side effect. If there is code in the test file that changes this
variable, then it is recorded.
Do note that R uses some environment variables that are read during startup,
such as \code{\_R\_OPTIONS\_STRINGS\_AS\_FACTORS\_}. Setting these at runtime
has no effect.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Running tests in parallel}
In \pkg{tinytest}, a file should be considered a closed unit: no information
created in one test file should be used in another. Under this condition,
tests can automatically run in parallel by running different files in different
R sessions.
Running code in parallel takes some careful consideration around setting up a
cluster, running the tests, and closing the cluster of preparing it for the
next run. Depending on the test runner used, there are different levels of
control and responsibility for the user to prepare the program for
parallelization. Below we describe them from less easier to more control.
\subsubsection*{\code{build\_install\_test}}
This function creates and installs a package in a temporary
location. By setting the \code{ncpu} parameter, the number of
cores used at the testing phase can be increased.
<<eval=FALSE>>=
build_install_test("/path/to/your/package", ncpu=2)
@
We already mentioned that the order in which files are run is in principle
system-dependent and it is a good practice not to rely on it. Under parallel
situations, all bets on file order are off.
\subsubsection*{\code{test\_package}.}
This function assumes that a package is installed. It can gather any
information necessary to parallelize a test run. The simplest way to
parallelize is to specify the number of CPUs used.
<<eval=FALSE>>=
test_package("PACKAGENAME", ncpu=2)
@
Here, \code{test\_package} will
\begin{enumerate}
\item Set up a local cluster using \code{parallel::makeCluster}.
\item Load the package on each R instance of the cluster.
\item Run test files in parallel over the cluster.
\item Collect the results and close the cluster.
\end{enumerate}
In stead of just passing the number of CPUs it is possible to pass a
\code{cluster} object. In that case \code{test\_package} will still load the
package on each node. However, note that if the package gets updated and
reinstalled, it should also be reloaded. It is in general hard to completely
unload a package in R (see \code{?detach} and \code{?unloadNamespace} for some
details on artifacts that will not be removed). So our advice is
to restart a cluster for each test run.
\subsubsection*{\code{run\_test\_dir}, \code{test\_all}}
These function assumes that all functionality needed to run the
tests is loaded. They accept an object of type \code{cluster}.
The user is responsible for setting up the nodes.
<<eval=FALSE>>=
cl <- parallel::makeCluster(4, outfile="")
parallel::clusterCall(cl, source, "R/myfunctions.R")
run_test_dir("inst/tinytest", cluster=cl)
@
where the argument \code{outfile=""} ensures that messages from each node
are forwarded to the master node.
It is possible to keep the cluster `alive', so modifications
can be made to \code{"R/myfunctions.R"} and then run for
example the following.
<<eval=FALSE>>=
parallel::clusterCall(cl, source, "R/myfunctions.R")
test_all(cluster=cl)
stopCluster(cl)
@
For heavy test routines it is thus possible to keep a test cluster
up to offload computations.
For more complex situations, including packages that use \code{S4} classes, or
compiled code, (re)loading takes more effort than sourcing a few R files. In
this cases it is often easier to restart a clean cluster for each test round.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{A few tips on packages and unit testing}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Make your package spherical}
\label{sect:spherical}
Larger packages typically consist of functions that are visible to the users
(exported functions) and a number of functions that are only used by the
exported functions. For example:
<<>>=
# exported, user-visible function
inch2cm <- function(x){
x*conversion_factor("inch")
}
# not exported function, package-internal
conversion_factor <- function(unit){
confac <- c(inch=2.54, pound=1/2.2056)
confac[unit]
}
@
We can think of the exported functions as the \emph{surface} of the package and
all the other functions as the \emph{volume}. The surface is what a user sees,
the volume is what the developer sees. The surface is how a user interacts with
a package.
If the surface is small (few functions exported), users are limited in the ways
they can interact with your package and that means there is less to test. So as
a rule of thumb, it is a good idea to keep the surface small. Since a sphere
has the smallest surface-to-volume ratio possible, I refer to this rule as
\emph{keep your package spherical}.
By the way, the technical term for the surface of a package is API (application
program interface).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Test the surface, not the volume}
Unexpected behavior (a bug) is often discovered when someone who is not the
developer starts using code. Bugfixing implies altering code and it may even
require you to refactor large chunks of code that is internal to a package. If
you defined extensive tests on non-exported functions, this means you need to
rewrite the tests as well. As a rule of thumb, it is a good idea to test only
the behaviour at the surface, so as a developer you have more freedom to change
the internals. This includes rewriting and renaming internal functions completely.
By the way, it is bad practice to change the surface, since that means you are
going to break other people's code. Nobody likes to program against an API that
changes frequently, and everybody hates to program against an API that changes
unexpectedly.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{How many tests do I need?}
When you call a function, you can think of its arguments flowing through a
certain path from input to output. As an example, let's take a look again at a
new, slightly safer unit conversion function.
<<>>=
pound2kg <- function(x){
stopifnot( is.numeric(x) )
if ( any(x < 0) ){
warning("Found negative input, converting to positive")
x <- abs(x)
}
x/2.2046
}
@
If we call \code{lbs2kg} with argument \code{2}, we can write:
\begin{verbatim}
2 -> /2.2046 -> output
\end{verbatim}
If we call \code{lbs2kg} with argument \code{-3} we can write
\begin{verbatim}
-3 -> abs() -> /2.2046 -> output
\end{verbatim}
Finally, if we call \code{pound2kg} with \code{"foo"} we can write
\begin{verbatim}
"foo" -> stop() -> Exception
\end{verbatim}
So we have three possible paths. In fact, we see that every nonnegative number
will follow the first path, every negative number will follow the second path
and anything nonnumeric follows the third path. So the following test suite
fully tests the behaviour of our function.
<<eval=FALSE>>=
expect_equal(pound2kg(1), 1/2.2046 )
# test for expected warning, store output
expect_warning( out <- pound2kg(-1) )
# test the output
expect_equal( out, 1/2.2046)
expect_error(pound2kg("foo"))
@
The number of paths of a function is called its \emph{cyclomatic complexity}.
For larger functions, with multiple arguments, the number of paths typically
grows extremely fast, and it quickly becomes impossible to define a test for
each and every one of them. If you want to get an impression of how many tests
one of your functions in needs in principle, you can have a look at the
\pkg{cyclocomp} package of G\'abor Cs\'ardi\cite{csardi2016cyclocomp}.
Since full path coverage is out of range in most cases, developers often strive
for something simpler, namely \emph{full code coverage}. This simply means that
each line of code is run in at least one test. Full code coverage is no
guarantee for bugfree code. Besides code coverage it is therefore a good idea
to think about the various ways a user might use your code and include tests
for that.
To measure code coverage, I recommend using the \pkg{covr} package by Jim
Hester\cite{hester2018covr}. Since \pkg{covr} is independent of the tools or
packages used for testing, it also works fine with \pkg{tinytest}.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{It's not a bug, it's a test!}
If users of your code are friendly enough to submit a bug report when they find
one, it is a good idea to start by writing a small test that reproduces the
error and add that to your test suite. That way, whenever you work on your
code, you can be sure to be alarmed when a bug reappears.
Tests that represent earlier bugs are sometimes called \emph{regression tests}.
If a bug reappears during development, software engineers sometimes refer
to this as a \emph{regression}.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Side effects are the Devil}
\label{sect:devil}
Since side-effects manipulate variables outside of the scope of a function, or
even outside of R, they can cause bugs that are hard to reproduce. R offers a
mechanism to ensure that a function leaves the outside world as it was, once
your code stops running.
Suppose you need to change working directory within a function, \code{source} a
file and return to the working directory. A naive way to do this is like so.
\marginpar{\code{on.exit}}
<<>>=
bad_function <- function(file){
oldwd <- getwd()
setwd(dirname(file))
source(basename(file))
setwd(oldwd)
}
@
This all works fine, untill \code{file} contains faulty code and throws an
error. As result, the execution of \code{bad\_function} will stop and leave
the user in a changed working directory. With \code{on.exit} you can define
code that will be carried out before the function exits, either normally
or with an error.
<<>>=
good_function <- function(file){
oldwd <- getwd()
on.exit(setwd(oldwd))
setwd(dirname(file))
source(basename(file))
}
@
\newpage
\begin{thebibliography}{5}
\bibitem{wickham2016testthat}
\href{https://cran.r-project.org/package=testthat}{Unit Testing for R}
Hadley Wickham (2016). testthat: Get Started with Testing. The R Journal,
vol. 3, no. 1, pp. 5--10, 2011
\bibitem{burger2016RUnit}
Matthias Burger, Klaus Juenemann and Thomas Koenig (2018).
\href{https://CRAN.R-project.org/package=RUnit}{RUnit: R Unit Test Framework}
R package version 0.4.32.
\bibitem{csardi2016cyclocomp}
\href{https://cran.r-project.org/package=cyclocomp}{cyclocomp: cyclomatic complexity of R code}
G\'abor Cs\'ardi (2016).
R package version 1.1.0
\bibitem{hester2018covr}
\href{https://CRAN.R-project.org/package=covr}{covr: Test Coverage for Packages}
Jim Hester (2018).
R package version 3.2.1
\bibitem{keane2020dittodb}
\href{https://CRAN.R-project.org/package=dittodb}{dittodb: A Test Environment for Database Requests}
Jonathan Keane and Mauricio Vargas (2020).
R package version 0.1.1
\end{thebibliography}
\end{document}
|