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 plugin runs even when irrelevant #703

Closed
jayvdb opened this issue Sep 13, 2020 · 5 comments · Fixed by #727
Closed

pytest plugin runs even when irrelevant #703

jayvdb opened this issue Sep 13, 2020 · 5 comments · Fixed by #727

Comments

@jayvdb
Copy link

jayvdb commented Sep 13, 2020

Describe the bug
pytest plugin runs even when irrelevant.

pytest --help
..
  File "/usr/lib/python3.8/site-packages/nameko/testing/pytest.py", line 80, in pytest_load_initial_conftests
    eventlet.monkey_patch()
  File "/usr/lib/python3.8/site-packages/eventlet/patcher.py", line 271, in monkey_patch
    _green_existing_locks()
  File "/usr/lib/python3.8/site-packages/eventlet/patcher.py", line 362, in _green_existing_locks
    if isinstance(obj, rlock_type):
  File "/usr/lib/python3.8/site-packages/django/utils/functional.py", line 224, in inner
    self._setup()
  File "/usr/lib/python3.8/site-packages/django/conf/__init__.py", line 57, in _setup
    raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Requested settings, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

To Reproduce

  1. Install nameko
  2. Go to a directory unrelated to nameko
  3. run pytest --help

Expected behavior
nameko plugin should expose its options, but otherwise do nothing.

Environment (please complete the following information):

  • Nameko version: 2.13.0
  • Python version: 3.8
  • OS: Linux
@palfrey
Copy link

palfrey commented Dec 11, 2020

Adding addopts = -p no:nameko to my pytest.ini at least worked around this

@mattbennett
Copy link
Member

Thanks for reporting this and sorry it's been a couple of months without a response.

The bit that's screwing you up here is the eventlet.monkey_patch in the pytest_load_initial_conftests hook of Nameko's pytest plugin. I don't know why it's conflicting with your Django conf.

Nameko, and all services built with Nameko, need the monkey patch to be applied in their test suites, as early as possible, before any imports. AFAIK there is no other suitable hook in pytest for this.

This situation will occur when you have PyTest and Nameko installed, but are not using Eventlet (and by extension not using Nameko either). I can't think of any better workaround than @palfrey has suggested.

@jayvdb
Copy link
Author

jayvdb commented Jan 6, 2021

pytest plugins typically only register, but dont do anything, unless there is some explicit clue that the plugin is needed. The correct place to do extremely early imports is in per-project conftest.py, which could simply invoke a function in Nameko to do any common test rig initialisation like imports.

At the very least you should move "Nameko test rig hacks" out of the main Nameko library, into a separately installable library, as it is not needed by all Nameko users - it is only needed by Nameko users that are running tests. If it is only initialising eventlet, you might create a pytest-eventlet plugin, as there doesnt appear to be one already. Then the installation of the plugin package is an explicit action.

Until this automatic pollution of the pytest environment is fixed, nameko is not suitable for inclusion in distros. i.e. I cant submit https://build.opensuse.org/package/show/home:jayvdb:py-new/python-nameko

@mattbennett
Copy link
Member

Thanks, you are completely right.

Forcing all users (that run tests) to add something to their project conftest.py would be clunky and jarring for lots of existing users, but I like the idea of moving the monkey-patch to a different library.

It can be automatically included in Nameko's dev dependencies, and appear as in testing or similar install_extras for folks that want to write tests for their Nameko services.

Thanks again for raising this.

mattbennett added a commit to mattbennett/nameko that referenced this issue Oct 5, 2021
(resolves nameko#703)

* warns users if the monkeypatch is not applied
* suggests using pytest-eventlet as an alternative
mattbennett added a commit to mattbennett/nameko that referenced this issue Oct 6, 2021
(resolves nameko#703)

* warns users if the monkeypatch is not applied
* suggests using pytest-eventlet as an alternative

# Conflicts:
#	nameko/testing/pytest.py
mattbennett added a commit that referenced this issue Oct 13, 2021
* remove automatic monkey-patching

(resolves #703)

* warns users if the monkeypatch is not applied
* suggests using pytest-eventlet as an alternative

* static

* add nameko test command

+ update warning
+ move nameko run patch into cli so it is earlier and more explicit

* add __main__ entrypoint so CI can run nameko test

* always continue on error for debug

* 0.25.1 is oldest supported eventlet on py37+

we need eventlet/eventlet#584

* Revert "always continue on error for debug"

This reverts commit e6fc162.

* don't need pytest-eventlet plugin anymore
mattbennett added a commit that referenced this issue Oct 13, 2021
* remove automatic monkey-patching

(resolves #703)

* warns users if the monkeypatch is not applied
* suggests using pytest-eventlet as an alternative

# Conflicts:
#	nameko/testing/pytest.py

* static

* add nameko test command

+ update warning
+ move nameko run patch into cli so it is earlier and more explicit

* add __main__ entrypoint so CI can run nameko test

* 0.25.1 is oldest supported eventlet on py37+

we need eventlet/eventlet#584

* don't need pytest-eventlet plugin anymore
@mattbennett
Copy link
Member

This has been fixed in Nameko 2.14.0 and 3.0.0rc10, now released.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants