Skip to content
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

pytest: Allow default test file naming patterns inside tests folders #4784

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

echoix
Copy link
Member

@echoix echoix commented Nov 28, 2024

To help out #4755 and also myself, that I worked-around in #4700 by renaming the file (p.s.: still waiting for feedback on the expected behaviour there)

This PR allows to use file names like shown in pytest docs here
https://docs.pytest.org/en/latest/reference/reference.html#confval-python_files, as long as it is inside a tests folder instead of anywhere. The only expected file name pattern currently is not as natural to me, requiring to end with _test.py.

@github-actions github-actions bot added the Python Related code is in Python label Nov 28, 2024
@echoix
Copy link
Member Author

echoix commented Nov 28, 2024

Oh, there are two tests that fail, that weren't collected before due to the expected behaviour not being the one implemented.

@echoix
Copy link
Member Author

echoix commented Dec 3, 2024

I found the reason why the test added in #3582, doesn't work:

from grass.script.task import grassTask
def test_mapcalc_simple_e_name():
gt = grassTask("r.mapcalc.simple")
assert gt.get_param("e")["name"] == "e"
def test_mapcalc_simple_expession_name():
gt = grassTask("r.mapcalc.simple")
assert gt.get_param("expression")["name"] == "expression"

It works when pytest is run inside a grass shell, where GISBASE, GISRC are set, and the modules, like "r.mapcalc.simple" are in path (the bin and scripts subfolders of the install location are in path).

So, I used a fixture that creates a temporary session like used in other pytest tests, but it wasn't enough, as grassTask doesn't take the "env" argument. For this, I used monkeypatch to set the env vars and undo once done: https://docs.pytest.org/en/latest/how-to/monkeypatch.html#monkeypatching-environment-variables

That makes it working again (for the first time in CI), as it wasn't run.

To ensure that modules in both the script and bin folders are usable, I added two other tests. The d.vect one is taken from one of the few usages of grassTask in the source code, in the gui.

@echoix echoix force-pushed the pytest-file-patterns branch from 3ddb862 to 4e18165 Compare December 3, 2024 02:36
@echoix echoix force-pushed the pytest-file-patterns branch from 4e18165 to 7f8a8eb Compare December 3, 2024 02:39
@echoix
Copy link
Member Author

echoix commented Dec 3, 2024

Only the GISBASE, GISRC and PATH were needed to monkeypatch to get these tests working. But, to keep it general and easier to use elsewhere, I kept the full loop, even if almost nothing was different before and inside the session (except PATH, obviously, but also LD_LIBRARY_PATH).

@echoix
Copy link
Member Author

echoix commented Dec 3, 2024

@Shreshth-Malik Take a look at the workaround I used here. Maybe it could be useful for you too if you got functions that don't work with the env from a SessionHandle.

@echoix echoix enabled auto-merge (squash) December 3, 2024 02:46
@echoix echoix requested a review from wenzeslaus December 3, 2024 02:46
@Shreshth-Malik
Copy link
Contributor

@echoix Thank you for this. I will definitely take a look and try to put it in my tests

@echoix
Copy link
Member Author

echoix commented Dec 4, 2024

Can someone take a quick look at this? The original intent of the PR was to have pytest run on tests for (new) users following the pytest docs. I have asked someone else to do the change, that I would have approved immediately, but there were some new tests failures that needed to be solved. So I did them myself, so I need to be approved by someone else. I would've approved this PR if it was somebody else that did it. To help out keep the trace of what was done, I document above in the comment the thoughts and the solution. That's what is important. There is one person that is more knowledgeable on this subject precise subject, but we cannot rely on only one person to be always available, effectively having a bus factor of 1 on reviewing testing infrastructure.

With the documentation above, I still recommend merging this even if not scrutated as much, so PRs of other new users can go through, and let them work on new tests. It fixes a real overlook of previous PRs that were merged without having their tests actually run, thus not failing. (4 new test files are running now as shown with code coverage, one of which was failing and has been added with tests of what I observed was failing when debugging)

Co-authored-by: Markus Neteler <[email protected]>
@echoix echoix disabled auto-merge December 5, 2024 00:21
@echoix echoix requested review from neteler and pesekon2 December 6, 2024 20:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libraries Python Related code is in Python
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants