-
Notifications
You must be signed in to change notification settings - Fork 1
/
testing.Rnw
66 lines (59 loc) · 2.4 KB
/
testing.Rnw
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
\section{Package unit testing}
\begin{frame}
\begin{block}{How to test the code in your package?}
Or how to make sure that changes in your code do not break existing functionality?
\begin{itemize}
\item Implicitely, documentation examples and a vignette do some tests.
\item Using \R's built-in testing, that runs some code and compares the output to a saved template.
\item Specific packages for unit testing:
\Rpackage{RUnit}\footnote{\url{http://cran.r-project.org/web/packages/RUnit/index.html}} or
\Rpackage{testthat}\footnote{\url{http://cran.r-project.org/web/packages/testthat/index.html}}.
\end{itemize}
\end{block}
\end{frame}
\begin{frame}{Using an \texttt{.Rout.save} file}
\begin{block}{In \texttt{package/tests/}}
Create
\begin{itemize}
\item \texttt{mytest.R} with code to be tested
\item \texttt{mytest.Rout.save} with the reference output
\end{itemize}
When \texttt{check}ing your package \R will
\begin{enumerate}
\item execute the code in \texttt{mytest.R}
\item save the output to \texttt{mytest.Rout}
\item compare \texttt{mytest.Rout} to \texttt{mytest.Rout.save}
\item report any differences
\end{enumerate}
\end{block}
\end{frame}
\begin{frame}{Using \Rpackage{testthat}}
\begin{block}{Test individual expression}
\texttt{expect\_that(object\_or\_expression, condition)} with conditions
\begin{description}
\item[equals] \texttt{expect\_that(1+2,equals(3))} or \texttt{expect\_equal(1+2,3)}
\item[gives\_warning] \texttt{expect\_that(warning("a"), gives\_warning())}
\item[is\_a] \texttt{expect\_that(1, is\_a("numeric"))} or \texttt{expect\_is(1,"numeric")}
\item[is\_true] \texttt{expect\_that(2 == 2, is\_true())} or \texttt{expect\_true(2==2)}
\item[matches] \texttt{expect\_that("Testing is fun", matches("fun"))} or
\texttt{expect\_match("Testing is fun", "f.n")}
\item[takes\_less\_than] \texttt{expect\_that(Sys.sleep(1),takes\_less\_than(3))}
\item[...]
\end{description}
\end{block}
\end{frame}
\begin{frame}[fragile]{Using \Rpackage{testthat}}
\begin{example}
<<testthat1,echo=TRUE,tidy=FALSE>>=
library(sequences)
library(testthat)
a <- new("DnaSeq",sequence="ACGTaa")
test_that("ok test", {
expect_equal(length(a),6)
expect_true(seq(a)=="ACGTAA")
expect_is(a,"DnaSeq")
})
expect_true(seq(a)=="ACGTaa")
@
\end{example}
\end{frame}