pytest-dev / pytest

The pytest framework makes it easy to write small tests, yet scales to support complex functional testing
https://pytest.org
MIT License
11.93k stars 2.65k forks source link

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

Closed vipkum2 closed 5 years ago

vipkum2 commented 5 years ago
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.

nicoddemus commented 5 years ago

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.

vipkum2 commented 5 years ago

Thanks @nicoddemus to look into this!

Observed same issue without @classmethod also:


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

self = <py3_openroadm.testWNode_spdr.Tests object at 0x7f5050563a20>

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

testWNode_spdr.py:57: AttributeError
nicoddemus commented 5 years ago

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

nicoddemus commented 5 years ago

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

vipkum2 commented 5 years ago

I will paste same tomorrow.

vipkum2 commented 5 years ago

Now Please take a look:


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 = <test.Tests object at 0x7f7b9ec04278>

    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 =============================================================================================
vipkum2 commented 5 years ago

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.


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
nicoddemus commented 5 years ago

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?

vipkum2 commented 5 years ago

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 !

RonnyPfannschmidt commented 5 years ago

so this is a regression report?

nicoddemus commented 5 years ago

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.

vipkum2 commented 5 years ago

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 !!!

vipkum2 commented 5 years ago

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 !!!