pax_global_header00006660000000000000000000000064136153357230014522gustar00rootroot0000000000000052 comment=986df35d2e44be5a5904e9ffe00a516c5e4f3b1e pytest-lazy-fixture-0.6.3/000077500000000000000000000000001361533572300155215ustar00rootroot00000000000000pytest-lazy-fixture-0.6.3/.gitignore000066400000000000000000000015141361533572300175120ustar00rootroot00000000000000# Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] *$py.class # C extensions *.so # Distribution / packaging .Python env/ build/ develop-eggs/ dist/ downloads/ eggs/ .eggs/ lib/ lib64/ parts/ sdist/ var/ *.egg-info/ .installed.cfg *.egg # PyInstaller # Usually these files are written by a python script from a template # before PyInstaller builds the exe, so as to inject date/other infos into it. *.manifest *.spec # Installer logs pip-log.txt pip-delete-this-directory.txt # Unit test / coverage reports htmlcov/ .tox/ .coverage .coverage.* .cache nosetests.xml coverage.xml *,cover .hypothesis/ # Translations *.mo *.pot # Django stuff: *.log local_settings.py # Flask instance folder instance/ # Sphinx documentation docs/_build/ # PyBuilder target/ # IPython Notebook .ipynb_checkpoints # pyenv .python-version pytest-lazy-fixture-0.6.3/.travis.yml000066400000000000000000000005721361533572300176360ustar00rootroot00000000000000# Config file for automatic testing at travis-ci.org sudo: false language: python python: - "2.7" - "3.4" - "3.5" - "3.6" - "3.7" - "pypy" install: - pip install tox - "TOX_ENV=${TRAVIS_PYTHON_VERSION/[0-9].[0-9]/py${TRAVIS_PYTHON_VERSION/.}}" script: tox -e $TOX_ENV before_cache: - rm -rf $HOME/.cache/pip/log cache: directories: - $HOME/.cache/pip pytest-lazy-fixture-0.6.3/LICENSE000066400000000000000000000020721361533572300165270ustar00rootroot00000000000000 The MIT License (MIT) Copyright (c) 2016 Marsel Zaripov Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. pytest-lazy-fixture-0.6.3/MANIFEST.in000066400000000000000000000001751361533572300172620ustar00rootroot00000000000000include LICENSE include README.md recursive-include tests *.py recursive-exclude * __pycache__ recursive-exclude * *.py[co] pytest-lazy-fixture-0.6.3/README.rst000066400000000000000000000035501361533572300172130ustar00rootroot00000000000000pytest-lazy-fixture |travis-ci| |appveyor| |pypi| ================================================= Use your fixtures in ``@pytest.mark.parametrize``. Installation ------------ .. code-block:: shell pip install pytest-lazy-fixture Usage ----- .. code-block:: python import pytest @pytest.fixture(params=[1, 2]) def one(request): return request.param @pytest.mark.parametrize('arg1,arg2', [ ('val1', pytest.lazy_fixture('one')), ]) def test_func(arg1, arg2): assert arg2 in [1, 2] Also you can use it as a parameter in ``@pytest.fixture``: .. code-block:: python import pytest @pytest.fixture(params=[ pytest.lazy_fixture('one'), pytest.lazy_fixture('two') ]) def some(request): return request.param @pytest.fixture def one(): return 1 @pytest.fixture def two(): return 2 def test_func(some): assert some in [1, 2] Please see `tests `_ for more examples. Contributing ------------ Contributions are very welcome. Tests can be run with ``tox``. License ------- Distributed under the terms of the ``MIT`` license, ``pytest-lazy-fixture`` is free and open source software Issues ------ If you encounter any problems, please ``file an issue`` along with a detailed description. .. |travis-ci| image:: https://travis-ci.org/TvoroG/pytest-lazy-fixture.svg?branch=master :target: https://travis-ci.org/TvoroG/pytest-lazy-fixture .. |appveyor| image:: https://ci.appveyor.com/api/projects/status/github/TvoroG/pytest-fixture-mark?branch=master&svg=true :target: https://ci.appveyor.com/project/TvoroG/pytest-fixture-mark .. |pypi| image:: https://badge.fury.io/py/pytest-lazy-fixture.svg :target: https://pypi.python.org/pypi/pytest-lazy-fixture/ pytest-lazy-fixture-0.6.3/appveyor.yml000066400000000000000000000015271361533572300201160ustar00rootroot00000000000000# What Python version is installed where: # http://www.appveyor.com/docs/installed-software#python environment: matrix: - PYTHON: "C:\\Python27" TOX_ENV: "py27" - PYTHON: "C:\\Python34" TOX_ENV: "py34" - PYTHON: "C:\\Python35" TOX_ENV: "py35" - PYTHON: "C:\\Python36" TOX_ENV: "py36" - PYTHON: "C:\\Python37" TOX_ENV: "py37" init: - "%PYTHON%/python -V" - "%PYTHON%/python -c \"import struct;print( 8 * struct.calcsize(\'P\'))\"" install: - "%PYTHON%/Scripts/easy_install -U pip" - "%PYTHON%/Scripts/pip install tox" - "%PYTHON%/Scripts/pip install wheel" build: false # Not a C# project, build stuff at the test step instead. test_script: - "%PYTHON%/Scripts/tox -e %TOX_ENV%" after_test: - "%PYTHON%/python setup.py bdist_wheel" - ps: "ls dist" artifacts: - path: dist\* pytest-lazy-fixture-0.6.3/pytest_lazyfixture.py000066400000000000000000000131441361533572300220740ustar00rootroot00000000000000# -*- coding: utf-8 -*- import copy import sys import types from collections import defaultdict import pytest PY3 = sys.version_info[0] == 3 string_type = str if PY3 else basestring def pytest_configure(): pytest.lazy_fixture = lazy_fixture @pytest.hookimpl(tryfirst=True) def pytest_runtest_setup(item): if hasattr(item, '_request'): item._request._fillfixtures = types.MethodType( fillfixtures(item._request._fillfixtures), item._request ) def fillfixtures(_fillfixtures): def fill(request): item = request._pyfuncitem 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 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 @pytest.hookimpl(tryfirst=True) def pytest_fixture_setup(fixturedef, request): val = getattr(request, 'param', None) if is_lazy_fixture(val): request.param = request.getfixturevalue(val.name) def pytest_runtest_call(item): if hasattr(item, 'funcargs'): for arg, val in item.funcargs.items(): if is_lazy_fixture(val): item.funcargs[arg] = item._request.getfixturevalue(val.name) @pytest.hookimpl(hookwrapper=True) def pytest_pycollect_makeitem(collector, name, obj): global current_node current_node = collector yield current_node = None def pytest_make_parametrize_id(config, val, argname): if is_lazy_fixture(val): return val.name @pytest.hookimpl(hookwrapper=True) def pytest_generate_tests(metafunc): yield normalize_metafunc_calls(metafunc, 'funcargs') normalize_metafunc_calls(metafunc, 'params') def normalize_metafunc_calls(metafunc, valtype, used_keys=None): newcalls = [] for callspec in metafunc._calls: calls = normalize_call(callspec, metafunc, valtype, used_keys) newcalls.extend(calls) metafunc._calls = newcalls def copy_metafunc(metafunc): copied = copy.copy(metafunc) copied.fixturenames = copy.copy(metafunc.fixturenames) copied._calls = [] try: copied._ids = copy.copy(metafunc._ids) except AttributeError: # pytest>=5.3.0 pass copied._arg2fixturedefs = copy.copy(metafunc._arg2fixturedefs) return copied def normalize_call(callspec, metafunc, valtype, used_keys): fm = metafunc.config.pluginmanager.get_plugin('funcmanage') used_keys = used_keys or set() valtype_keys = set(getattr(callspec, valtype).keys()) - used_keys for arg in valtype_keys: val = getattr(callspec, valtype)[arg] if is_lazy_fixture(val): try: _, fixturenames_closure, arg2fixturedefs = fm.getfixtureclosure([val.name], metafunc.definition.parent) except ValueError: # 3.6.0 <= pytest < 3.7.0; `FixtureManager.getfixtureclosure` returns 2 values fixturenames_closure, arg2fixturedefs = fm.getfixtureclosure([val.name], metafunc.definition.parent) except AttributeError: # pytest < 3.6.0; `Metafunc` has no `definition` attribute fixturenames_closure, arg2fixturedefs = fm.getfixtureclosure([val.name], current_node) extra_fixturenames = [fname for fname in fixturenames_closure if fname not in callspec.params and fname not in callspec.funcargs] newmetafunc = copy_metafunc(metafunc) newmetafunc.fixturenames = extra_fixturenames newmetafunc._arg2fixturedefs.update(arg2fixturedefs) newmetafunc._calls = [callspec] fm.pytest_generate_tests(newmetafunc) normalize_metafunc_calls(newmetafunc, valtype, used_keys | set([arg])) return newmetafunc._calls used_keys.add(arg) return [callspec] def sorted_by_dependency(params, fixturenames): free_fm = [] non_free_fm = defaultdict(list) for key in _sorted_argnames(params, fixturenames): val = params.get(key) if key not in params or not is_lazy_fixture(val) or val.name not in params: free_fm.append(key) else: non_free_fm[val.name].append(key) non_free_fm_list = [] for free_key in free_fm: non_free_fm_list.extend( _tree_to_list(non_free_fm, free_key) ) return [(key, params.get(key)) for key in (free_fm + non_free_fm_list)] def _sorted_argnames(params, fixturenames): argnames = set(params.keys()) for name in fixturenames: if name in argnames: argnames.remove(name) yield name if argnames: for name in argnames: yield name def _tree_to_list(trees, leave): lst = [] for l in trees[leave]: lst.append(l) lst.extend( _tree_to_list(trees, l) ) return lst def lazy_fixture(names): if isinstance(names, string_type): return LazyFixture(names) else: return [LazyFixture(name) for name in names] def is_lazy_fixture(val): return isinstance(val, LazyFixture) class LazyFixture(object): def __init__(self, name): self.name = name def __repr__(self): return '<{} "{}">'.format(self.__class__.__name__, self.name) def __eq__(self, other): return self.name == other.name pytest-lazy-fixture-0.6.3/requirements.txt000066400000000000000000000000201361533572300207750ustar00rootroot00000000000000-e . tox flake8 pytest-lazy-fixture-0.6.3/setup.cfg000066400000000000000000000000671361533572300173450ustar00rootroot00000000000000[flake8] max-line-length = 120 ignore = F821 W503 W504 pytest-lazy-fixture-0.6.3/setup.py000077500000000000000000000031471361533572300172430ustar00rootroot00000000000000#!/usr/bin/env python # -*- coding: utf-8 -*- import os import codecs from setuptools import setup def read(fname): file_path = os.path.join(os.path.dirname(__file__), fname) return codecs.open(file_path, encoding='utf-8').read() setup( name='pytest-lazy-fixture', version='0.6.3', author='Marsel Zaripov', author_email='marszaripov@gmail.com', maintainer='Marsel Zaripov', maintainer_email='marszaripov@gmail.com', license='MIT', url='https://github.com/tvorog/pytest-lazy-fixture', description='It helps to use fixtures in pytest.mark.parametrize', long_description=read('README.rst'), py_modules=['pytest_lazyfixture'], install_requires=['pytest>=3.2.5'], classifiers=[ 'Development Status :: 4 - Beta', 'Framework :: Pytest', 'Intended Audience :: Developers', 'Topic :: Software Development :: Testing', 'Programming Language :: Python', 'Programming Language :: Python :: 2', 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 3', 'Programming Language :: Python :: 3.4', 'Programming Language :: Python :: 3.5', 'Programming Language :: Python :: 3.6', 'Programming Language :: Python :: 3.7', 'Programming Language :: Python :: Implementation :: CPython', 'Programming Language :: Python :: Implementation :: PyPy', 'Operating System :: OS Independent', 'License :: OSI Approved :: MIT License', ], entry_points={ 'pytest11': [ 'lazy-fixture = pytest_lazyfixture', ], }, ) pytest-lazy-fixture-0.6.3/tests/000077500000000000000000000000001361533572300166635ustar00rootroot00000000000000pytest-lazy-fixture-0.6.3/tests/conftest.py000066400000000000000000000000341361533572300210570ustar00rootroot00000000000000pytest_plugins = 'pytester' pytest-lazy-fixture-0.6.3/tests/test_lazyfixture.py000066400000000000000000000631161361533572300226710ustar00rootroot00000000000000# -*- coding: utf-8 -*- import pytest from pytest_lazyfixture import sorted_by_dependency, lazy_fixture, _sorted_argnames try: import numpy except ImportError: numpy = None def test_fixture_in_parametrize_with_params(testdir): items = testdir.getitems(""" import pytest @pytest.fixture(params=[1,2]) def one(request): return request.param @pytest.mark.parametrize('arg1,arg2', [ ('val1', pytest.lazy_fixture('one')), ('val1', 'val2') ]) def test_func(arg1, arg2): pass """) assert len(items) == 3 assert items[0].callspec.params['one'] == 1 assert items[1].callspec.params['one'] == 2 def test_several_fixtures_in_parametrize_with_params(testdir): items = testdir.getitems(""" import pytest @pytest.fixture(params=[1,2]) def one(request): return request.param @pytest.fixture(params=[3,4]) def two(request): return request.param @pytest.mark.parametrize('arg1,arg2,arg3', [ ('val1', pytest.lazy_fixture('one'), pytest.lazy_fixture('two')), ]) def test_func(arg1, arg2, arg3): pass """) assert len(items) == 4 expected_results = [ {'one': 1, 'two': 3}, {'one': 1, 'two': 4}, {'one': 2, 'two': 3}, {'one': 2, 'two': 4} ] def is_subset(subset, superset): return all(superset[k] == subset[k] for k in subset) for item in items: assert any(is_subset(result, item.callspec.params) for result in expected_results) def test_fixtures_in_parametrize_with_indirect(testdir): items = testdir.getitems(""" import pytest @pytest.fixture def one(): pass @pytest.fixture def two(): pass @pytest.mark.parametrize('arg1,one', [ ('val1', pytest.lazy_fixture('two')), ], indirect=['one']) def test_func(arg1, one): pass """) assert len(items) == 1 assert items[0].callspec.params['one'].name == 'two' def test_fixtures_with_params_in_parametrize_with_indirect(testdir): items = testdir.getitems(""" import pytest @pytest.fixture def one(): pass @pytest.fixture(params=[1,2]) def two(request): return request.param @pytest.mark.parametrize('arg1,one', [ ('val1', pytest.lazy_fixture('two')), ], indirect=['one']) def test_func(arg1, one): pass """) assert len(items) == 2 assert items[0].callspec.params['two'] == 1 assert items[1].callspec.params['two'] == 2 def test_lazy_fixture_is_value_in_parametrize(testdir): testdir.makepyfile(""" import pytest @pytest.fixture def one(): return 1 @pytest.fixture def two(): return 2 @pytest.mark.parametrize('arg1,arg2', [ pytest.lazy_fixture(('one', 'two')) ]) def test_func(arg1, arg2): assert arg1 == 1 assert arg2 == 2 """) reprec = testdir.inline_run('-s') reprec.assertoutcome(passed=1) def test_lazy_fixture_as_funcarg_in_parametrize_with_indirect(testdir): testdir.makepyfile(""" import pytest @pytest.fixture def one(): return 1 @pytest.fixture def two(): return 2 @pytest.fixture def three(request): return request.param @pytest.mark.parametrize('arg1,arg2,three', [ (pytest.lazy_fixture('one'), pytest.lazy_fixture('two'), '3') ], indirect=['three']) def test_func(arg1, arg2, three): assert arg1 == 1 assert arg2 == 2 assert three == '3' """) reprec = testdir.inline_run('-s') reprec.assertoutcome(passed=1) def test_lazy_fixture_is_value_in_parametrize_with_indirect(testdir): testdir.makepyfile(""" import pytest @pytest.fixture def one(request): return request.param @pytest.fixture def two(): return 2 @pytest.mark.parametrize('one', [ pytest.lazy_fixture('two') ], indirect=True) def test_func(one): assert one == 2 """) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) def test_lazy_fixture_as_param_of_fixture(testdir): testdir.makepyfile(""" import pytest @pytest.fixture(params=[ pytest.lazy_fixture('one'), pytest.lazy_fixture('two') ]) def some(request): return request.param @pytest.fixture def one(): return 1 @pytest.fixture def two(): return 2 def test_func(some): assert some in [1, 2] """) reprec = testdir.inline_run('-s') reprec.assertoutcome(passed=2) def test_lazy_fixture_in_params_which_has_params(testdir): testdir.makepyfile(""" import pytest @pytest.fixture(params=[1, 2, 3]) def one(request): return str(request.param) @pytest.fixture def two(): return 4 @pytest.fixture(params=[ pytest.lazy_fixture('one'), pytest.lazy_fixture('two') ]) def some(request): return request.param def test_func(some): assert some in {'1', '2', '3', 4} """) reprec = testdir.inline_run('-s') reprec.assertoutcome(passed=4) def test_lazy_fixture_three_times_nested(testdir): testdir.makepyfile(""" import pytest @pytest.fixture(params=[ 1, 2, pytest.lazy_fixture('three')]) def one(request): return str(request.param) @pytest.fixture def two(): return 4 @pytest.fixture def three(): return 3 @pytest.fixture(params=[ pytest.lazy_fixture('one'), pytest.lazy_fixture('two') ]) def some(request): return request.param def test_func(some): assert some in {'1', '2', '3', 4} """) reprec = testdir.inline_run('-s') reprec.assertoutcome(passed=4) def test_lazy_fixture_three_times_nested_with_one_failed(testdir): testdir.makepyfile(""" import pytest @pytest.fixture(params=[ 1, 2, pytest.lazy_fixture('three') ]) def one(request): return str(request.param) @pytest.fixture def two(): return 4 @pytest.fixture def three(): return 5 @pytest.fixture(params=[ pytest.lazy_fixture('one'), pytest.lazy_fixture('two') ]) def some(request): return request.param def test_func(some): assert some in {'1', '2', '3', 4} """) reprec = testdir.inline_run('-s') reprec.assertoutcome(passed=3, failed=1) def test_lazy_fixture_common_dependency(testdir): testdir.makepyfile(""" import pytest @pytest.fixture(params=[1, 2, 3]) def one(request): return request.param @pytest.fixture(params=[pytest.lazy_fixture('one')]) def as_str(request): return str(request.param) @pytest.fixture(params=[pytest.lazy_fixture('one')]) def as_hex(request): return hex(request.param) def test_as_str(as_str): assert as_str in {'1', '2', '3'} def test_as_hex(as_hex): assert as_hex in {'0x1', '0x2', '0x3'} def test_as_hex_vs_as_str(as_str, as_hex): assert int(as_hex, 16) == int(as_str) """) reprec = testdir.inline_run('-s') reprec.assertoutcome(passed=9) def test_lazy_fixture_common_dependency_with_getfixturevalue(testdir): testdir.makepyfile(""" import pytest @pytest.fixture(params=[1, 2, 3]) def one(request): return request.param @pytest.fixture(params=[pytest.lazy_fixture('one')]) def as_str(request): return str(request.getfixturevalue('one')) @pytest.fixture(params=[pytest.lazy_fixture('one')]) def as_hex(request): return hex(request.getfixturevalue('one')) def test_as_str(as_str): assert as_str in {'1', '2', '3'} def test_as_hex(as_hex): assert as_hex in {'0x1', '0x2', '0x3'} def test_as_hex_vs_as_str(as_str, as_hex): assert int(as_hex, 16) == int(as_str) """) reprec = testdir.inline_run('-s') reprec.assertoutcome(passed=9) def test_issues2(testdir): testdir.makepyfile(""" import pytest @pytest.fixture(params=[1, 2, 3]) def one(request): return request.param @pytest.fixture(params=[pytest.lazy_fixture('one')]) def as_str(request): return str(request.getfixturevalue('one')) @pytest.mark.parametrize('val', ('a', 'b', 'c')) def test_as_str(val, as_str): combined = ''.join((val, as_str)) assert combined in {'a1', 'a2', 'a3', 'b1', 'b2', 'b3', 'c1', 'c2', 'c3'} """) reprec = testdir.inline_run('-s') reprec.assertoutcome(passed=9) def test_issues2_2(testdir): testdir.makepyfile(""" import pytest @pytest.fixture(params=[1, 2, 3]) def one(request): return request.param @pytest.fixture(params=[pytest.lazy_fixture('one')]) def as_str(request): return str(request.getfixturevalue('one')) @pytest.mark.parametrize('val, one', ( ('a', '1'), ('b', '2'), ('c', '3') ), indirect=['one']) def test_as_str(val, one, as_str): combined = ''.join((val, as_str)) assert combined in {'a1', 'b2', 'c3'} """) reprec = testdir.inline_run('-s') reprec.assertoutcome(passed=3) def test_issues3_autouse_fixtures_should_run_first(testdir): testdir.makepyfile(""" import pytest gl = False @pytest.fixture(autouse=True) def auto_one(): global gl gl = True @pytest.fixture def one(): return 1 if gl is True else -1 @pytest.mark.parametrize('arg1', [ pytest.lazy_fixture('one') ]) def test_some(arg1): assert arg1 == 1 """) reprec = testdir.inline_run('-s') reprec.assertoutcome(passed=1) def test_issues10_xfail(testdir): testdir.makepyfile(""" import pytest def division(a, b): return a / b @pytest.fixture(params=[0]) def zero(request): return request.param @pytest.mark.parametrize(('a', 'b'), [ pytest.param(1, pytest.lazy_fixture('zero'), marks=pytest.mark.xfail(reason=ZeroDivisionError)) ]) def test_division(a, b): division(a, b) """) reprec = testdir.inline_run('-s', '-v') reprec.assertoutcome(skipped=1) def test_issues11_autouse_fixture_in_test_class(testdir): testdir.makepyfile(""" import pytest class TestModels(object): @pytest.fixture(autouse=True) def setup(self): self.var = 15 def test_model_a(self): assert self.var == 15 def test_model_b(self): assert self.var == 15 """) reprec = testdir.inline_run('-s', '-v') reprec.assertoutcome(passed=2) def test_issues12_skip_test_function(testdir): testdir.makepyfile(""" import pytest @pytest.fixture def one(): return 1 @pytest.mark.parametrize('a', [ pytest.param(pytest.lazy_fixture('one'), marks=pytest.mark.skip(reason='skip')) ]) def test_skip1(a): assert a == 1 @pytest.mark.skip(reason='skip') @pytest.mark.parametrize('a', [ pytest.lazy_fixture('one') ]) def test_skip2(a): assert a == 1 def test_after_skip(one): assert one == 1 """) reprec = testdir.inline_run('-s', '-v') reprec.assertoutcome(skipped=2, passed=1) def test_issues12_skip_test_method(testdir): testdir.makepyfile(""" import pytest class TestModels: @pytest.fixture def one(self): return 1 @pytest.mark.skip(reason='skip this') @pytest.mark.parametrize('a', [ pytest.lazy_fixture('one') ]) def test_model_a(self, a): assert a == 1 @pytest.mark.parametrize('a', [ pytest.param(pytest.lazy_fixture('one'), marks=pytest.mark.skip(reason='skip this')) ]) def test_model_b(self, a): assert a == 1 def test_after_skip(self, one): assert one == 1 """) reprec = testdir.runpytest('-s', '-v') reprec.assert_outcomes(skipped=2, passed=1) def test_issues12_lf_as_method_of_test_class(testdir): testdir.makepyfile(""" import pytest class TestModels: @pytest.fixture def one(self): return 1 @pytest.mark.parametrize('a', [ pytest.lazy_fixture('one') ]) def test_lf(self, a): assert a == 1 """) reprec = testdir.inline_run('-s', '-v') reprec.assertoutcome(passed=1) def test_issues13_unittest_testcase_class_should_not_fail(testdir): testdir.makepyfile(""" import unittest import pytest class TestModels(unittest.TestCase): def test_models(self): assert True def test_models_fail(self): assert False """) reprec = testdir.inline_run('-s', '-v') reprec.assertoutcome(passed=1, failed=1) def test_argnames_initialized_in_right_order(testdir): testdir.makepyfile(""" import pytest @pytest.fixture def one(): return [1] @pytest.fixture def plus_two(a): a[0] = a[0] + 2 @pytest.mark.parametrize('a,b', [ (pytest.lazy_fixture('one'), pytest.lazy_fixture('plus_two')) ]) def test_skip1(a, b): assert a == [3] """) reprec = testdir.inline_run('-s', '-v') reprec.assertoutcome(passed=1) # https://github.com/TvoroG/pytest-lazy-fixture/pull/19 def test_argnames_initialized_in_right_order2(testdir): testdir.makepyfile(""" import pytest @pytest.fixture def one(): return [1] @pytest.fixture def plus_two(a): a[0] = a[0] + 2 def test_skip1(a): assert a == [3] def pytest_generate_tests(metafunc): metafunc.fixturenames = ['a', 'b'] metafunc.parametrize(argnames=['a', 'b'], argvalues=[(pytest.lazy_fixture('one'), pytest.lazy_fixture('plus_two'))], indirect=['b']) """) reprec = testdir.inline_run('-s', '-v') reprec.assertoutcome(passed=1) def lf(fname): return lazy_fixture(fname) @pytest.mark.parametrize('params,expected_paths', [ ( {'some': lf('one'), 'one': lf('three')}, ['one>some'], ), ( {'grand1': lf('parent1_1'), 'parent1_1': lf('child1'), 'grand2': lf('parent1_2'), 'parent1_2': lf('child1'), 'child1': lf('none')}, ['child1>parent1_1>grand1>parent1_2>grand2', 'child1>parent1_2>grand2>parent1_1>grand1'] ), ( {'param1': 'val1', 'param2': 'val2'}, ['param1>param2', 'param2>param1'] ), ({}, ['']), ({'param1': 'val1'}, ['param1']), ({'param1': lf('some')}, ['param1']), ( {'one': 1, 'as_str': lf('one'), 'as_hex': lf('one')}, ['one>as_str>as_hex', 'one>as_hex>as_str'] ) ]) def test_sorted_by_dependency(params, expected_paths): sp = sorted_by_dependency(params, []) path = '>'.join(param for param, _ in sp) assert path in expected_paths @pytest.mark.parametrize('params,fixturenames,expect_keys', [ ({'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 def test_lazy_fixtures_with_subfixtures(testdir): testdir.makepyfile(""" import pytest @pytest.fixture(params=["a", "A"]) def a(request): return request.param @pytest.fixture(params=["b", "B"]) def b(a, request): return request.param + a @pytest.fixture def c(a): return "c" + a @pytest.fixture(params=[pytest.lazy_fixture('a'), pytest.lazy_fixture('b'), pytest.lazy_fixture('c')]) def d(request): return "d" + request.param @pytest.fixture(params=[pytest.lazy_fixture('a'), pytest.lazy_fixture('d'), ""]) def e(request): return "e" + request.param def test_one(d): assert d in ("da", "dA", "dba", "dbA", "dBa", "dBA", "dca", "dcA") def test_two(e): assert e in ("ea", "eA", "eda", "edA", "edba", "edbA", "edBa", "edBA", "edca", "edcA", "e") """) reprec = testdir.inline_run('-s', '-v') reprec.assertoutcome(passed=19) def test_lazy_fixtures_in_subfixture(testdir): testdir.makepyfile(""" import pytest @pytest.fixture def a(): return "a" @pytest.fixture def b(): return "b" @pytest.fixture(params=[pytest.lazy_fixture('a'), pytest.lazy_fixture('b')]) def c(request): return "c" + request.param @pytest.fixture def d(c): return "d" + c def test_one(d): assert d in ("dca", "dcb") """) reprec = testdir.inline_run('-s', '-v') reprec.assertoutcome(passed=2) @pytest.mark.parametrize('autouse', [False, True]) def test_issues23(testdir, autouse): testdir.makepyfile(""" import pytest @pytest.fixture(params=[0, 1], autouse={}) def zero(request): return request.param @pytest.fixture(params=[1]) def one(request, zero): return zero * request.param @pytest.fixture(params=[ pytest.lazy_fixture('one'), ]) def some(request): return request.param def test_func(some): assert some in [0, 1] """.format(autouse)) reprec = testdir.inline_run('-s', '-v') reprec.assertoutcome(passed=2) def test_lazy_fixture_nested_fixtures(testdir): testdir.makepyfile(""" import pytest @pytest.fixture def one(request): return "SOME_VALUE" @pytest.fixture def two(request): return "SOME_VALUE2" @pytest.fixture(params=[ pytest.lazy_fixture("one"), pytest.lazy_fixture("two"), ]) def some_fixture1(request): return request.param @pytest.fixture def some_fixture2(some_fixture1): return "NEW_" + some_fixture1 def test_func(some_fixture2): assert ((some_fixture2 == "NEW_SOME_VALUE") or (some_fixture2 == "NEW_SOME_VALUE2")) """) 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 ) # https://github.com/TvoroG/pytest-lazy-fixture/issues/42 @pytest.mark.skipif(numpy is None, reason='numpy is not installed') def test_numpy_array_as_value(testdir): testdir.makepyfile(""" import pytest import numpy as np @pytest.mark.parametrize( 'value', [ np.arange(10, dtype=np.int64), np.arange(10, dtype=np.int32), ] ) def test_bug(value): assert isinstance(value, np.ndarray) """) result = testdir.inline_run('-s') result.assertoutcome(passed=2) # https://github.com/TvoroG/pytest-lazy-fixture/issues/46 def test_lazy_fixture_ids(testdir): testdir.makepyfile(""" import pytest from pytest_lazyfixture import lazy_fixture @pytest.fixture() def foo(): return "foo" @pytest.fixture(params=['spam', 'eggs']) def bar(request): return "bar-{}".format(request.param) @pytest.mark.parametrize("data", [lazy_fixture("foo"), lazy_fixture("bar")]) def test_the_thing(data): assert False """) result = testdir.runpytest('--collect-only') stdout = result.stdout.str() assert 'test_the_thing[foo]' in stdout assert 'test_the_thing[bar-spam]' in stdout assert 'test_the_thing[bar-eggs]' in stdout pytest-lazy-fixture-0.6.3/tox.ini000066400000000000000000000032271361533572300170400ustar00rootroot00000000000000# For more information about tox, see https://tox.readthedocs.io/en/latest/ [tox] envlist = {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,5_1,5_2,5_3} 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_5_2: pytest<5.3.0 pytest_5_2: attrs==19.1.0 pytest_5_3: pytest<5.4.0 pytest_5_3: attrs==19.1.0 numpy==1.16.5 [testenv:flake8] skip_install = true deps = flake8 commands = flake8 pytest_lazyfixture.py setup.py tests [testenv:pytest] deps = -egit+https://github.com/pytest-dev/pytest.git#egg=pytest tox hypothesis>=3.5.2 nose mock requests xmlschema changedir = {envdir}/src/pytest commands = pytest --lsof -rfsxX