Skip to content

fixtures with yield statement in if-else clause crash pytest #7061

@allan-silverstein

Description

@allan-silverstein

If I have a fixture with a yield statement within an else clause and the else clause is False, pytest will crash

Here is a simple example (note one of the parametrize lines is purposely commented out as you can use it to toggle back and forth. Only when "filename" is None does it crash. Otherwise it runs fine. Am I missing something? Is there a different way that I'm suppose to do this? My objective is to only have the fixture do something if the filename passed to the fixture is not None. If there is a valid filename, then I want the fixture to do pre-setup work and then yield and then post test cleanup....

import pytest

#################################################
# Simple test
#################################################
#@pytest.mark.parametrize("dut, filename", [("dev1", "myfile.yml"),])
@pytest.mark.parametrize("dut, filename", [("dev1", None),])
def test_1(dut, filename, preconfig_fix2):
    print("Running test function")

@pytest.fixture(scope="function")
def preconfig_fix2(request,):

    filename = request.getfuncargvalue("filename")
    if filename is None:
        print("There is nothing to do here!!")
    else:
        print("There is work to do")
        yield
        print("Finishing up")

python --version
Python 3.7.4

Ubuntu 16.04.6 LTS

pip freeze
alerta==6.5.0
allure-pytest==2.6.5
allure-python-commons==2.6.5
ansi2html==1.5.2
asn1crypto==0.24.0
astroid==2.2.5
atomicwrites==1.3.0
attrs==19.1.0
auto-reporting-system==1.3.3
auto-ssh-client==1.0
auto-tests-fw==1.0
auto-utils==1.0
backcall==0.1.0
backports.ssl==0.0.9
bcrypt==3.1.7
bitarray==1.0.1
blackswan==1.0.75
bleach==3.1.0
certifi==2019.9.11
cffi==1.12.3
chardet==3.0.4
Click==7.0
colorlog==4.0.2
cryptography==2.7
dateparser==0.7.2
datetime-extractor==0.5
decorator==4.1.2
defusedxml==0.6.0
dill==0.3.0
dn-grpc-client==1.0.0
dn-ncclient==1.0.0
docker==3.7.0
docker-pycreds==0.4.0
docutils==0.15.2
dominate==2.3.5
easysnmp==0.2.5
elasticsearch==6.1.1
elasticsearch-dsl==6.1.0
entrypoints==0.3
enum34==1.1.6
Flask==1.0.2
Flask-Bootstrap==3.3.7.1
Flask-Login==0.4.1
flask-nav==0.6
Flask-WTF==0.14.2
future==0.17.1
gevent==1.4.0
GitHub-Flask==3.2.0
googleapis-common-protos==1.5.8
greenlet==0.4.15
grpcio==1.19.0
idna==2.6
importlib-metadata==0.19
ipaddress==1.0.22
ipykernel==5.1.2
ipython==7.8.0
ipython-genutils==0.2.0
ipywidgets==7.5.1
isort==4.3.4
itsdangerous==1.1.0
ixia==1.0.0
IxNetwork==8.50.1501.10
ixnetwork-restpy==1.0.51
jedi==0.15.1
Jinja2==2.10
jira==2.0.0
jsonschema==3.0.2
jupyter==1.0.0
jupyter-client==5.3.3
jupyter-console==6.0.0
jupyter-core==4.5.0
lazy-object-proxy==1.4.2
libraries==1.0
Logbook==1.5.2
lxml==4.1.1
MarkupSafe==1.1.1
mccabe==0.6.1
mistune==0.8.4
more-itertools==7.2.0
natsort==6.0.0
nbconvert==5.6.0
nbformat==4.4.0
ncclient==0.6.4
netaddr==0.7.19
netmiko==2.4.1
notebook==6.0.1
numpy==1.17.2
oauthlib==3.1.0
packaging==19.1
pandas==0.25.1
pandocfilters==1.4.2
paramiko==2.5.0
parso==0.5.1
pbr==5.4.3
pexpect==4.7.0
pickleshare==0.7.5
pluggy==0.12.0
ply==3.11
prometheus-client==0.7.1
prompt-toolkit==2.0.9
protobuf==3.9.1
psycopg2-binary==2.8
ptyprocess==0.6.0
py==1.8.0
pyaml==19.4.1
pyang==2.0.1
pyangbind==0.8.1
pyasn1==0.4.7
pycodestyle==2.5.0
pycparser==2.19
pycryptodomex==3.9.0
pydocstyle==4.0.1
pyflakes==2.1.1
Pygments==2.4.2
pygrok==1.0.0
PyJWT==1.7.1
pylama==7.6.6
pylint==2.3.1
PyNaCl==1.3.0
pyOpenSSL==19.0.0
pyparsing==2.4.2
pyrsistent==0.15.4
pyserial==3.4
pyshark==0.4.2.5
pysmi==0.3.4
pysnmp==4.4.6
pytest==3.7.0
pytest-check==0.3.5
pytest-csv==2.0.1
pytest-dependency==0.4.0
pytest-instafail==0.4.0
pytest-ordering==0.6
pytest-repeat==0.7.0
pytest-reportportal==1.0.5
python-consul==1.1.0
python-dateutil==2.6.1
pytz==2017.3
PyXB==1.2.6
PyYAML==5.1.1
pyzmq==18.1.0
qtconsole==4.5.5
redis==2.10.6
regex==2019.6.8
rejson==0.2.1
reportportal-client==3.2.0
requests==2.18.4
requests-oauthlib==1.2.0
requests-toolbelt==0.9.1
scp==0.13.2
selenium==3.141.0
semantic-version==2.6.0
Send2Trash==1.5.0
six==1.12.0
snowballstemmer==1.9.1
SQLAlchemy==1.3.2
tabulate==0.8.2
terminado==0.8.2
testpath==0.4.2
textfsm==1.1.0
timeout-decorator==0.4.1
tornado==6.0.3
traitlets==4.3.2
typed-ast==1.4.0
tzlocal==2.0.0
urllib3==1.22
visitor==0.1.3
waiting==1.4.1
wcwidth==0.1.7
webencodings==0.5.1
websocket-client==0.56.0
Werkzeug==0.15.6
widgetsnbextension==3.5.1
wrapt==1.11.2
WTForms==2.2.1
WTForms-JSON==0.3.3
XlsxWriter==1.0.2
xmltodict==0.11.0
yangson==1.3.20
zipp==0.5.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: fixturesanything involving fixtures directly or indirectlytopic: reportingrelated to terminal output and user-facing messages and errorstype: enhancementnew feature or API change, should be merged into features branch

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions