2 new commits in pytest: https://bitbucket.org/hpk42/pytest/commits/af707032e4da/ Changeset: af707032e4da User: hpk42 Date: 2013-05-05 14:48:17 Summary: bump version Affected #: 2 files
diff -r f693c65bfa4dd90625eb231e2bcd2ba593103c61 -r af707032e4da0d1f391f2683b017cc698ce620d9 _pytest/__init__.py --- a/_pytest/__init__.py +++ b/_pytest/__init__.py @@ -1,2 +1,2 @@ # -__version__ = '2.3.5' +__version__ = '2.3.6.dev1' diff -r f693c65bfa4dd90625eb231e2bcd2ba593103c61 -r af707032e4da0d1f391f2683b017cc698ce620d9 setup.py --- a/setup.py +++ b/setup.py @@ -12,7 +12,7 @@ name='pytest', description='py.test: simple powerful testing with Python', long_description = long_description, - version='2.3.5', + version='2.3.6.dev1', url='http://pytest.org', license='MIT license', platforms=['unix', 'linux', 'osx', 'cygwin', 'win32'], https://bitbucket.org/hpk42/pytest/commits/b93ac0cdae02/ Changeset: b93ac0cdae02 User: hpk42 Date: 2013-05-05 14:48:37 Summary: allow fixture functions to be implemented as context managers: @pytest.fixture def myfix(): # setup yield 1 # teardown Affected #: 5 files diff -r af707032e4da0d1f391f2683b017cc698ce620d9 -r b93ac0cdae02effaa3c136a681cc45bba757fe46 CHANGELOG --- a/CHANGELOG +++ b/CHANGELOG @@ -1,4 +1,12 @@ -Changes between 2.3.4 and 2.3.5dev +Changes between 2.3.5 and DEV +----------------------------------- + +- (experimental) allow fixture functions to be + implemented as context managers + + + +Changes between 2.3.4 and 2.3.5 ----------------------------------- - never consider a fixture function for test function collection diff -r af707032e4da0d1f391f2683b017cc698ce620d9 -r b93ac0cdae02effaa3c136a681cc45bba757fe46 _pytest/__init__.py --- a/_pytest/__init__.py +++ b/_pytest/__init__.py @@ -1,2 +1,2 @@ # -__version__ = '2.3.6.dev1' +__version__ = '2.3.6.dev2' diff -r af707032e4da0d1f391f2683b017cc698ce620d9 -r b93ac0cdae02effaa3c136a681cc45bba757fe46 _pytest/python.py --- a/_pytest/python.py +++ b/_pytest/python.py @@ -1613,6 +1613,29 @@ except ValueError: pass +def call_fixture_func(fixturefunc, request, kwargs): + if is_generator(fixturefunc): + iter = fixturefunc(**kwargs) + next = getattr(iter, "__next__", None) + if next is None: + next = getattr(iter, "next") + res = next() + def teardown(): + try: + next() + except StopIteration: + pass + else: + fs, lineno = getfslineno(fixturefunc) + location = "%s:%s" % (fs, lineno+1) + pytest.fail( + "fixture function %s has more than one 'yield': \n%s" % + (fixturefunc.__name__, location), pytrace=False) + request.addfinalizer(teardown) + else: + res = fixturefunc(**kwargs) + return res + class FixtureDef: """ A container for a factory definition. """ def __init__(self, fixturemanager, baseid, argname, func, scope, params, @@ -1663,7 +1686,7 @@ fixturefunc = fixturefunc.__get__(request.instance) except AttributeError: pass - result = fixturefunc(**kwargs) + result = call_fixture_func(fixturefunc, request, kwargs) assert not hasattr(self, "cached_result") self.cached_result = result return result diff -r af707032e4da0d1f391f2683b017cc698ce620d9 -r b93ac0cdae02effaa3c136a681cc45bba757fe46 setup.py --- a/setup.py +++ b/setup.py @@ -12,7 +12,7 @@ name='pytest', description='py.test: simple powerful testing with Python', long_description = long_description, - version='2.3.6.dev1', + version='2.3.6.dev2', url='http://pytest.org', license='MIT license', platforms=['unix', 'linux', 'osx', 'cygwin', 'win32'], diff -r af707032e4da0d1f391f2683b017cc698ce620d9 -r b93ac0cdae02effaa3c136a681cc45bba757fe46 testing/python/fixture.py --- a/testing/python/fixture.py +++ b/testing/python/fixture.py @@ -1795,3 +1795,101 @@ *hello world* """) + + +class TestContextManagerFixtureFuncs: + def test_simple(self, testdir): + testdir.makepyfile(""" + import pytest + @pytest.fixture + def arg1(): + print ("setup") + yield 1 + print ("teardown") + def test_1(arg1): + print ("test1 %s" % arg1) + def test_2(arg1): + print ("test2 %s" % arg1) + assert 0 + """) + result = testdir.runpytest("-s") + result.stdout.fnmatch_lines(""" + setup + test1 1 + teardown + setup + test2 1 + teardown + """) + + def test_scoped(self, testdir): + testdir.makepyfile(""" + import pytest + @pytest.fixture(scope="module") + def arg1(): + print ("setup") + yield 1 + print ("teardown") + def test_1(arg1): + print ("test1 %s" % arg1) + def test_2(arg1): + print ("test2 %s" % arg1) + """) + result = testdir.runpytest("-s") + result.stdout.fnmatch_lines(""" + setup + test1 1 + test2 1 + teardown + """) + + def test_setup_exception(self, testdir): + testdir.makepyfile(""" + import pytest + @pytest.fixture(scope="module") + def arg1(): + pytest.fail("setup") + yield 1 + def test_1(arg1): + pass + """) + result = testdir.runpytest("-s") + result.stdout.fnmatch_lines(""" + *pytest.fail*setup* + *1 error* + """) + + def test_teardown_exception(self, testdir): + testdir.makepyfile(""" + import pytest + @pytest.fixture(scope="module") + def arg1(): + yield 1 + pytest.fail("teardown") + def test_1(arg1): + pass + """) + result = testdir.runpytest("-s") + result.stdout.fnmatch_lines(""" + *pytest.fail*teardown* + *1 passed*1 error* + """) + + + def test_yields_more_than_one(self, testdir): + testdir.makepyfile(""" + import pytest + @pytest.fixture(scope="module") + def arg1(): + yield 1 + yield 2 + def test_1(arg1): + pass + """) + result = testdir.runpytest("-s") + result.stdout.fnmatch_lines(""" + *fixture function* + *test_yields*:2* + """) + + Repository URL: https://bitbucket.org/hpk42/pytest/ -- This is a commit notification from bitbucket.org. You are receiving this because you have the service enabled, addressing the recipient of this email. _______________________________________________ pytest-commit mailing list pytest-commit@python.org http://mail.python.org/mailman/listinfo/pytest-commit