-
Notifications
You must be signed in to change notification settings - Fork 4
/
L30-slides.tex
516 lines (442 loc) · 12 KB
/
L30-slides.tex
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
\documentclass{beamer}
\usetheme{Boadilla}
%\includeonlyframes{current}
\usepackage{times}
\usefonttheme{structurebold}
\usepackage{listings}
\usepackage{ragged2e}
\usepackage{pgf}
\usepackage{tikz}
\usepackage{alltt}
\usepackage[normalem]{ulem}
\usetikzlibrary{arrows}
\usetikzlibrary{automata}
\usetikzlibrary{shapes}
\usepackage{amsmath,amssymb}
\usepackage{rotating}
\usepackage{ulem}
\usepackage{listings}
\usepackage{enumerate}
\usepackage{tikz}
\tikzset{
every overlay node/.style={
draw=black,fill=white,rounded corners,anchor=north west,
},
}
\def\tikzoverlay{%
\tikz[baseline,overlay]\node[every overlay node]
}%
%\setbeamercovered{dynamic}
\setbeamertemplate{footline}[page number]{}
\setbeamertemplate{navigation symbols}{}
\usefonttheme{structurebold}
\title{Software Testing, Quality Assurance \& Maintenance---Lecture 30}
\author{Patrick Lam}
\date{March 20, 2015}
\colorlet{redshaded}{red!25!bg}
\colorlet{shaded}{black!25!bg}
\colorlet{shadedshaded}{black!10!bg}
\colorlet{blackshaded}{black!40!bg}
\colorlet{darkred}{red!80!black}
\colorlet{darkblue}{blue!80!black}
\colorlet{darkgreen}{green!80!black}
\newcommand{\rot}[1]{\rotatebox{90}{\mbox{#1}}}
\newcommand{\gray}[1]{\mbox{#1}}
\newenvironment{changemargin}[1]{%
\begin{list}{}{%
\setlength{\topsep}{0pt}%
\setlength{\leftmargin}{#1}%
\setlength{\rightmargin}{1em}
\setlength{\listparindent}{\parindent}%
\setlength{\itemindent}{\parindent}%
\setlength{\parsep}{\parskip}%
}%
\item[]}{\end{list}}
\lstset{ %
language=C++,
basicstyle=\ttfamily,commentstyle=\scriptsize\itshape,showstringspaces=false,breaklines=true}
\begin{document}
\begin{frame}
\titlepage
\end{frame}
\begin{frame}
\frametitle{Today}
\begin{changemargin}{2cm}
Result Verification for Tests.\\[2em]
Reference: Gerard Meszaros. \emph{xUnit Test Patterns: Refactoring Test Code}.
\end{changemargin}
\end{frame}
\begin{frame}
\frametitle{Goal}
\Large
\begin{changemargin}{2cm}
Good tests are \emph{self-checking}:\\[1em]
no errors, no failures = successful test.
\end{changemargin}
\begin{center}
\includegraphics[width=.6\textwidth]{L30/pass}
\end{center}
\end{frame}
\begin{frame}
\frametitle{Why Self-Checking Tests?}
\Large
\begin{changemargin}{2cm}
Tests automatically report status.\\[1em]
Enables ``keep the bar green''\\
coding style.\\[1em]
Worry less about introducing bugs.\\[1em]
Plus: Tests help document system specs.
\end{changemargin}
\end{frame}
\begin{frame}
\frametitle{Today's Plan}
\Large
\begin{changemargin}{1cm}
HOWTO make your tests self-checking.
\end{changemargin}
\end{frame}
\begin{frame}
\Large
\begin{changemargin}{2cm}
``Isn't it just calling asserts?''\\[1em]
\only<2>{ \hspace*{4cm} [sadly, no.]}
\end{changemargin}
\end{frame}
\begin{frame}
\Large
\begin{changemargin}{2cm}
Two questions about asserts:
\begin{enumerate}
\item Q: what for?\\
A: check method call results\\[1em]
\item Q: where?\\
A: usually after calling SUT \\ ~~~~(System Under Test)
\end{enumerate}
\end{changemargin}
\end{frame}
\begin{frame}
\frametitle{Counter Example}
\begin{changemargin}{2cm}
\lstinputlisting{L30/Counter.java}
\end{changemargin}
\end{frame}
\begin{frame}
\frametitle{Counter Test}
\begin{changemargin}{2cm}
\lstinputlisting{L30/CounterTest.java}
\end{changemargin}
\end{frame}
\begin{frame}
\frametitle{State or Behaviour?}
\begin{changemargin}{2cm}
Was Counter Test verifying state or behaviour?
\end{changemargin}
\end{frame}
\begin{frame}
\frametitle{State vs Behaviour}
\large
\begin{changemargin}{2cm}
{\bf State:} e.g. object field values.\\
\hspace*{1.5cm}
Call accessor methods to verify.\\[1em]
{\bf Behaviour:} which calls SUT makes.\\
\hspace*{1.5cm} Insert observation points, \\ \hspace*{1.5cm} monitor interactions.
\end{changemargin}
\end{frame}
\begin{frame}
\frametitle{Flight example}
\small
\lstinputlisting{L30/flight.java}
\end{frame}
\begin{frame}
\frametitle{Flight example: state verification}
\small
\lstinputlisting{L30/flight_extended_state_spec.java}
\end{frame}
\begin{frame}
\frametitle{What Is State Verification?}
\Large
\begin{changemargin}{2cm}
\begin{enumerate}
\item Exercise SUT.
\item Verify state \& check return values.
\end{enumerate}
~\\[1em]
Inspect only outputs; \\ \hspace*{1cm} only call methods from SUT.\\[1em]
Do not instrument SUT.\\[1em]
Do not check interactions.
\end{changemargin}
\end{frame}
\begin{frame}
\frametitle{Implementing State Verification}
\Large
\begin{changemargin}{2cm}
Two options:
\begin{enumerate}
\item procedural (bunch of asserts); or,
\item via expected objects (stay tuned).
\end{enumerate}
\end{changemargin}
\end{frame}
\begin{frame}
\frametitle{Flight Example: discussing state verification}
\large
\begin{changemargin}{2cm}
We do check that the flight got removed.\\
We don't check that the removal got logged.\\[1em]
Hard to check state and observe logging.\\[1em]
Solution: Spy on SUT behaviour.
\end{changemargin}
\end{frame}
\begin{frame}
\frametitle{Flight example: procedural behaviour verification}
\small
\lstinputlisting{L30/flight_pbv.java}
\end{frame}
\begin{frame}
\frametitle{Alternative: Expected Behaviour Specification}
\Large
\begin{changemargin}{2cm}
Use a mock object framework (e.g. JMock)\\
to define expected behaviour.\\[1em]
Observe calls to the logger, \\
make sure right calls happen.
\end{changemargin}
\end{frame}
\begin{frame}
\frametitle{Kinds of Assertions}
\large
\begin{changemargin}{2cm}
Three built-in choices:
\begin{enumerate}
\item assertTrue(aBooleanExpression)
\item assertEquals(expected, actual)
\item assertEquals(expected, actual, tolerance)
\end{enumerate}
~\\[1em]
note: {\tt assertTrue} can give \\
hard-to-diagnose error messages \\
(must try harder when using).
\end{changemargin}
\end{frame}
\begin{frame}
\frametitle{Using Assertions}
\large
\begin{changemargin}{2cm}
Assertions are good:
\begin{itemize}
\item to check all things that should be true\\
\begin{changemargin}{1cm}
(more = better)
\end{changemargin}
\item to serve as documentation:
\begin{changemargin}{1cm}
when system in state $S_1$,\\
and I do $X$,\\
assert that the result should be $R$, and\\
that system should be in $S_2$.
\end{changemargin}
\item to allow failure diagnosis\\
\begin{changemargin}{1cm}
(include assertion messages!)
\end{changemargin}
\end{itemize}
\end{changemargin}
\end{frame}
\begin{frame}
\frametitle{Not Using Assertions}
\Large
\begin{changemargin}{2cm}
Can also do external result verification:\\[1em]
Write output to files, diff (or custom diff)
expected and actual output.\\[1em]
Twist: expected result then not visible when looking at test.\\[1em]
(What's a good workaround?)
\end{changemargin}
\end{frame}
\begin{frame}
\frametitle{Verifying Behaviour}
\Large
\begin{changemargin}{2cm}
Observe actions (calls) of the SUT.
\begin{itemize}
\item procedural behaviour verification; or,\\
(challenge: recording \& verifying behaviour)\\[.5em]
\item via expected behaviour specification.\\
(also captures outbound calls of SUT)
\end{itemize}
\end{changemargin}
\end{frame}
\begin{frame}
\frametitle{So far\ldots}
\Large
\begin{changemargin}{2cm}
Seen the basics of result verification.\\[1em]
Next: how to improve your tests!
\end{changemargin}
\end{frame}
\begin{frame}
\frametitle{Reducing Test Code Duplication}
\Large
\begin{changemargin}{2cm}
Usual cause: copy-pasta.\\[1em]
Mitigating duplication in result verification:
\begin{itemize}
\item Expected Objects
\item Custom Assertions
\item Verification Methods
\end{itemize}
\end{changemargin}
\end{frame}
\begin{frame}
\frametitle{Duplication-Prone Test Method}
\begin{changemargin}{1.5cm}
\large
One might expect many test methods like this one.
\end{changemargin}
\small
\lstinputlisting{L30/testInvoice.java}
\end{frame}
\begin{frame}
\frametitle{Using an Expected Object}
\begin{changemargin}{1.5cm}
\large
We can compare objects instead:
\end{changemargin}
\small
\begin{changemargin}{1cm}
\lstinputlisting{L30/testInvoice-equality.java}
\end{changemargin}
\begin{changemargin}{1.5cm}
\large
Need:
\begin{itemize}
\item a way to create the Expected Object;
\item a suitable {\tt equals()} method.
\end{itemize}
\end{changemargin}
\end{frame}
\begin{frame}
\frametitle{Potential Issues}
\begin{changemargin}{1.5cm}
Perhaps we:
\begin{itemize}
\item need special {\tt equals()} method, \\
e.g. to compare subset of fields; or,
\item may only have {\tt equals()} that checks identity; or,
\item can't create desired expected object.
\end{itemize}
~\\
Solutions:
\begin{itemize}
\item create custom assertion; or,
\item provide special {\tt equals()} on expected object.
\end{itemize}
\end{changemargin}
\end{frame}
\begin{frame}
\frametitle{Custom Assertions Example}
\begin{changemargin}{.5cm}
\small
\lstinputlisting{L30/testInvoice-custom-assertion.java}
\end{changemargin}
\begin{changemargin}{2cm}
\Large
Pick a good, declarative name.\\
Obtain by refactoring, \\ \hspace*{2cm} using usual techniques.
\end{changemargin}
\end{frame}
\begin{frame}
\frametitle{Benefits of Custom Assertions}
\begin{changemargin}{2cm}
\Large
\begin{itemize}
\item Hide irrelevant detail.
\item Label actions with a good name.
\item Are themselves testable.
\end{itemize}
\end{changemargin}
\end{frame}
\begin{frame}
\frametitle{Variant: Outcome-describing Verification Method}
\begin{changemargin}{.5cm}
\small
\lstinputlisting{L30/testInvoice-verification-method.java}
\end{changemargin}
\begin{changemargin}{2cm}
Differences: a verification method
\begin{itemize}
\item also interacts with SUT;
\item may have arbitrary parameters.
\end{itemize}
\end{changemargin}
\end{frame}
\begin{frame}
\frametitle{Going Further: Parameterized, Data-Driven Tests}
\begin{changemargin}{2cm}
\Large
While we're at it,\\
we can have entire tests \\
that differ only in input data.\\[1em]
Concrete tests invoke parametrized tests.
\end{changemargin}
\end{frame}
\begin{frame}[fragile]
\frametitle{Avoiding Logic in Tests}
\begin{changemargin}{2cm}
Problem: tests are untestable.\\
Including ifs and loops in tests = danger!\\
\end{changemargin}
{\small
\begin{lstlisting}
// BAD
List lineItems = invoice.getLineItems();
if (lineItems.size() == 1) {
// ...
} else {
fail("Invoice should have exactly 1 line item");
}
\end{lstlisting}
}
\begin{changemargin}{2cm}
Instead, do this:
\end{changemargin}
{\small
\begin{lstlisting}
// GOOD
List lineItems = invoice.getLineItems();
// (guard assertion:)
assertEquals("number of items", lineItems.size(), 1);
// ... proceed as before
\end{lstlisting}
}
\begin{changemargin}{2cm}
The guard keeps you out of trouble.
\end{changemargin}
\end{frame}
\begin{frame}
\frametitle{Loops}
\Large
\begin{changemargin}{2cm}
Don't put loops directly in tests.\\[1em]
Use a well-named, testable \\Test Utility Method instead.
\end{changemargin}
\end{frame}
\begin{frame}
\frametitle{Summary}
\Large
\begin{changemargin}{1cm}
Practical techniques for writing tests.\\[1em]
Today's focus: result verification.
\begin{itemize}
\item state verification
\item behaviour verification
\end{itemize}~\\
Also, techniques for improving your tests.
\begin{itemize}
\item reducing duplication
\item simplifying tests
\end{itemize}
\end{changemargin}
\end{frame}
\end{document}