Skip to content

Commit 8bdf061

Browse files
Jon Haitz Legarretathewtex
authored andcommitted
DOC: Add testing section to SW guide.
Add a section to describe the process and tools to write tests in ITK. Change-Id: I76cafa4b8a8e99c76aa0f09498604f7763a73f4f
1 parent 1f25ff8 commit 8bdf061

4 files changed

Lines changed: 125 additions & 15 deletions

File tree

SoftwareGuide/Art/Dashboard.jpg

-119 KB
Loading

SoftwareGuide/Latex/00-Preamble-Common.tex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
\usepackage[toc,page]{appendix}
99
\usepackage{verbatim}
1010
\usepackage{imakeidx}
11+
%\usepackage{siunitx}
1112
\usepackage[papersize={7.5in,9.25in},margin=1in]{geometry}
1213

1314
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

SoftwareGuide/Latex/DG04-CreateAModule.tex

Lines changed: 95 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,12 @@ \section{Tests}
259259
Regression tests for a module are placed in the \code{test} directory. This
260260
directory will contain a \texttt{CMakeLists.txt} with the CMake configuration,
261261
test sources, and optional \texttt{Input} and \texttt{Baseline} directories,
262-
which contain test input and baseline image datasets, respectively.
262+
which contain test input and baseline image datasets, respectively. Placement
263+
of the input and baseline image datasets within a given module directory is
264+
preferred over placement in the general \texttt{Testing/Data} directory; this
265+
ensures that a module's data is only downloaded when the module is enabled. An
266+
exception to this rule may be widely used input datasets, such as the
267+
\texttt{cthead1.png} image.
263268
264269
An example CMake configuration for a test directory is shown below.
265270
@@ -301,22 +306,100 @@ \section{Tests}
301306
around the CMake \code{add\_test} command that will resolve content links in
302307
the \code{DATA} macro. Testing data paths are given inside the \code{DATA}
303308
macro. Content link files, stored in the source code directory, are replaced
304-
by actual content files in the build directory when CMake downloads the target
305-
at build time. A content link file has the same name as its target, but a
306-
\texttt{.md5} extension is added, and the \texttt{.md5} file's contents are
307-
only the MD5SUM hash of its target. Content links for data files in a Git
308-
distributed version control repository prevent repository bloat. To obtain
309-
content links, register an account with the \texttt{ITK} community at
310-
\url{https://midas3.kitware.com} and request upload permissions on the ITK
311-
mailing list.
309+
by actual content files in the build directory when CMake downloads the
310+
\code{ITKData} target at build time. A content link file has the same name as
311+
its target, but a \texttt{.md5} extension is added, and the \texttt{.md5}
312+
file's contents are only the MD5SUM hash of its target. Content links for data
313+
files in a Git distributed version control repository prevent repository
314+
bloat. To obtain content links, register an account with the \texttt{ITK}
315+
community at \url{https://midas3.kitware.com} and request upload permissions
316+
on the ITK mailing list. The content links may also be created locally: once
317+
the actual file at issue has been placed in the corresponding directory, run
318+
CMake configuration, and CMake provides the desired \texttt{.md5} extension
319+
content link file.
320+
321+
When a test requires a new (or modified) input or baseline image dataset,
322+
the corresponding content link files have to be provided as well. Image
323+
datasets provided should be kept as small as possible. As a rule of thumb,
324+
their size should be under 50~\textit{kB}.
312325
313326
Test commands should call the test driver executable, followed by options for
314327
the test, followed by the test function name, followed by arguments that are
315-
passed to the test. The test driver accepts options like \texttt{--compare} to
316-
compare output images to baselines or options that modify tolerances on
317-
comparisons.
328+
passed to the test. The test driver accepts options like \texttt{--compare}
329+
(or \texttt{--compare-MD5} when using the MD5SUM hash) to compare output images
330+
to baselines or options that modify tolerances on comparisons. An exhaustive
331+
list of options is displayed in \texttt{itkTestDriverInclude.h}.
318332
333+
A few rules must be acknowledged to actually write a units test file
334+
\texttt{itk<ClassName>Test.cxx} for a given ITK class:
335+
\begin{enumerate}
336+
\item All class methods must be exercised.
337+
\item Test cases with values for member variables different from the default
338+
ones should be provided. The usefulness of this rule is especially manifest
339+
for boolean members, whose value usually determines whether a large portion
340+
of code is exercised or not.
341+
\item Test cases to reach the exception cases within the class should be
342+
provided.
343+
\item Regression tests must be included for methods returning a value.
344+
\item When a test detects a failure condition it must return the
345+
\code{EXIT\_FAILURE} value; if a test exits normally, it must return
346+
the \code{EXIT\_SUCCESS} value.
347+
\end{enumerate}
348+
349+
In any case, ITK provides with a number of classes and macros that ease the
350+
process of writing tests and checking the expected results. The following is an
351+
exhaustive list of such tools:
352+
\begin{itemize}
353+
\item \texttt{itkTestingMacros.h}: it contains a number of macros that allow
354+
testing of basic object properties:
355+
\begin{itemize}
356+
\item \code{EXERCISE\_BASIC\_OBJECT\_METHODS()}: verifies whether the class and
357+
superclass names provided match the RTTI, and exercises the \code{PrintSelf()}
358+
method. Since the \code{PrintSelf()} method prints all class member variables,
359+
this macro, when exercised, can identify uninitialized member variables.
360+
\item \code{TEST\_SET\_GET\_VALUE()}: once a member variable value has been set
361+
using the corresponding Set macro, this macro verifies that the value provided
362+
to the \code{Set()} method was effectively assigned to the member variable
363+
by comparing it to the value returned by the \code{Get()} value.
364+
\item \code{TEST\_SET\_GET\_BOOLEAN()}: exercises the \code{Set()/Get()},
365+
and \code{On()/Off()} methods of class applied to a boolean member variable.
366+
\end{itemize}
367+
\item \code{TRY\_EXPECT\_NO\_EXCEPTION()}: exercises a method which is expected to
368+
return with no errors. It is only required for methods that are known to throw
369+
exceptions, such as I/O operations, filter updates, etc.
370+
\item \code{TRY\_EXPECT\_EXCEPTION()}: exercises a method in the hope of detecting
371+
an exception. This macro allows a test to continue its execution when setting
372+
test cases bound to hit a class' exception cases. It is only required for
373+
methods that are known to throw exceptions, such as I/O operations, filter
374+
updates, etc.
375+
\item \code{itkMath.h}: contains a series of static methods used for basic
376+
type comparison. Methods are available to perform fuzzy floating point equality
377+
comparison, e.g. \code{itk::Math::FloatAlmostEquals()}, to handle expected
378+
cross-platform differences.
379+
\end{itemize}
319380
381+
A test may have some input arguments. When a test does not need any input
382+
argument (e.g., it generates a synthetic input image), the \code{main}
383+
argument names may either be omitted (\code{int itk<ClassName>Test( int,
384+
char* [] )}), or the \code{itkNotUsed }macro can be used (\code{int
385+
itk<ClassName>Test( int itkNotUsed( argc ), char *itkNotUsed( argv ) [] )}),
386+
to avoid compiler warnings about unused variables.
387+
388+
The number of input arguments provided must be checked at the beginning of the
389+
test. If a test requires a fixed number of input arguments, then the argument
390+
number check should verify the exact number of arguments.
391+
392+
It is essential that a test is made quantitative, i.e., the methods' returned
393+
values and the test's output must be compared to a known ground-truth. As
394+
mentioned, ITK contains a series of methods to compare basic types. ITK also
395+
provide a powerful regression tool for a test that checks the validity of a
396+
process over an image, which is the most common case in ITK. To this end, the
397+
test is expected to write its output to a file. The first time the test is run,
398+
the output is expected to be manually placed within the test module's
399+
\texttt{Baseline} folder. Hence, when CTest is executed, the distance between
400+
the test's output and the expected output (i.e., the baseline) is computed. If
401+
the distance is below a configurable tolerance, the regression test is marked as
402+
a success.
320403
321404
\section{Wrapping}
322405
\label{sec:ModuleWrapping}

SoftwareGuide/Latex/DG05-SoftwareProcess.tex

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,9 @@ \section{CDash Regression Testing System}
111111
Rational. (Other memory checking programs will be added in the future.)
112112

113113
\item[PrintSelf.] All classes in ITK are expected to print out all
114-
their instance variables (i.e., those with associated Set and Get
115-
methods) correctly. This test checks to make sure
116-
that this is the case.
114+
their instance (i.e., those with associated Set and Get
115+
methods) and their internal variables correctly. This test checks to
116+
make sure that this is the case.
117117

118118
\item[Unit.] Each class in ITK should have a corresponding unit test
119119
where the class functionalities are exercised and quantitatively
@@ -153,6 +153,32 @@ \section{CDash Regression Testing System}
153153
the dashboard. This is useful for tracking problems and keeping up to date
154154
with new additions to ITK.
155155

156+
\subsection{Developing tests}
157+
\label{subsec:developingTests}
158+
159+
As highlighted, testing is an essential part of ITK. Regression testing on a
160+
regular basis allows ITK to meet high code quality standards, and to enable
161+
reproducible research. Code coverage reported daily in CDash allows us to
162+
systematically measure the degree to which the ITK source code is reliable.
163+
Therefore, writing tests, and improving current tests and the testing
164+
infrastructure is crucial to ITK.
165+
166+
There are a number of scenarios when writing tests:
167+
\begin{itemize}
168+
\item \textbf{Modifying existing classes}. When modifying an existing class
169+
(either due to a bug or a performance improvement), it must be checked that
170+
existing tests exercise the new code. Otherwise, either the existing tests
171+
should be modified to include the appropriate cases for the new code to be
172+
exercised (without harm to existing cases), or a new test file may be required.
173+
\item \textbf{Contributing new classes}. When contributing a new class, a unit
174+
test or a few unit tests should be provided to check that the class is working
175+
as expected. The unit tests are expected to exercise all of the members of the
176+
new class.
177+
\end{itemize}
178+
179+
In either case, the tips and tools described in Section~\ref{sec:Tests} were
180+
developed to improve and facilitate the process.
181+
156182
\section{Working The Process}
157183
\label{sec:WorkingTheProcess}
158184

0 commit comments

Comments
 (0)