pytest icon indicating copy to clipboard operation
pytest copied to clipboard

Cannot create two fixtures based on the same function

Open triccare opened this issue 8 years ago • 4 comments

I am not sure I should be able to do this, but I am wondering why not. Code is below. What I would like to do is create two fixtures, but both are based on the same fixture function. However, the second one is stomping on the first one, as demonstrated below. I would have thought I would get First is None, but instead get Not First is None

Code:

import pytest


def generate_params(request):
    """Simple param reflection for pytest.fixtures"""
    return request.param


params = pytest.fixture(
    scope='function',
    params=['First']
)(generate_params)

not_params = pytest.fixture(
    scope='function',
    params=['Not First']
)(generate_params)


def test_me(params):
    assert params is None

Run:

$ py.test pytest_what.py
============================================================================= test session starts ==============================================================================
platform darwin -- Python 3.5.2, pytest-2.9.2, py-1.4.31, pluggy-0.3.1
rootdir: /Users/eisenham/Documents/ssbdev, inifile:
collected 1 items

pytest_what.py F

=================================================================================== FAILURES ===================================================================================
______________________________________________________________________________ test_me[Not First] ______________________________________________________________________________

params = 'Not First'

    def test_me(params):
>       assert params is None
E       assert 'Not First' is None

pytest_what.py:21: AssertionError
=========================================================================== 1 failed in 0.01 seconds ===========================================================================

pip list:

$ pip list
alabaster (0.7.8)
anaconda-client (1.4.0)
anaconda-navigator (1.2.1)
appnope (0.1.0)
appscript (1.0.1)
argcomplete (1.0.0)
astropy (1.2.1)
Babel (2.3.3)
backports.shutil-get-terminal-size (1.0.0)
beautifulsoup4 (4.4.1)
bitarray (0.8.1)
blaze (0.10.1)
bokeh (0.12.0)
boto (2.40.0)
Bottleneck (1.1.0)
cffi (1.6.0)
chest (0.2.3)
click (6.6)
cloudpickle (0.2.1)
clyent (1.2.2)
colorama (0.3.7)
conda (4.1.11)
conda-build (1.21.3)
conda-env (2.5.0a0)
configobj (5.0.6)
contextlib2 (0.5.3)
cryptography (1.4)
cycler (0.10.0)
Cython (0.24)
cytoolz (0.8.0)
dask (0.10.0)
datashape (0.5.2)
decorator (4.0.10)
dill (0.2.5)
docutils (0.12)
dynd (0.7.3.dev1)
elpy (1.12.0)
epc (0.0.5)
et-xmlfile (1.0.1)
fastcache (1.0.2)
flake8 (3.0.4)
Flask (0.11.1)
Flask-Cors (2.1.2)
gevent (1.1.1)
greenlet (0.4.10)
h5py (2.6.0)
HeapDict (1.0.0)
idna (2.1)
imagesize (0.7.1)
importmagic (0.1.7)
ipykernel (4.3.1)
ipython (4.2.0)
ipython-genutils (0.1.0)
ipywidgets (4.1.1)
itsdangerous (0.24)
jdcal (1.2)
jedi (0.9.0)
Jinja2 (2.8)
jsonschema (2.5.1)
jupyter (1.0.0)
jupyter-client (4.3.0)
jupyter-console (4.1.1)
jupyter-core (4.1.0)
llvmlite (0.11.0)
locket (0.2.0)
lxml (3.6.0)
MarkupSafe (0.23)
matplotlib (1.5.1)
mccabe (0.5.2)
mistune (0.7.2)
mpmath (0.19)
multipledispatch (0.4.8)
nb-anacondacloud (1.1.0)
nb-conda (1.1.0)
nb-conda-kernels (1.0.3)
nbconvert (4.2.0)
nbformat (4.0.1)
nbpresent (3.0.2)
networkx (1.11)
nltk (3.2.1)
nose (1.3.7)
notebook (4.2.1)
numba (0.26.0)
numexpr (2.6.0)
numpy (1.11.1)
odo (0.5.0)
openpyxl (2.3.2)
pandas (0.18.1)
partd (0.3.4)
path.py (0.0.0)
pathlib2 (2.1.0)
patsy (0.4.1)
pep8 (1.7.0)
pexpect (4.0.1)
pickleshare (0.7.2)
Pillow (3.2.0)
pip (8.1.2)
ply (3.8)
psutil (4.3.0)
ptyprocess (0.5.1)
py (1.4.31)
pyasn1 (0.1.9)
pycodestyle (2.0.0)
pycosat (0.6.1)
pycparser (2.14)
pycrypto (2.6.1)
pycurl (7.43.0)
pyflakes (1.2.3)
Pygments (2.1.3)
pyOpenSSL (16.0.0)
pyparsing (2.1.4)
pytest (2.9.2)
python-dateutil (2.5.3)
pytz (2016.4)
PyYAML (3.11)
pyzmq (15.2.0)
qtconsole (4.2.1)
QtPy (1.0.2)
redis (2.10.5)
requests (2.10.0)
rope-py3k (0.9.4.post1)
ruamel-yaml (-VERSION)
scikit-image (0.12.3)
scikit-learn (0.17.1)
scipy (0.17.1)
setuptools (23.0.0)
sexpdata (0.0.3)
simplegeneric (0.8.1)
singledispatch (3.4.0.3)
six (1.10.0)
snowballstemmer (1.2.1)
sockjs-tornado (1.0.3)
Sphinx (1.4.1)
sphinx-rtd-theme (0.1.9)
spyder (2.3.9)
SQLAlchemy (1.0.13)
statsmodels (0.6.1)
sympy (1.0)
tables (3.2.2)
terminado (0.6)
toolz (0.8.0)
tornado (4.3)
traitlets (4.2.1)
unicodecsv (0.14.1)
Werkzeug (0.11.10)
wheel (0.29.0)
xlrd (1.0.0)
XlsxWriter (0.9.2)
xlwings (0.7.2)
xlwt (1.1.2)

System info:

  • OS X 10.11.6
  • Python 3.5.2 :: Anaconda 4.1.1 (x86_64)

triccare avatar Aug 25 '16 21:08 triccare

Hi,

Unfortunately this is not possible with current pytest because the @pytest.fixture decorator keeps some attributes in the test function object itself, so reusing the same function object will override the attributes from the previous decoration.

Closing this as this can't be reliably fixed in current pytest (or even if it is worth doing at all TBH), but feel free to reopen if you still have questions.

nicoddemus avatar Aug 26 '16 01:08 nicoddemus

Thanks. Will consider other approaches.

triccare avatar Aug 26 '16 02:08 triccare

Would be nice if it was possible, I'm not sure at all how feasible, to have some sort of useful error message when one tries to do that. I was trying to reuse a code just like in the example here, two fixtures calling the same function which spun up a local server, and pytest didn't tell me anything, I just saw that the server didn't go up, and took me a while to figure out what was wrong (that's on me as well, don't get me wrong).

mattixpet avatar Aug 31 '22 12:08 mattixpet

@nicoddemus given that we no longer return the actual function, it should be permissible to allow this now

RonnyPfannschmidt avatar Aug 31 '22 14:08 RonnyPfannschmidt

👍

triccare avatar Oct 10 '22 15:10 triccare