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