Pytest: When a parametrized fixture is present, even unrelated tests (not using the fixture) are being run twice.

Created on 23 Jul 2020  路  2Comments  路  Source: pytest-dev/pytest

Having a two fixtures in conftest, one parameterized (two params), unused, one unparameterized forces all present tests to be run twice.

MWE:

import pytest

#"""
# uncomment to see double tests!
@pytest.fixture(scope="session", params=["single", "multi"], autouse=True)
def parametrized_something(request):
    return request.param
#"""


@pytest.fixture(scope="session", autouse=True)
def reg_list():
    return "yea"


"""
Then we have a file test_params.py, that looks like that: 
def test_single_param(reg_list):
    assert reg_list == "yea"
But gets executed twice, even though the fixture needed is not parametrized:
"""

Running on Ubuntu 18.04.4 LTS.

My pip list:

Package                  Version
------------------------ ------------
absl-py                  0.9.0
appdirs                  1.4.4
astunparse               1.6.3
attrs                    19.3.0
backcall                 0.2.0
beautifulsoup4           4.9.1
boto3                    1.13.11
botocore                 1.16.11
brokenaxes               0.4.2
Brotli                   1.0.7
cachetools               4.1.0
certifi                  2020.4.5.1
cfgv                     3.1.0
cftime                   1.1.3
chardet                  3.0.4
click                    7.1.1
cloudpickle              1.4.1
cmdstanpy                0.4.0
configobj                5.0.6
convertdate              2.2.1
cycler                   0.10.0
Cython                   0.29.17
daemonize                2.5.0
dask                     2.16.0
decorator                4.4.2
distlib                  0.3.0
distributed              2.16.0
docutils                 0.15.2
dropbox                  10.2.0
ephem                    3.7.7.1
fbprophet                0.6
filelock                 3.0.12
Flask                    1.1.2
Flask-Compress           1.5.0
gast                     0.3.3
google-api-core          1.19.1
google-api-python-client 1.9.1
google-auth              1.16.1
google-auth-httplib2     0.0.3
google-auth-oauthlib     0.4.1
google-pasta             0.2.0
googleapis-common-protos 1.52.0
graphviz                 0.14
grpcio                   1.29.0
guildai                  0.7.0rc9
h5py                     2.10.0
HeapDict                 1.0.1
hiplot                   0.1.11
holidays                 0.10.2
httplib2                 0.18.1
identify                 1.4.15
idna                     2.9
importlib-metadata       1.6.0
ipython                  7.15.0
ipython-genutils         0.2.0
itsdangerous             1.1.0
jedi                     0.17.0
Jinja2                   2.11.2
jmespath                 0.10.0
joblib                   0.14.1
jsonpickle               1.4.1
Keras-Preprocessing      1.1.2
kiwisolver               1.2.0
korean-lunar-calendar    0.2.1
lightgbm                 2.3.1
LunarCalendar            0.0.9
Markdown                 3.2.2
MarkupSafe               1.1.1
matplotlib               3.2.0
metaflow                 2.0.3
mlframework              0.1.1
more-itertools           8.3.0
msgpack                  1.0.0
msgpack-numpy            0.4.5
netCDF4                  1.5.3
nodeenv                  1.3.5
numpy                    1.18.1
oauth2client             4.1.3
oauthlib                 3.1.0
omegaconf                2.0.0
opt-einsum               3.2.1
packaging                20.3
pandas                   1.0.1
parso                    0.7.0
patsy                    0.5.1
pexpect                  4.8.0
pickleshare              0.7.5
Pillow                   7.1.2
pip                      20.1
pkginfo                  1.5.0.1
pluggy                   0.13.1
pmdarima                 1.6.0
pre-commit               2.1.1
prompt-toolkit           3.0.5
protobuf                 3.12.0
psutil                   5.7.0
psycopg2                 2.8.5
ptyprocess               0.6.0
pvlib                    0.7.1
py                       1.8.1
pyaml                    20.4.0
pyasn1                   0.4.8
pyasn1-modules           0.2.8
pycurl                   7.43.0.5
pydantic                 1.4
pydevd                   1.9.1
pydot                    1.4.1
PyDrive                  1.3.1
Pygments                 2.6.1
PyMeeus                  0.3.7
Pympler                  0.8
pyparsing                2.4.7
pystan                   2.19.1.1
pytest                   5.4.3
python-dateutil          2.8.1
python-prctl             1.7
pytz                     2020.1
PyYAML                   5.3.1
pyzmq                    19.0.1
requests                 2.23.0
requests-oauthlib        1.3.0
rsa                      4.0
s3transfer               0.3.3
scikit-learn             0.22.2.post1
scikit-optimize          0.5.2
scipy                    1.4.1
seaborn                  0.10.0
setuptools               46.4.0
setuptools-git           1.2
simplejson               3.17.0
siphon                   0.8.0
six                      1.14.0
sklearn-pandas           1.8.0
sortedcontainers         2.1.0
soupsieve                2.0.1
SQLAlchemy               1.3.17
statsmodels              0.11.1
tabulate                 0.8.7
tabview                  1.4.4
tblib                    1.6.0
tensorboard              2.2.1
tensorboard-plugin-wit   1.6.0.post3
tensorflow               2.3.0rc1
tensorflow-addons        0.9.1
tensorflow-estimator     2.3.0rc0
tensorflow-gpu           2.2.0
tensorpack               0.10.1
termcolor                1.1.0
threadpoolctl            2.0.0
timezonefinder           4.2.0
toml                     0.10.1
toolz                    0.10.0
tornado                  6.0.4
tqdm                     4.46.1
traitlets                4.3.3
typeguard                2.7.1
typing-extensions        3.7.4.2
uritemplate              3.0.1
urllib3                  1.25.9
virtualenv               20.0.20
wcwidth                  0.1.9
Werkzeug                 1.0.1
wget                     3.2
wheel                    0.34.2
wrapt                    1.12.1
xgboost                  1.1.0
zict                     2.0.0
zipp                     3.1.0

question

All 2 comments

Autouse enforces to use the fixture for all tests, take out the autouse to keep the fixture to only the relevant tests

Closing this as I agree this is intended behavior with an autouse fixture. Feel free to follow up if you have any remaining questions, though!

Was this page helpful?
0 / 5 - 0 ratings