Skip to content

Already imported module #2992

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

Closed
keller00 opened this issue Dec 1, 2017 · 8 comments
Closed

Already imported module #2992

keller00 opened this issue Dec 1, 2017 · 8 comments

Comments

@keller00
Copy link

keller00 commented Dec 1, 2017

Hey!
I'm trying to migrate over my tests from nose to pytest to test my twisted application.

This site says:
If a plugin is installed, pytest automatically finds and integrates it, there is no need to activate it.
So according to this all I have to do is pip install pytest-twisted, but when I try running my tests I get:
AttributeError: 'module' object has no attribute 'inlineCallbacks'

So on this site it says to put the following into conftest.py:
pytest_plugins = "pytest_twisted"
So I make conftest.py next to my testing Python file in the tests folder and get the following warning:

None
  Module already imported so can not be re-written: pytest_twisted

-- Docs: http://doc.pytest.org/en/latest/warnings.html

It seems like it auto discovers the plugin, but doesn't update itself with inlinecallback without reimporting it? Would this be a bug, or just me not understanding something properly?

Any help is appreciated! Thank you!

@RonnyPfannschmidt
Copy link
Member

there is certain places at which things can be availiable, without knowing your code its not clear how oyu use things together

@keller00
Copy link
Author

keller00 commented Dec 1, 2017

Okay, then. Here is part of my code:
So let's say that I have:
twisted_ap
├── tests
│     └── test_rtc_master.py
└── coordinator
    └── rtc_master.py
Edit: I'm not going to show the other files.
Where test_rtc_master.py looks like this:

#!/usr/bin/env python2.7
import pytest, requests
from ..coordinator.rtc_master import RtcMaster

IP = '127.0.0.1'
EXTENSION = '/rtc'

class TestRtcMaster(object):
    # See https://docs.pytest.org/en/latest/fixture.html to learn about fixtures
    @pytest.fixture(scope='class', autouse=True)
    # NOTE: setting the scope to class lets us only create the rtc master once and pass that around to
    #   all of the functions
    def rtc_master(self):
        """ Fixture for creating a Rtc Master """
        assert requests.get('https://' + IP + '/rtc/info', verify=False, timeout=2).status_code == 200
        return RtcMaster('127.0.0.1', '/rtc')

    def test_init(self, rtc_master):
        """ Check if RtcMaster was created """
        assert rtc_master
        assert isinstance(rtc_master, RtcMaster)

    @pytest.inlineCallbacks
    def test_list_streams(self, rtc_master):
        """ Check if RtcMaster was created """
        stream_list = yield rtc_master.list_rtc_streams()
        assert isinstance(stream_list, list)

@nicoddemus
Copy link
Member

@keller00 how's your environment? You should not have to use pytest_plugins if you are using a virtual environment and pip it should be detected and activated automatically.

@keller00
Copy link
Author

keller00 commented Dec 1, 2017

@nicoddemus I'm not using a virtual environment. It is detected automatically as you can see here:

=============================== test session starts ===============================
platform linux2 -- Python 2.7.12, pytest-3.3.0, py-1.5.2, pluggy-0.6.0
rootdir: /home/mark/twisted_ap/, inifile:
plugins: twisted-1.5
collected 8 items / 1 errors                                                      

===================================== ERRORS ======================================
___________________ ERROR collecting tests/test_rtc_master.py ___________________
tests/test_rtc_master.py:8: in <module>
    class TestRtcMaster(object):
tests/test_rtc_master.py:23: in TestRtcMaster
    @pytest.inlineCallbacks
E   AttributeError: 'module' object has no attribute 'inlineCallbacks'
!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!!
============================= 1 error in 0.48 seconds =============================

@keller00 keller00 closed this as completed Dec 1, 2017
@keller00 keller00 reopened this Dec 1, 2017
@nicoddemus
Copy link
Member

Are you passing --twisted on the command line? I suggest to do that or add to the pytest.ini file:

[pytest]
twisted=1

Adding pytest_plugins to the conftest.py only works if you add it to your root conftest.py file. Anyway I suggest adding to pytest.ini as this is the simplest option IMHO.

@keller00
Copy link
Author

keller00 commented Dec 1, 2017

Okay, I that fixes the problem. Thank you!
Is it not a bit counter intuitive when pytest reports the plugin, but it is not active?

@keller00 keller00 closed this as completed Dec 1, 2017
@nicoddemus
Copy link
Member

It is active, but for some reason the callbacks are only available if explicitly sets that "twisted" flag... not sure why pytest-twisted authors decided to do that: https://github.com/pytest-dev/pytest-twisted/blob/master/pytest_twisted/__init__.py#L19

@keller00
Copy link
Author

keller00 commented Dec 1, 2017

Okay, thanks! I'll bug them with my question.

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

No branches or pull requests

3 participants