Skip to content

tests: Fix collection errors when importing tests have side effects #5541

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 19, 2025

Conversation

echoix
Copy link
Member

@echoix echoix commented Apr 13, 2025

Some test files do some actions, like set up steps outside the set up functions. So, simply importing them for collection, even with pure unittest python -m unittest discover, would have side effects and possibly fail.

With the following change in pyproject.toml for allowing to discover gunittest tests with pytest:

-python_files = "*/tests/*_test.py */tests/test_*.py"
+python_files = """
+    */tests/*_test.py
+    */tests/test_*.py
+    **/testsuite/*test*.py
+"""

These errors are now pointed out.
This PR fixes these collection errors, so at least pytest, if configured correctly to be able to find and import grass from python, doesn't fail anymore.

@github-actions github-actions bot added raster Related to raster data processing temporal Related to temporal data processing Python Related code is in Python libraries module tests Related to Test Suite labels Apr 13, 2025
@echoix
Copy link
Member Author

echoix commented Apr 13, 2025

I read a bit too fast what the lines were doing for one of these test files. They don't actually create the files, only the file names. So, using gs.try_remove instead, as one of them would be created per test, not all of them.

gs.try_remove simply ignores errors if the call fails:

def try_remove(path: StrOrBytesPath) -> None:
    """Attempt to remove a file; no exception is generated if the
    attempt fails.

    :param str path: path to file to remove
    """
    try:
        os.remove(path)
    except Exception:
        pass

@echoix echoix merged commit 24d7441 into OSGeo:main Apr 19, 2025
28 checks passed
@echoix echoix deleted the gunittest-pytest-collection-errors branch April 19, 2025 21:44
@github-actions github-actions bot added this to the 8.5.0 milestone Apr 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libraries module Python Related code is in Python raster Related to raster data processing temporal Related to temporal data processing tests Related to Test Suite
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants