vxgmichel / aiostream

Generator-based operators for asynchronous iteration
http://aiostream.readthedocs.io
GNU General Public License v3.0
797 stars 34 forks source link

Fix pytest addopts parameter #115

Closed penguinpee closed 4 months ago

penguinpee commented 4 months ago

Tests are located in tests/ not aiostream/.

vxgmichel commented 4 months ago

Tests are located in tests but the coverage tool should target aiostream. This config is fine in my opinion, as you can see from the CI: https://github.com/vxgmichel/aiostream/actions/runs/9544020517/job/26301796421#step:5:1

Did you notice any issue with pytest?

penguinpee commented 4 months ago

Did you notice any issue with pytest?

Yes. When running a plain pytest -v from the project dir it results in:

============================= test session starts ==============================
platform linux -- Python 3.13.0b2, pytest-7.4.3, pluggy-1.3.0 -- /usr/bin/python3
cachedir: .pytest_cache
rootdir: /builddir/build/BUILD/python-aiostream-0.6.1-build/aiostream-0.6.1
configfile: pyproject.toml
plugins: asyncio-0.23.6
asyncio: mode=Mode.STRICT
collecting ... collected 0 items

============================ no tests ran in 0.06s =============================

Note, in Fedora we are not interested in coverage. So, the only change made leading to above result is patching out --cov from addopts in pyproject.toml.

If I leave pyproject.toml as is and install pytest-cov it results in:

============================= test session starts ==============================
platform linux -- Python 3.13.0b2, pytest-7.4.3, pluggy-1.3.0 -- /usr/bin/python3
cachedir: .pytest_cache
rootdir: /builddir/build/BUILD/python-aiostream-0.6.1-build/aiostream-0.6.1
configfile: pyproject.toml
plugins: cov-4.0.0, asyncio-0.23.6
asyncio: mode=Mode.STRICT
collecting ... /usr/lib64/python3.13/site-packages/coverage/control.py:883: CoverageWarning: No data was collected. (no-data-collected)
  self._warn("No data was collected.", slug="no-data-collected")
collected 104 items / 1 error

==================================== ERRORS ====================================
___________________ ERROR collecting aiostream/test_utils.py ___________________
import file mismatch:
imported module 'aiostream.test_utils' has this __file__ attribute:
  /builddir/build/BUILD/python-aiostream-0.6.1-build/BUILDROOT/usr/lib/python3.13/site-packages/aiostream/test_utils.py
which is not the same as the test file we want to collect:
  /builddir/build/BUILD/python-aiostream-0.6.1-build/aiostream-0.6.1/aiostream/test_utils.py
HINT: remove __pycache__ / .pyc files and/or use a unique basename for your test file modules

----------- coverage: platform linux, python 3.13.0-beta-2 -----------
Name                            Stmts   Miss  Cover
---------------------------------------------------
aiostream/__init__.py               5      5     0%
aiostream/aiter_utils.py          109    109     0%
aiostream/core.py                 254    254     0%
aiostream/manager.py               91     91     0%
aiostream/pipe.py                  36     36     0%
aiostream/stream/__init__.py        8      8     0%
aiostream/stream/advanced.py       65     65     0%
aiostream/stream/aggregate.py      40     40     0%
aiostream/stream/combine.py        88     88     0%
aiostream/stream/create.py         79     79     0%
aiostream/stream/misc.py           27     27     0%
aiostream/stream/select.py        147    147     0%
aiostream/stream/time.py           33     33     0%
aiostream/stream/transform.py      49     49     0%
aiostream/test_utils.py           115    115     0%
---------------------------------------------------
TOTAL                            1146   1146     0%

=========================== short test summary info ============================
ERROR aiostream/test_utils.py
!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
=============================== 1 error in 0.56s ===============================

I'm not sure why it works for you in CI. Does it break, though, with the patch applied? With coverage enabled and tests passed to pytest, I see the same error as above with unpatched pyproject.toml.

vxgmichel commented 4 months ago

So, the only change made leading to above result is patching out --cov from addopts in pyproject.toml.

If you patch out --cov you have to remove aiostream as well since it is the argument to the --cov option. See the help message:

$ pytest --help
...
coverage reporting with distributed testing support:
  --cov=[SOURCE]        Path or package name to measure during execution (multi-allowed). Use --cov= to not do any source filtering and record
                        everything.

So removing --cov aiostream should work for you. Let me know how it goes :)

penguinpee commented 4 months ago

If you patch out --cov you have to remove aiostream as well since it is the argument to the --cov option.

Ah, my bad. I read that as a positional argument to pytest. My lack of knowledge regarding coverage tools shows. :blush:

So removing --cov aiostream should work for you.

That alone is not sufficient.

After some more fiddling, it appears adding --import-mode importlib to pytest solves the file mismatch error in both cases, with and without coverage.

I'm sure I've tried that before. But chances are high something else was not quite right, when I tried first.

Thanks for the pointer nonetheless.

penguinpee commented 4 months ago

I think the reason --import-mode is not required for CI is the editable install. When I do that in a venv, followed by pytest, I do not need to pass additional parameters.

I'm closing this. All good. Sorry for the noise.

vxgmichel commented 4 months ago

Oh I did not notice this:

import file mismatch:
imported module 'aiostream.test_utils' has this __file__ attribute:
[...]/usr/lib/python3.13/site-packages/aiostream/test_utils.py
which is not the same as the test file we want to collect:
[...]/BUILD/python-aiostream-0.6.1-build/aiostream-0.6.1/aiostream/test_utils.py

It looks like aiostream/test_utils.py is mistaken for a test file. I'll see what I can do to fix that.

All good. Sorry for the noise.

On the contrary, thanks for letting me know that the tests do not run with a non-editable install of aiostream :)

penguinpee commented 4 months ago

Maybe you need to define testpaths? I just saw it in another pyproject.toml. I haven't looked into what its impact is.

testpaths = [
    "tests",
]

EDIT: Part of the tool.pytest.ini_options table, naturally.

penguinpee commented 4 months ago

The documentation confirms, this will instruct pytest to search only in listed paths for tests.

vxgmichel commented 4 months ago

Maybe you need to define testpaths?

Great idea, done in PR #119 :tada: