Skip to content

ENH: Add GoogleTests covering ITK exception classes - #6956

Merged
hjmjohnson merged 1 commit into
InsightSoftwareConsortium:mainfrom
hjmjohnson:enh-exception-class-gtests
Oct 23, 2026
Merged

hjmjohnson merged 1 commit into
InsightSoftwareConsortium:mainfrom
hjmjohnson:enh-exception-class-gtests

Conversation

@hjmjohnson

@hjmjohnson hjmjohnson commented Oct 20, 2026 •

Copy link
Copy Markdown
Member

Adds GoogleTests that exercise ITK's exception classes directly, so a test of a filter that throws only needs to check the throw, not print the exception for coverage. Run alone, the exception tests cover 100% of the exception-class lines (196 of 196), up from 71% today.

What is covered
File Classes
Core/Common/test/itkCommonExceptionsGTest.cxx MemoryAllocationError, RangeError, InvalidArgumentError, IncompatibleOperandsError, DerefError, ProcessAborted, DataObjectError, InvalidRequestedRegionError
IO/ImageBase/test/itkImageIOExceptionsGTest.cxx ImageFileReaderException, ImageFileWriterException, ImageSeriesWriterException
IO/MeshBase/test/itkMeshIOExceptionsGTest.cxx MeshFileReaderException, MeshFileWriterException
Numerics/Statistics/test/itkSampleToHistogramFilterExceptionsGTest.cxx the seven itkDeclareExceptionMacro exceptions in itkSampleToHistogramFilter.h

Each class is checked for its name, construction (default and with file, line, description, and location), Print output, copy and move, and use through a base-class reference. Throwability is checked at compile time (static_assert on the base class and on non-throwing copy), not by throwing in the test. DataObjectError and InvalidRequestedRegionError are printed both without a data object ("(None)") and with an image attached, which also checks that the image's PrintSelf output appears. IOMeshBase gets its first GoogleTest driver.

Why

Most exception-class code was covered as a side effect of filter tests printing the exceptions they caught. DataObjectError::Print, for example, was reached only through the ImageCompare, ImageCompose, and ImageGrid test drivers. Tests dedicated to the exceptions keep that coverage when filter tests use plain EXPECT_THROW.

Verification
  • LLVM source-based coverage build (macOS, Apple clang) of ITKCommon, ITKIOImageBase, ITKIOMeshBase, and ITKStatistics; only ExceptionObject.*, the four new suites, and itkExceptionObjectTest run:

    Exception-class lines covered
    main 137 / 192 (71.4%)
    this PR 196 / 196 (100%)
  • All GoogleTests in the four drivers pass under CTest, and the build has no warnings.

  • pre-commit run --all-files passes.

AI assistance

An AI coding assistant wrote the tests and ran the coverage measurement under the author's direction.

@github-actions github-actions Bot added type:Infrastructure Infrastructure/ecosystem related changes, such as CMake or buildbots type:Enhancement Improvement of existing methods or implementation type:Testing Ensure that the purpose of a class is met/the results on a wide set of test cases are correct area:Core Issues affecting the Core module area:IO Issues affecting the IO module area:Numerics Issues affecting the Numerics module labels Oct 20, 2026
@hjmjohnson
hjmjohnson marked this pull request as ready for review October 20, 2026 14:48
@greptile-apps

greptile-apps Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Low impact] This test-only change appears safe to merge.

Summary

Adds 20 focused GoogleTests for exception classes across four modules.

  • Common exceptions now have direct tests for their reported details.
  • Image IO exceptions now have direct tests for their messages and locations.
  • Mesh IO exceptions now have direct tests in a GoogleTest driver.
  • Histogram exceptions now have direct tests for their names and messages.

No actionable issues found. Tests were not run during this review.

Reviews (1) · Last reviewed commit: "ENH: Add GoogleTests covering ITK except..." · Reviewed by Greptile

Comment thread Modules/Core/Common/test/itkCommonExceptionsGTest.cxx Outdated
@hjmjohnson
hjmjohnson force-pushed the enh-exception-class-gtests branch from ebad435 to f62b3ff Compare October 20, 2026 20:33
@hjmjohnson
hjmjohnson requested a review from N-Dekker October 21, 2026 23:53
@hjmjohnson
hjmjohnson requested a review from dzenanz October 22, 2026 12:14
Comment thread Modules/Core/Common/test/itkCommonExceptionsGTest.cxx Outdated
Comment thread Modules/Core/Common/test/itkCommonExceptionsGTest.cxx Outdated
@dzenanz

dzenanz commented Oct 22, 2026

Copy link
Copy Markdown
Member

Niels has reviewed this PR. Philosophically, I am in favor. I prefer to skip reviewing it in detail.

@N-Dekker

Copy link
Copy Markdown
Contributor

Niels has reviewed this PR.

Sorry, @dzenanz, I did not review all 500+ added lines of code so far. I just did some cherry-picking on low hanging fruit. Although I do agree philosophically 😸

@hjmjohnson
hjmjohnson force-pushed the enh-exception-class-gtests branch 2 times, most recently from b05d135 to 0418064 Compare October 23, 2026 12:25
@hjmjohnson

Copy link
Copy Markdown
Member Author

Two force-pushes: f62b3ff → b05d135 is a plain rebase on main; b05d135 → 0418064 holds only the review fixes.

@hjmjohnson
hjmjohnson force-pushed the enh-exception-class-gtests branch from 0418064 to 7e99108 Compare October 23, 2026 13:21
@hjmjohnson

Copy link
Copy Markdown
Member Author

One more content-only push, 0418064 → 7e99108: the seven EXPECT_TRUE(a == b) checks are now EXPECT_EQ(a, b), so a failure prints both exceptions.

EXPECT_EQ(std::as_const(defaultConstructed).GetDataObject(), nullptr);

const std::string printedWithoutDataObject = PrintToString(defaultConstructed);
EXPECT_TRUE(Contains(printedWithoutDataObject, "itk::" + expectedName));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a future improvement, I think it would be nice if we could use GoogleTest Matchers like HasSubstr, instead of using our own local Contains helper function:

EXPECT_THAT(printedWithoutDataObject, HasSubstr("itk::" + expectedName));

But for now, EXPECT_TRUE(Contains(... is fine to me. 👍

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed that matchers would be nicer. They need googlemock, though, and ITK neither vendors it nor builds it: UpdateFromUpstream.sh imports only googletest/, BUILD_GMOCK is forced OFF, and the drivers link only GTest::gtest. So I am keeping Contains here and treating matchers as out of scope until a larger investigation of using gMock in ITK is done: its benefit to the tests, the cost of vendoring it, and the system-GoogleTest builds. I will bring that to Discourse before proposing a change.

Comment thread Modules/Numerics/Statistics/test/itkSampleToHistogramFilterExceptionsGTest.cxx Outdated
Comment thread Modules/Core/Common/test/itkCommonExceptionsGTest.cxx Outdated

@N-Dekker N-Dekker left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Hans! Approved 👍

Exception-class behavior was tested mostly as a side effect of filter
tests printing the exceptions they caught; DataObjectError::Print, for
example, was reached only through three filter test drivers. These tests
exercise each exception class directly (construction, accessors, Print,
copy and move, and use through a base-class reference), so a test that only
checks a throw does not need to print the exception for coverage.

Covers the Common exceptions (including DataObjectError and
InvalidRequestedRegionError with and without a data object), the image
and mesh IO exceptions, and the SampleToHistogramFilter exceptions.
IOMeshBase gets its first GoogleTest driver.
@hjmjohnson
hjmjohnson force-pushed the enh-exception-class-gtests branch from 7e99108 to 324ca0c Compare October 23, 2026 14:22
@hjmjohnson

Copy link
Copy Markdown
Member Author

Content-only push after your approval, 7e99108 → 324ca0c: TParent is renamed to TExpectedBaseClass, and single-call TESTs are merged into one TEST per helper. No checks changed.

@hjmjohnson
hjmjohnson merged commit 383375e into InsightSoftwareConsortium:main Oct 23, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:Core Issues affecting the Core module area:IO Issues affecting the IO module area:Numerics Issues affecting the Numerics module type:Enhancement Improvement of existing methods or implementation type:Infrastructure Infrastructure/ecosystem related changes, such as CMake or buildbots type:Testing Ensure that the purpose of a class is met/the results on a wide set of test cases are correct

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants