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
12.14k stars 2.69k forks source link

Class decorators don't work for pytest fixtures inside them #3958

Open godlark opened 6 years ago

godlark commented 6 years ago

Minimal example:

@freeze_time('2016-02-18')
class TestOrderWithAddressCollection2:

    @pytest.fixture(autouse=True)
    def current_date(self):
        self.now = timezone.now()

    def test_current_date(self):
        assert self.now == timezone.now()

For pytest > 3.7.0 (including 3.8.0) I get:

tests/test_returns/test_domain.py:73 (TestOrderWithAddressCollection2.test_current_date)
FakeDatetime(2016, 2, 18, 0, 0, tzinfo=<UTC>) != datetime.datetime(2018, 9, 7, 16, 52, 44, 922611, tzinfo=<UTC>)

Expected :datetime.datetime(2018, 9, 7, 16, 52, 44, 922611, tzinfo=<UTC>)
Actual   :FakeDatetime(2016, 2, 18, 0, 0, tzinfo=<UTC>)
 <Click to see difference>

test_domain.py:75: in test_current_date
    assert self.now == timezone.now()
E   assert datetime.datetime(2018, 9, 7, 16, 52, 44, 922611, tzinfo=<UTC>) == FakeDatetime(2016, 2, 18, 0, 0, tzinfo=<UTC>)
E    +  where datetime.datetime(2018, 9, 7, 16, 52, 44, 922611, tzinfo=<UTC>) = <tests.test_returns.test_domain.TestOrderWithAddressCollection2 object at 0x7f30b9f21630>.now
E    +  and   FakeDatetime(2016, 2, 18, 0, 0, tzinfo=<UTC>) = <function now at 0x7f30c1023950>()
E    +    where <function now at 0x7f30c1023950> = timezone.now

For pytest 3.7.0 it passes.

PIP list:

Package               Version
--------------------- -------
amqp                  2.3.2  
apipkg                1.5    
appdirs               1.4.3  
asn1crypto            0.24.0 
astroid               1.6.5  
atomicwrites          1.2.1  
attrs                 18.2.0 
aws-xray-sdk          2.1.0  
backcall              0.1.0  
beautifulsoup4        4.6.0  
billiard              3.5.0.4
boto                  2.49.0 
boto3                 1.4.7  
botocore              1.7.48 
cached-property       1.5.1  
celery                4.1.1  
cffi                  1.11.5 
cookies               2.2.1  
coverage              5.0a2  
cryptography          2.3.1  
decorator             4.3.0  
defusedxml            0.5.0  
diff-cover            1.0.4  
dj-database-url       0.4.2  
Django                2.0.6  
django-allauth        0.36.0 
django-bootstrap3     10.0.1 
django-countries      5.0    
django-formtools      2.1    
django-pipeline       1.6.12 
django-waffle         0.14.0 
django-webpack-loader 0.5.0  
django-widget-tweaks  1.4.2  
djangorestframework   3.8.2  
docker                3.5.0  
docker-pycreds        0.3.0  
docutils              0.14   
ecdsa                 0.13   
execnet               1.5.0  
factory-boy           2.8.1  
Faker                 0.9.0  
flake8                3.5.0  
flake8-isort          2.5    
freezegun             0.3.9  
future                0.16.0 
idna                  2.7    
inflect               1.0.0  
ipdb                  0.11   
ipython               6.5.0  
ipython-genutils      0.2.0  
isodate               0.6.0  
isort                 4.3.4  
jedi                  0.12.1 
Jinja2                2.10   
jinja2-pluralize      0.3.0  
jmespath              0.9.3  
jsondiff              1.1.1  
jsonpickle            0.9.6  
kombu                 4.2.1  
lazy-object-proxy     1.3.1  
lxml                  4.2.4  
MarkupSafe            1.0    
mccabe                0.6.1  
mock                  2.0.0  
more-itertools        4.3.0  
moto                  1.2.0  
multidict             4.4.0  
oauthlib              2.1.0  
paramiko              1.18.4 
parso                 0.3.1  
pbr                   4.2.0  
pdfkit                0.6.1  
pexpect               4.6.0  
phonenumberslite      8.8.7  
pickleshare           0.7.4  
Pillow                5.0.0  
pip                   18.0   
pluggy                0.7.1  
prompt-toolkit        1.0.15 
psycopg2-binary       2.7.5  
ptyprocess            0.6.0  
py                    1.6.0  
pyaml                 17.12.1
pycodestyle           2.3.1  
pycparser             2.18   
pycrypto              2.6.1  
pyflakes              1.6.0  
Pygments              2.2.0  
pylint                1.9.3  
pylint-django         0.11.1 
pylint-plugin-utils   0.4    
PyPDF2                1.26.0 
pytest                3.8.0  
pytest-cache          1.0    
pytest-cov            2.6.0  
pytest-django         3.3.0  
python-dateutil       2.6.0  
python3-openid        3.1.0  
pytz                  2018.5 
PyYAML                3.13   
redis                 2.10.6 
reportlab             3.4.0  
requests              2.15.1 
requests-oauthlib     1.0.0  
requests-toolbelt     0.8.0  
responses             0.9.0  
s3transfer            0.1.13 
setuptools            40.2.0 
simplegeneric         0.8.1  
six                   1.11.0 
testfixtures          6.3.0  
text-unidecode        1.2    
traitlets             4.3.2  
Unidecode             0.4.20 
vcrpy                 1.10.5 
vine                  1.1.4  
wcwidth               0.1.7  
websocket-client      0.52.0 
Werkzeug              0.14.1 
wheel                 0.31.1 
wrapt                 1.10.11
xmltodict             0.11.0 
yarl                  1.2.6  
zeep                  3.0.0 
RonnyPfannschmidt commented 6 years ago

at first glance this mainly worked by accident, and will fatally break once fixture declarations are no longer callable