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

module 'pytest_cases' has no attribute 'CasesCollectionWarning' when running with xdist enabled #249

Closed
lilo-d opened this issue Jan 24, 2022 · 3 comments · Fixed by #267

Comments

@lilo-d
Copy link

lilo-d commented Jan 24, 2022

Hi,

started integrating pytest_cases in our unit testing suite (already did before in our integration testing suite), but I'm facing an issue at collection when running with xdist:

pytest -c ../pytest.ini -n 1 --rootdir=.

[Running Unit Tests]
=========================================================================================== test session starts ============================================================================================
platform darwin -- Python 3.8.12, pytest-6.1.2, py-1.10.0, pluggy-0.13.1
Test order randomisation NOT enabled. Enable with --random-order or --random-order-bucket=<bucket_type>
rootdir: /Users/lievenvanacker/Deliverect/backend/Tests/unit, configfile: ../pytest.ini
plugins: freezegun-0.4.2, redis-2.0.0, random-order-1.0.4, celery-4.4.7, xdist-2.3.0, anyio-3.4.0, mock-3.3.1, cov-2.10.1, asyncio-0.14.0, cases-3.6.8, mongodb-2.2.0, forked-1.3.0
gw0 ok!!!!!!!!!!!!!!!!!!!! <ExceptionInfo AttributeError("module 'pytest_cases' has no attribute 'CasesCollectionWarning'") tblen=2>
gw0 [4764]
INTERNALERROR> Traceback (most recent call last):
INTERNALERROR>   File "/Users/lievenvanacker/Deliverect/backend/venv/lib/python3.8/site-packages/xdist/workermanage.py", line 367, in process_from_remote
INTERNALERROR>     warning_message = unserialize_warning_message(
INTERNALERROR>   File "/Users/lievenvanacker/Deliverect/backend/venv/lib/python3.8/site-packages/xdist/workermanage.py", line 398, in unserialize_warning_message
INTERNALERROR>     cls = getattr(mod, data["message_class_name"])
INTERNALERROR> AttributeError: module 'pytest_cases' has no attribute 'CasesCollectionWarning'
[gw0] node down: <ExceptionInfo AttributeError("module 'pytest_cases' has no attribute 'CasesCollectionWarning'") tblen=2>
F
replacing crashed worker gw0
gw1 C     .INTERNALERROR> Traceback (most recent call last):
INTERNALERROR>   File "/Users/lievenvanacker/Deliverect/backend/venv/lib/python3.8/site-packages/_pytest/main.py", line 257, in wrap_session
INTERNALERROR>     session.exitstatus = doit(config, session) or 0
INTERNALERROR>   File "/Users/lievenvanacker/Deliverect/backend/venv/lib/python3.8/site-packages/_pytest/main.py", line 313, in _main
INTERNALERROR>     config.hook.pytest_runtestloop(session=session)
INTERNALERROR>   File "/Users/lievenvanacker/Deliverect/backend/venv/lib/python3.8/site-packages/pluggy/hooks.py", line 286, in __call__
INTERNALERROR>     return self._hookexec(self, self.get_hookimpls(), kwargs)
INTERNALERROR>   File "/Users/lievenvanacker/Deliverect/backend/venv/lib/python3.8/site-packages/pluggy/manager.py", line 93, in _hookexec
INTERNALERROR>     return self._inner_hookexec(hook, methods, kwargs)
INTERNALERROR>   File "/Users/lievenvanacker/Deliverect/backend/venv/lib/python3.8/site-packages/pluggy/manager.py", line 84, in <lambda>
INTERNALERROR>     self._inner_hookexec = lambda hook, methods, kwargs: hook.multicall(
INTERNALERROR>   File "/Users/lievenvanacker/Deliverect/backend/venv/lib/python3.8/site-packages/pluggy/callers.py", line 208, in _multicall
INTERNALERROR>     return outcome.get_result()
INTERNALERROR>   File "/Users/lievenvanacker/Deliverect/backend/venv/lib/python3.8/site-packages/pluggy/callers.py", line 80, in get_result
INTERNALERROR>     raise ex[1].with_traceback(ex[2])
INTERNALERROR>   File "/Users/lievenvanacker/Deliverect/backend/venv/lib/python3.8/site-packages/pluggy/callers.py", line 187, in _multicall
INTERNALERROR>     res = hook_impl.function(*args)
INTERNALERROR>   File "/Users/lievenvanacker/Deliverect/backend/venv/lib/python3.8/site-packages/xdist/dsession.py", line 112, in pytest_runtestloop
INTERNALERROR>     self.loop_once()
INTERNALERROR>   File "/Users/lievenvanacker/Deliverect/backend/venv/lib/python3.8/site-packages/xdist/dsession.py", line 135, in loop_once
INTERNALERROR>     call(**kwargs)
INTERNALERROR>   File "/Users/lievenvanacker/Deliverect/backend/venv/lib/python3.8/site-packages/xdist/dsession.py", line 278, in worker_runtest_protocol_complete
INTERNALERROR>     self.sched.mark_test_complete(node, item_index, duration)
INTERNALERROR>   File "/Users/lievenvanacker/Deliverect/backend/venv/lib/python3.8/site-packages/xdist/scheduler/load.py", line 151, in mark_test_complete
INTERNALERROR>     self.node2pending[node].remove(item_index)
INTERNALERROR> KeyError: <WorkerController gw0>
!!!!!!!!!!!!!!!!!!!! <ExceptionInfo AttributeError("module 'pytest_cases' has no attribute 'CasesCollectionWarning'") tblen=2>
INTERNALERROR> Traceback (most recent call last):
INTERNALERROR>   File "/Users/lievenvanacker/Deliverect/backend/venv/lib/python3.8/site-packages/xdist/workermanage.py", line 367, in process_from_remote
INTERNALERROR>     warning_message = unserialize_warning_message(
INTERNALERROR>   File "/Users/lievenvanacker/Deliverect/backend/venv/lib/python3.8/site-packages/xdist/workermanage.py", line 398, in unserialize_warning_message
INTERNALERROR>     cls = getattr(mod, data["message_class_name"])
INTERNALERROR> AttributeError: module 'pytest_cases' has no attribute 'CasesCollectionWarning'

Note that running without xdist works fine:

pytest -c ../pytest.ini --rootdir=.

Not sure if this is our setup or a general issue? I could help to further debug if needed.

Thanks, Lieven

@lilo-d lilo-d changed the title module 'pytest_cases' has no attribute 'CasesCollectionWarning' module 'pytest_cases' has no attribute 'CasesCollectionWarning' when running with xdist enabled Jan 24, 2022
@smarie
Copy link
Owner

smarie commented Jan 25, 2022

It seems from the traceback that the issue comes from xdist in unserializing the warning message:

xdist/workermanage.py (unserialize_warning_message) seems to expect that the warning class is always a top-level attribute of the main package: cls = getattr(mod, data["message_class_name"])

I found a similar issue in pytest-xdist: pytest-dev/pytest-xdist#404
I asked them for guidance

@smarie
Copy link
Owner

smarie commented Mar 21, 2022

I found the culprit:

class CasesCollectionWarning(UserWarning):
"""
Warning emitted when pytest cases is not able to collect a file or symbol in a module.
"""
__module__ = "pytest_cases"

The issue is that I set __module__ myself so that the warning message looks nice... However then the __init__ should contain the class for consistency.

@smarie
Copy link
Owner

smarie commented Mar 21, 2022

@lilo-d please note that the CasesCollectionWarning means that there is at least one class containing cases, that is ignored in your tests because it contains __init__ or __new__. So, probably something to check on your side as well (even when the bug is fixed)

smarie pushed a commit that referenced this issue Mar 21, 2022
…CollectionWarning' when running `pytest-xdist` and at least one cases class is ignored because of `__init__` or `__new__`. Fixed #249
smarie added a commit that referenced this issue Mar 21, 2022
…CollectionWarning' when running `pytest-xdist` and at least one cases class is ignored because of `__init__` or `__new__`. Fixed #249 (#267)

Co-authored-by: Sylvain MARIE <sylvain.marie@se.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants