Doc: Enable CI testing of Python API examples #13055
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Enables CI testing of Python API examples, using pytest's built-in doctest integration.
To avoid polluting the examples with a bunch of setup code or complicated asserts, it relies on a two hacks in
conftest.py
.Hack 1: We break into the internals of pytest to add custom result matching code. This lets us use human-readable comments in the expected output to control how the matching is done. Some situations where this is needed:
doctest
and therefore causes output matching to fail. Keeping these outputs in the documentation is important in cases likeFeature.DumpReadable
. The# no-check
comment causes the matcher to ignore these.# rtol: 1e-4
syntax allows this.# random
syntax allows this.The code that handles these comments may need to be made more robust in the future, but it's good enough for the API examples we have so far.
pytest-doctestplus https://github.com/scientific-python/pytest-doctestplus and https://github.com/scipy/scipy_doctest both make some efforts along the same lines but neither one covers all of these cases, and I think the hack is preferable to contorting the examples.
Hack 2: To avoid including lengthy paths to input files (
byte.tif
, not../../autotest/gcore/data/byte.tif
), we keep a list of the input test files and their locations, then monkey-patch GDAL functions that might try to access these files (gdal.Open
, etc.) and do a find-and-replace of their arguments to substitute the full paths. The alternative would be to use the.. testsetup::
and.. testcleanup::
directives to modify working directories before and after each test, but this seems tedious.This PR adds a 169kb test data file. If there's already a file in the repo that uses a RAT I could rewrite the RAT tests to avoid that. I could also make a new, smaller subset of the same data.
What are related issues/pull requests?
#9949
Tasklist