Skip to content

Instantly share code, notes, and snippets.

@cielavenir
Last active May 14, 2018 02:41
Show Gist options
  • Save cielavenir/5cefaefe2a9374a666c97c851aead289 to your computer and use it in GitHub Desktop.
Save cielavenir/5cefaefe2a9374a666c97c851aead289 to your computer and use it in GitHub Desktop.
pytest session test
import pytest
import inspect
@pytest.fixture(scope="session")
def F(request):
print('setup')
yield inspect.stack()[0]
print('teardown')

I expect only 1 setup/teardown

$ python -m pytest -s
========================================================================== test session starts ===========================================================================
platform linux2 -- Python 2.7.13, pytest-3.5.0, py-1.5.3, pluggy-0.6.0
rootdir: /home/*****/devel/test, inifile:
plugins: xdist-1.22.2, cov-2.5.1, teamcity-messages-1.21, forked-0.2, repeat-0.4.1, hypothesis-3.56.5
collected 2 items                                                                                                                                                        

test_1.py setup
(<frame object at 0x7ff2c97fa730>, '/home/*****/devel/test/__init__.py', 6, 'F', ['  yield inspect.stack()[0]\n'], 0)
.                                                                                                                                                              
test_2.py setup
(<frame object at 0x7ff2c97faaa0>, '/home/*****/devel/test/__init__.py', 6, 'F', ['  yield inspect.stack()[0]\n'], 0)
.teardown
teardown


======================================================================== 2 passed in 0.02 seconds ========================================================================
$ python3 -m pytest -s
========================================================================== test session starts ===========================================================================
platform linux -- Python 3.6.1, pytest-3.5.0, py-1.5.3, pluggy-0.6.0
rootdir: /home/*****/devel/test, inifile:
plugins: teamcity-messages-1.21, cov-2.5.1
collected 2 items                                                                                                                                                        

test_1.py setup
FrameInfo(frame=<frame object at 0x7f21574c08b8>, filename='/home/*****/devel/test/__init__.py', lineno=6, function='F', code_context=['  yield inspect.stack()[0]\n'], index=0)
.                                                                                                                                                              
test_2.py setup
FrameInfo(frame=<frame object at 0x7f21574c0c18>, filename='/home/*****/devel/test/__init__.py', lineno=6, function='F', code_context=['  yield inspect.stack()[0]\n'], index=0)
.teardown
teardown


======================================================================== 2 passed in 0.03 seconds ========================================================================
from . import F
def test_1(F):
print(F)
from . import F
def test_2(F):
print(F)
@cielavenir
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment