@@ -259,7 +259,12 @@ \section{Tests}
259259Regression tests for a module are placed in the \code {test} directory. This
260260directory will contain a \texttt {CMakeLists.txt } with the CMake configuration,
261261test 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
264269An example CMake configuration for a test directory is shown below.
265270
@@ -301,22 +306,100 @@ \section{Tests}
301306around the CMake \code {add\_ test} command that will resolve content links in
302307the \code {DATA} macro. Testing data paths are given inside the \code {DATA}
303308macro. 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
313326Test commands should call the test driver executable, followed by options for
314327the 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 }
0 commit comments