Skip to content

Commit

Permalink
Merge pull request #41 from TvoroG/issues39
Browse files Browse the repository at this point in the history
Issues39
  • Loading branch information
TvoroG authored Oct 13, 2019
2 parents 8f234fd + cf72a89 commit b1837ab
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 17 deletions.
21 changes: 10 additions & 11 deletions pytest_lazyfixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,16 @@ def pytest_runtest_setup(item):
def fillfixtures(_fillfixtures):
def fill(request):
item = request._pyfuncitem
fixturenames = item.fixturenames
autousenames = item.session._fixturemanager._getautousenames(item.nodeid)

for fname in fixturenames:
if fname not in item.funcargs and fname in autousenames:
item.funcargs[fname] = request.getfixturevalue(fname)
fixturenames = getattr(item, "fixturenames", None)
if fixturenames is None:
fixturenames = request.fixturenames

if hasattr(item, 'callspec'):
for param, val in sorted_by_dependency(item.callspec.params, fixturenames):
if is_lazy_fixture(val):
if val is not None and is_lazy_fixture(val):
item.callspec.params[param] = request.getfixturevalue(val.name)
elif param not in item.funcargs:
item.funcargs[param] = request.getfixturevalue(param)

_fillfixtures()
return fill
Expand Down Expand Up @@ -126,9 +125,9 @@ def sorted_by_dependency(params, fixturenames):
non_free_fm = defaultdict(list)

for key in _sorted_argnames(params, fixturenames):
val = params[key]
val = params.get(key)

if not is_lazy_fixture(val) or val.name not in params:
if not val or not is_lazy_fixture(val) or val.name not in params:
free_fm.append(key)
else:
non_free_fm[val.name].append(key)
Expand All @@ -139,7 +138,7 @@ def sorted_by_dependency(params, fixturenames):
_tree_to_list(non_free_fm, free_key)
)

return [(key, params[key]) for key in (free_fm + non_free_fm_list)]
return [(key, params.get(key)) for key in (free_fm + non_free_fm_list)]


def _sorted_argnames(params, fixturenames):
Expand All @@ -148,7 +147,7 @@ def _sorted_argnames(params, fixturenames):
for name in fixturenames:
if name in argnames:
argnames.remove(name)
yield name
yield name

if argnames:
for name in argnames:
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[flake8]
max-line-length = 120
ignore = F821
ignore = F821 W503 W504
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def read(fname):

setup(
name='pytest-lazy-fixture',
version='0.5.2',
version='0.6.0',
author='Marsel Zaripov',
author_email='[email protected]',
maintainer='Marsel Zaripov',
Expand All @@ -23,7 +23,7 @@ def read(fname):
description='It helps to use fixtures in pytest.mark.parametrize',
long_description=read('README.rst'),
py_modules=['pytest_lazyfixture'],
install_requires=['pytest>=2.9.2'],
install_requires=['pytest>=3.2.5'],
classifiers=[
'Development Status :: 4 - Beta',
'Framework :: Pytest',
Expand Down
158 changes: 156 additions & 2 deletions tests/test_lazyfixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,8 +575,8 @@ def test_sorted_by_dependency(params, expected_paths):


@pytest.mark.parametrize('params,fixturenames,expect_keys', [
({'b': 1, 'a': 0}, ['c', 'a', 'd', 'b'], ['a', 'b']),
({'b': 1, 'a': 0}, ['c', 'b'], ['b', 'a'])
({'b': 1, 'a': 0}, ['c', 'a', 'd', 'b'], ['c', 'a', 'd', 'b']),
({'b': 1, 'a': 0}, ['c', 'b'], ['c', 'b', 'a'])
])
def test_sorted_argnames(params, fixturenames, expect_keys):
assert list(_sorted_argnames(params, fixturenames)) == expect_keys
Expand Down Expand Up @@ -698,3 +698,157 @@ def test_func(some_fixture2):
""")
reprec = testdir.inline_run('-s')
reprec.assertoutcome(passed=2)


# https://github.com/TvoroG/pytest-lazy-fixture/issues/39
def test_usefixture_runs_before_function_fixtures(testdir):
testdir.makepyfile("""
import pytest
from pytest_lazyfixture import lazy_fixture
invocation_order = []
@pytest.fixture
def module_fixture():
invocation_order.append('using module fixture')
@pytest.fixture
def fixture1():
invocation_order.append('using fixture1')
return 'fixture1'
@pytest.fixture
def fixture2():
invocation_order.append('using fixture2')
return 'fixture2'
@pytest.mark.usefixtures("module_fixture")
@pytest.mark.parametrize("fixt", [lazy_fixture("fixture1"), lazy_fixture("fixture2")])
def test_test(fixt):
if fixt == 'fixture2':
print(' '.join(invocation_order))
""")
result = testdir.runpytest('-s')
stdout = result.stdout.str()
assert (
'using module fixture using fixture1 using module fixture using fixture2' in stdout
)


# https://github.com/TvoroG/pytest-lazy-fixture/issues/39
def test_autouse_and_usefixture_module_scope_runs_before_function_fixtures(testdir):
testdir.makepyfile("""
import pytest
from pytest_lazyfixture import lazy_fixture
invocation_order = []
@pytest.fixture(autouse=True)
def autouse_fixture():
invocation_order.append('using autouse_fixture')
@pytest.fixture(scope='module')
def module_fixture():
invocation_order.append('using module fixture')
@pytest.fixture
def fixture1():
invocation_order.append('using fixture1')
return 'fixture1'
@pytest.fixture
def fixture2():
invocation_order.append('using fixture2')
return 'fixture2'
@pytest.mark.usefixtures("module_fixture")
@pytest.mark.parametrize("fixt", [lazy_fixture("fixture1"), lazy_fixture("fixture2")])
def test_test(fixt):
if fixt == 'fixture2':
print(' '.join(invocation_order))
""")
result = testdir.runpytest('-s')
stdout = result.stdout.str()
assert (
# pytest==3.2.5
'using autouse_fixture using module fixture using fixture1 using autouse_fixture using fixture2' in stdout
or
'using module fixture using autouse_fixture using fixture1 using autouse_fixture using fixture2' in stdout
)


@pytest.mark.parametrize('autouse_scope', [
'session',
'module',
pytest.param('function', marks=pytest.mark.xfail)
])
def test_session_autouse_and_usefixture_module_scope_runs_before_function_fixtures(testdir, autouse_scope):
testdir.makepyfile("""
import pytest
from pytest_lazyfixture import lazy_fixture
invocation_order = []
@pytest.fixture(autouse=True, scope='{autouse_scope}')
def autouse_fixture():
invocation_order.append('using autouse_fixture')
@pytest.fixture(scope='module')
def module_fixture():
invocation_order.append('using module fixture')
@pytest.fixture
def fixture1():
invocation_order.append("using fixture1")
return 'fixture1'
@pytest.fixture
def fixture2():
invocation_order.append("using fixture2")
return 'fixture2'
@pytest.mark.usefixtures("module_fixture")
@pytest.mark.parametrize("fixt", [lazy_fixture("fixture1"), lazy_fixture("fixture2")])
def test_test(fixt):
if fixt == 'fixture2':
print(' '.join(invocation_order))
""".format(autouse_scope=autouse_scope))
result = testdir.runpytest('-s')
assert 'using autouse_fixture using module fixture using fixture1 using fixture2' in result.stdout.str()


# https://github.com/TvoroG/pytest-lazy-fixture/issues/39
def test_module_scope_runs_before_function_fixtures(testdir):
testdir.makepyfile("""
import pytest
from pytest_lazyfixture import lazy_fixture
invocation_order = []
@pytest.fixture(scope='module')
def module_fixture():
invocation_order.append('using module fixture')
@pytest.fixture
def fixture1():
invocation_order.append("using fixture1")
return 'fixture1'
@pytest.fixture
def fixture2():
invocation_order.append("using fixture2")
return 'fixture2'
@pytest.mark.parametrize("fixt", [lazy_fixture("fixture1"), lazy_fixture("fixture2")])
def test_test(fixt, module_fixture):
if fixt == 'fixture2':
print(' '.join(invocation_order))
""")
result = testdir.runpytest('-s')
stdout = result.stdout.str()
assert (
# pytest==3.2.5
'using fixture1 using module fixture using fixture2' in stdout
or
'using module fixture using fixture1 using fixture2' in stdout
)
42 changes: 41 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -1,15 +1,55 @@
# For more information about tox, see https://tox.readthedocs.io/en/latest/
[tox]
envlist =
{py27,py34,py35,py36,py37,pypy}-pytest_{3_2,3_6,last}
{py27,py34,py35,py36,py37,pypy}-pytest_{3_2,3_3,3_4,3_5,3_6,3_7,3_8,3_9,3_10,4_0,4_1,4_2,4_3,4_4,4_5,4_6,5_0,last}
flake8
skip_missing_interpreters=True

[testenv]
commands = py.test {posargs:tests}
deps =
pytest_3_2: pytest<3.3.0

pytest_3_3: pytest<3.4.0
pytest_3_3: attrs==19.1.0

pytest_3_4: pytest<3.5.0
pytest_3_4: attrs==19.1.0

pytest_3_5: pytest<3.6.0
pytest_3_6: pytest<3.7.0
pytest_3_7: pytest<3.8.0
pytest_3_8: pytest<3.9.0
pytest_3_9: pytest<3.10.0
pytest_3_10: pytest<4.0.0

pytest_4_0: pytest<4.1.0
pytest_4_0: attrs==19.1.0

pytest_4_1: pytest<4.2.0
pytest_4_1: attrs==19.1.0

pytest_4_2: pytest<4.3.0
pytest_4_2: attrs==19.1.0

pytest_4_3: pytest<4.4.0
pytest_4_3: attrs==19.1.0

pytest_4_4: pytest<4.5.0
pytest_4_4: attrs==19.1.0

pytest_4_5: pytest<4.6.0
pytest_4_5: attrs==19.1.0

pytest_4_6: pytest<5.0.0
pytest_4_6: attrs==19.1.0

pytest_5_0: pytest<5.1.0
pytest_5_0: attrs==19.1.0

pytest_5_1: pytest<5.2.0
pytest_5_1: attrs==19.1.0

pytest_last: pytest

[testenv:flake8]
Expand Down

0 comments on commit b1837ab

Please sign in to comment.