Pytest: Observed AttributeError: 'Tests' object has no attribute 't' again as issue #3869

Created on 1 May 2019  路  13Comments  路  Source: pytest-dev/pytest

class Tests(object):
    @classmethod
    @pytest.fixture(scope='module', autouse=True)
    def getTestFixture(self, request):
        self.fixtureIp   = request.config.getoption('--fixtureIp')

        # Connect to the NE
        self.t = OpenRoadmTest(self.fixtureIp)


    def test00_getStartingConfig(self):
        print(self.t)

After executing the same observed:

    def test00_getStartingConfig(self):
>       print(self.t)
E       AttributeError: 'Tests' object has no attribute 't'

testWNode_spdr.py:58: AttributeError

Same was working with pytest 3.0.4, python 3.6.3. But now I have upgraded to pytest 4.4.1 and python 3.6.5. I have raised a same issue #3869 but solution written in issue is not working here.

question

All 13 comments

Hi @vipkum2,

Try this please:

class Tests(object):

    @pytest.fixture(scope='module', autouse=True)
    def getTestFixture(self, request):
        self.fixtureIp   = request.config.getoption('--fixtureIp')

        # Connect to the NE
        self.t = OpenRoadmTest(self.fixtureIp)


    def test00_getStartingConfig(self):
        print(self.t)

(IOW just remove the @classmethod)

If that doesn't work please post a reproducible example.

Thanks @nicoddemus to look into this!

Observed same issue without @classmethod also:
```Python
==================================================================================================== FAILURES =====================================================================================================
_________________________________________________________________________________________ Tests.test00_getStartingConfig __________________________________________________________________________________________

self =

def test00_getStartingConfig(self):
  print(self.t)

E AttributeError: 'Tests' object has no attribute 't'

testWNode_spdr.py:57: AttributeError

That's strange. Can you post a reproducible example? If not, please post the full code.

Oh I just realized my example missed something: use type(self).t = ... too

I will paste same tomorrow.

Now Please take a look:
```Python
hax-vipkumar-1|~/scripts$ cat test.py
import pytest
class Tests(object):
@classmethod
@pytest.fixture(scope='module', autouse=True)
def getTestFixture(self, request):
self.fixtureIp = '1.1.1.1'

    # Connect to the NE
    self.t = 5


def test00_getStartingConfig(self):
    print(self.t)

hax-vipkumar-1|~/scripts$

hax-vipkumar-1|~/scripts$ pt3 -s -v test.py
=============================================================================================== test session starts ===============================================================================================
platform linux -- Python 3.6.5, pytest-4.4.1, py-1.8.0, pluggy-0.9.0 -- /opt/tools/swtools/anaconda/pftools/envs/linux-64/openroadm_py3/bin/python
cachedir: .pytest_cache
rootdir: /home/vipkumar/scripts
collected 1 item

test.py::Tests::test00_getStartingConfig FAILED

==================================================================================================== FAILURES =====================================================================================================
_________________________________________________________________________________________ Tests.test00_getStartingConfig __________________________________________________________________________________________

self =

def test00_getStartingConfig(self):
  print(self.t)

E AttributeError: 'Tests' object has no attribute 't'

test.py:13: AttributeError
============================================================================================ 1 failed in 0.05 seconds =============================================================================================

And as you told, below is working without @classmethod and with type(self).t:
I don't want to change the script accordingly this as these are working scripts.

```Python
import pytest
class Tests(object):
#@classmethod
@pytest.fixture(scope='module', autouse=True)
def getTestFixture(self, request):
self.fixtureIp = '1.1.1.1'

    # Connect to the NE
    type(self).t = 5


def test00_getStartingConfig(self):
    print(self.t)

hax-vipkumar-1|~/scripts$ pt3 -s -v test.py
=============================================================================================== test session starts ===============================================================================================
platform linux -- Python 3.6.5, pytest-4.4.1, py-1.8.0, pluggy-0.9.0 -- /opt/tools/swtools/anaconda/pftools/envs/linux-64/openroadm_py3/bin/python
cachedir: .pytest_cache
rootdir: /home/vipkumar/scripts
collected 1 item

test.py::Tests::test00_getStartingConfig 5
PASSED

I don't want to change the script accordingly this as these are working scripts.

I don't understand, you say you don't want to apply the working fix above?

Actually, These are working with earlier version of pytest, but in latest version, to execute the same scripts I have to change the scripts with your fix.
So my query is - can't we change the same in pytest if possible. Otherwise if there is no option left I have to change all the scripts.

Thank you very much for your time and help !

so this is a regression report?

So my query is - can't we change the same in pytest if possible. Otherwise if there is no option left I have to change all the scripts.

Oh I see. Unfortunately this is a duplicate of #3778, which unfortunately we won't be able to fix, for the reasons mentioned in that issue and in #3781. 馃槙

I'm afraid the only solution now is to update your test files.

Ok thanks @nicoddemus ,

At least we have workaround to run the suites. I will change the scripts accordingly.

Thanks once again to resolving my problem !!!

Ok thanks @nicoddemus ,

At least we have workaround to run the suites. I will change the scripts accordingly.

Thanks once again to resolving my problem !!!

Was this page helpful?
0 / 5 - 0 ratings