from pytest_cases.plugin import SuperClosure from pytest_cases import fixture, fixture_union @fixture(autouse=True) def a(): return def test_issue116(request): normal_closure = request._pyfuncitem._fixtureinfo.names_closure assert isinstance(normal_closure, list) normal_closure.remove('a') b = fixture_union('b', [a, a]) super_closure = None def test_super_closure_edits(request, b): # save for later global super_closure super_closure = request._pyfuncitem._fixtureinfo.names_closure def test_super_closure_edits2(): global super_closure assert isinstance(super_closure, SuperClosure) super_closure = copy(super_closure) assert len(super_closure) == 4
return fixture_fun @fixture(hook=my_hook) def foo(): return 2, 1 o, p = unpack_fixture('o,p', foo, hook=my_hook) p1 = param_fixture("p1", [1, 2], hook=my_hook) p2, p3 = param_fixtures("p2,p3", [(3, 4)], hook=my_hook) u = fixture_union("u", (o, p), hook=my_hook) @parametrize("arg", [fixture_ref(u), fixture_ref(p1)]) def test_a(arg, p2, p3): pass def test_synthesis(module_results_dct): assert list(module_results_dct) == [ 'test_a[u-/o-3-4]', 'test_a[u-/p-3-4]', 'test_a[p1-1-3-4]', 'test_a[p1-2-3-4]' ]
# Authors: Sylvain MARIE <*****@*****.**> # + All contributors to <https://github.com/smarie/python-pytest-cases> # # License: 3-clause BSD, <https://github.com/smarie/python-pytest-cases/blob/master/LICENSE> from pytest_cases import param_fixture, fixture_union a = param_fixture("a", [1, 2]) b = param_fixture("b", [3, 4]) my_explicit = fixture_union('my_explicit', ['a', b], idstyle='explicit') my_compact = fixture_union('my_compact', ['a', b]) # , idstyle='compact' is the default now my_none = fixture_union('my_none', ['a', b], idstyle=None) my_custom_list = fixture_union('my_custom_list', ['a', b], ids=['c=A', 'c=B']) def my_gen(o): return str(o) my_custom_gen = fixture_union('my_custom_gen', ['a', b], ids=my_gen) class TestA: def test_ids_explicit(self, my_explicit): pass def test_ids_compact(my_compact): pass
from pytest_cases import param_fixture, fixture_union, pytest_fixture_plus a = param_fixture('a', ['x', 'y']) @pytest_fixture_plus(params=[1, 2]) def b(request): # make sure that if this is called, then it is for a good reason assert request.param in [1, 2] return request.param c = fixture_union('c', [a, b]) def test_fixture_union(c, a): print(c, a) def test_synthesis(module_results_dct): assert list(module_results_dct) == ["test_fixture_union[c_is_a-x]", "test_fixture_union[c_is_a-y]", "test_fixture_union[c_is_b-1-x]", "test_fixture_union[c_is_b-1-y]", "test_fixture_union[c_is_b-2-x]", "test_fixture_union[c_is_b-2-y]"]
monkeypatch.setitem(implementation_registry, "s3", local_s3_implementation) rig = CloudProviderTestRig( path_class=LocalS3Path, client_class=LocalS3Client, drive=drive, test_dir=test_dir, ) rig.client_class().set_as_default_client() # set default client yield rig rig.client_class._default_client = None # reset default client rig.client_class.reset_default_storage_dir( ) # reset local storage directory rig = fixture_union( "rig", [ azure_rig, gs_rig, s3_rig, local_azure_rig, local_s3_rig, local_gs_rig, ], )
from pytest_cases import param_fixture, fixture_union a = param_fixture("a", [1, 2]) b = param_fixture("b", [3, 4]) c = fixture_union('c', ['a', b], ids=['A', 'B'], idstyle='explicit') d = fixture_union('d', ['a'], idstyle='compact') e = fixture_union('e', ['a'], idstyle=None) def test_the_ids(c, d, e): pass def test_synthesis(module_results_dct): assert list(module_results_dct) == [ 'test_the_ids[c_is_A-1-Ua-a]', 'test_the_ids[c_is_A-2-Ua-a]', 'test_the_ids[c_is_B-3-Ua-1-a]', 'test_the_ids[c_is_B-3-Ua-2-a]', 'test_the_ids[c_is_B-4-Ua-1-a]', 'test_the_ids[c_is_B-4-Ua-2-a]', ]
def root1(o): return o, o[0] def test_function(a, b): assert a[0] == b assert a in ('hello', 'world') @pytest_fixture_plus @pytest.mark.parametrize("o", ['yeepee', 'yay']) def root2(o): return o, o[0] fixture_union("root", [root1, root2], unpack_into="c,d") def test_function2(c, d): assert c[0] == d assert c in ('hello', 'world', 'yeepee', 'yay') def test_synthesis(module_results_dct): assert list(module_results_dct) == ['test_function[hello]', 'test_function[world]', 'test_function2[root_is_root1-hello]', 'test_function2[root_is_root1-world]', 'test_function2[root_is_root2-yeepee]', 'test_function2[root_is_root2-yay]', ]
@pytest_cases.fixture() def rnd_dense_spd_mat(n_cols: int, rng: np.random.Generator) -> np.ndarray: """Random spd matrix generated from :meth:`random_spd_matrix`.""" return random_spd_matrix(rng=rng, dim=n_cols) @pytest_cases.fixture() def rnd_sparse_spd_mat(n_cols: int, density: float, rng: np.random.Generator) -> scipy.sparse.spmatrix: """Random sparse spd matrix generated from :meth:`random_sparse_spd_matrix`.""" return random_sparse_spd_matrix(rng=rng, dim=n_cols, density=density) rnd_spd_mat = pytest_cases.fixture_union( "spd_mat", [rnd_dense_spd_mat, rnd_sparse_spd_mat]) @pytest.fixture( params=[ pytest.param(namegroup, id=f"{namegroup[0]}") for namegroup in (("wm1", "HB"), ) ], name="suitesparse_mat", ) def fixture_suitesparse_mat(request) -> SuiteSparseMatrix: return suitesparse_matrix(name=request.param[0], group=request.param[1]) @pytest.fixture(name="suitesparse_mycielskian") def fixture_suitesparse_mycielskian() -> SuiteSparseMatrix:
return quant_conv_variant(out_channels=OUTPUT_CH, in_channels=IN_CH, kernel_size=KERNEL_SIZE, bias=bias_enabled) @pytest_cases.fixture() def default_weight_tensor_quant(default_wbiol_layer): """ Returns default_wbiol_layer.weight_quant.tensor_quant """ return default_wbiol_layer.weight_quant.tensor_quant # Union of all WBIOL layers with default quantization settings fixture_union('default_wbiol_layer', ['default_wbiol_quant_linear', 'default_wbiol_quant_conv']) def test_default_wbiol_input_quant_enabled(default_wbiol_layer: QuantWBIOL): assert not default_wbiol_layer.is_input_quant_enabled def test_default_wbiol_output_quant_enabled(default_wbiol_layer: QuantWBIOL): assert not default_wbiol_layer.is_output_quant_enabled def test_default_wbiol_bias_quant_enabled(default_wbiol_layer: QuantWBIOL): assert not default_wbiol_layer.is_bias_quant_enabled def test_default_wbiol_weight_quant_enabled(default_wbiol_layer: QuantWBIOL):
import pytest from pytest_cases import fixture_union @pytest.fixture(params=[2, 3]) def a(): return 'a123' @pytest.fixture(params=[0, 1, 2]) def b(): return 'b321' f_union = fixture_union("f_union", [a, "b"]) def test_fixture_union(f_union): return
except KeyError: module = import_module(module_name) return module, expected_version @pytest_fixture_plus def self_uninstalled_git(): expected_version = get_version(join(getversion.__file__, pardir, pardir)) return getversion, expected_version # Create the union of all cases mod = fixture_union("mod", [ builtin_module_and_submodule, module_with_version_attr, submodule_in_pkg_with_version_attr, installed_pkg_and_module, unzipped_wheel_or_egg, self_uninstalled_git ], unpack_into="module, expected_version") # @pytest_fixture_plus # @cases_data(module=test_main_cases) # def mod(case_data): # return case_data.get() # # # module, expected_version = unpack_fixture("module, expected_version", mod) def test_modules_version(module, expected_version): found_version, detailed_results = get_module_version(module) try:
from distutils.version import LooseVersion import pytest from pytest_cases import fixture_union @pytest.fixture def a(): return 1 u = fixture_union("u", (a, a)) def test_foo(u): pass def test_synthesis(module_results_dct): if LooseVersion(pytest.__version__) < LooseVersion('3.0.0'): # the way to make ids uniques in case of duplicates was different in old pytest assert list(module_results_dct) == [ 'test_foo[0u_is_a]', 'test_foo[1u_is_a]' ] else: assert list(module_results_dct) == [ 'test_foo[u_is_a0]', 'test_foo[u_is_a1]' ]
from pytest_cases import param_fixture, fixture_union # basic parametrized fixtures a = param_fixture('a', ['x', 'y']) b = param_fixture('b', [1, 2]) # union fixtures c = fixture_union('c', [a, b]) d = fixture_union('d', [b, a]) def test_fixture_union_harder(c, a, d): print(c, a, d) def test_synthesis(module_results_dct): assert list(module_results_dct) == [ "test_fixture_union_harder[c_is_a-x-d_is_b-1]", "test_fixture_union_harder[c_is_a-x-d_is_b-2]", "test_fixture_union_harder[c_is_a-x-d_is_a]", "test_fixture_union_harder[c_is_a-y-d_is_b-1]", "test_fixture_union_harder[c_is_a-y-d_is_b-2]", "test_fixture_union_harder[c_is_a-y-d_is_a]", "test_fixture_union_harder[c_is_b-1-x-d_is_b]", "test_fixture_union_harder[c_is_b-1-x-d_is_a]", "test_fixture_union_harder[c_is_b-1-y-d_is_b]", "test_fixture_union_harder[c_is_b-1-y-d_is_a]", "test_fixture_union_harder[c_is_b-2-x-d_is_b]", "test_fixture_union_harder[c_is_b-2-x-d_is_a]", "test_fixture_union_harder[c_is_b-2-y-d_is_b]", "test_fixture_union_harder[c_is_b-2-y-d_is_a]"
""" return BitWidthConst(bit_width_init) @pytest_cases.fixture() def bit_width_parameter_defaults(bit_width_init): """ Learned bit-width with default arguments module """ module = BitWidthParameter(bit_width_init) return module @pytest_cases.fixture() def bit_width_parameter(bit_width_init, min_bit_width_init, override_pretrained): """ Learned bit-width with default arguments module """ if bit_width_init < min_bit_width_init: pytest.xfail('bit_width cannot be smaller than min_bit_width') module = BitWidthParameter( bit_width_init, min_bit_width=min_bit_width_init, override_pretrained_bit_width=override_pretrained) return module # Union of all variants of bit-width fixture_union('bit_width_all', ['bit_width_const', 'bit_width_parameter'])
def test_2(a, i2): assert (a + i2) in ("a0cdx", "a0cdz", "a1cdx", "a1cdz") @fixture # @parametrize(ub=(fixture_ref(a), fixture_ref(c)), ib=['x', 'z']) # >> no: order of kwargs changes across versions # >> AND besides, using **kwargs style with at least a fixture ref creates a fixture for all parametrization even the ones not using fixture_refs # see https://github.com/smarie/python-pytest-cases/issues/118 @parametrize(ib=['x', 'z']) @parametrize(ub=(fixture_ref(a), fixture_ref(c))) def b(ub, ib): return "b%s" % ib + ub u = fixture_union("u", (a, b)) super_closure = None def test_1(u, request): # store for later global super_closure super_closure = request._pyfuncitem.fixturenames def test_closure(): # make sure that the closure tree looks good global super_closure assert str(super_closure) == """SuperClosure with 3 alternative closures: - ['environment', 'e', 'request', 'u', 'a', 'c', 'd'] (filters: u=u[0]=a)
bucket.upload_file( str(test_file), str(f"{test_dir}/{PurePosixPath(test_file.relative_to(assets_dir))}" ), ) else: # Mock cloud SDK monkeypatch.setattr( cloudpathlib.s3.s3client, "Session", mocked_session_class_factory(test_dir), ) rig = CloudProviderTestRig(path_class=S3Path, client_class=S3Client, drive=drive, test_dir=test_dir) rig.client_class().set_as_default_client() # set default client yield rig rig.client_class._default_client = None # reset default client if os.getenv("USE_LIVE_CLOUD") == "1": # Clean up test dir bucket.objects.filter(Prefix=test_dir).delete() rig = fixture_union("rig", [azure_rig, s3_rig])
num_beams=432, ) def _setup_gsf_test_data(src_datafile: GsfDatafile, tmp_path: Path): tmp_path.mkdir(parents=True, exist_ok=True) tmp_datafile_path = tmp_path / src_datafile.path.name shutil.copyfile(src_datafile.path, tmp_datafile_path) yield GsfDatafile(src_datafile.gsf_version, tmp_datafile_path, src_datafile.num_beams) shutil.rmtree(tmp_path, ignore_errors=True) @pytest.fixture def gsf_test_data_03_06(tmp_path): yield from _setup_gsf_test_data(GSF_03_06_DATAFILE, tmp_path) @pytest.fixture def gsf_test_data_03_07(tmp_path): yield from _setup_gsf_test_data(GSF_03_07_DATAFILE, tmp_path) fixture_union("gsf_test_data", [gsf_test_data_03_06, gsf_test_data_03_07]) @pytest.fixture(params=[GSF_03_06_DATAFILE, GSF_03_07_DATAFILE]) def gsf_test_data(request, tmp_path): src_datafile: GsfDatafile = request.param yield from _setup_gsf_test_data(src_datafile, tmp_path)
@fixture @parametrize(ib=['x', 'z']) def b(a, c, ib): return "b%s" % ib + c + a @fixture @parametrize(ib=['x', 'z']) @parametrize(ub=(fixture_ref(a), fixture_ref(c)), idstyle="explicit") def b(ub, ib): return "b%s" % ib + ub u = fixture_union("u", (a, b), idstyle="explicit") def test_1(u): pass @parametrize("e", [-1], indirect=True) def test_synthesis_1(module_results_dct): assert list(module_results_dct) == [ 'test_2[ie=-1-ia=0-i2=x]', 'test_2[ie=-1-ia=0-i2=z]', 'test_2[ie=-1-ia=1-i2=x]', 'test_2[ie=-1-ia=1-i2=z]', 'test_2[ie=1-ia=0-i2=x]', 'test_2[ie=1-ia=0-i2=z]',
@pytest_cases.fixture() def started_round(new_round: Round): new_round.start() return new_round # todo: ongoing_round with autoplay @pytest_cases.fixture() def current_player(started_round): return started_round.current_player game_round = pytest_cases.fixture_union("game_round", [new_round, started_round]) @pytest_cases.fixture() @pytest.mark.parametrize( "players", argvalues=PLAYER_LIST_CASES, ids=lambda x: f"Game(<{len(x)} players>)" ) def new_game(players) -> Game: return Game(players) @pytest_cases.fixture() def started_game(new_game: Game) -> Game: new_game.start() return new_game
from pytest_cases.common_pytest_marks import PYTEST3_OR_GREATER from pytest_cases import fixture_union @pytest.fixture def a(): return 1 @pytest.fixture def b(): return 1 u = fixture_union("u", (a, b, a), ids=['1', '2', '3']) def test_foo(u): pass v = fixture_union("v", (a, b, a)) def test_foo2(v): pass def test_synthesis(module_results_dct): if not PYTEST3_OR_GREATER:
""" return ConstScaling(standalone_scaling_init) @pytest_cases.fixture() def parameter_scaling_impl(standalone_scaling_init): """ Scaling by a parameter implementation """ return ParameterScaling(standalone_scaling_init) @pytest_cases.fixture() @pytest_cases.parametrize('value', [0.001, 5.0]) def standalone_scaling_init(value): """ Value to initialize const/parameter scaling with """ return value @pytest_cases.fixture() def randn_inp(): """ 4-dim randn tensor """ return torch.randn(size=(5, 4, 3, 2)) fixture_union('scaling_impl_all', ['const_scaling_impl', 'parameter_scaling_impl'])
# Authors: Sylvain MARIE <*****@*****.**> # + All contributors to <https://github.com/smarie/python-pytest-cases> # # License: 3-clause BSD, <https://github.com/smarie/python-pytest-cases/blob/master/LICENSE> from pytest_cases import param_fixture, fixture_union, fixture a = param_fixture('a', ['x', 'y']) @fixture(params=[1, 2]) def b(request): # make sure that if this is called, then it is for a good reason assert request.param in [1, 2] return request.param c = fixture_union('c', [a, b], idstyle="explicit") def test_fixture_union(c, a): print(c, a) def test_synthesis(module_results_dct): assert list(module_results_dct) == [ "test_fixture_union[c/a-x]", "test_fixture_union[c/a-y]", "test_fixture_union[c/b-1-x]", "test_fixture_union[c/b-1-y]", "test_fixture_union[c/b-2-x]", "test_fixture_union[c/b-2-y]" ]
def mygen(name): """An infinite generator of ids""" for i in itt.count(): yield "%s{%i}" % (name, i) if LooseVersion(pytest.__version__) < LooseVersion('3.0.0'): @fixture @parametrize("x", [1, 2], ids=mygen("x")) def my_fixture(x): return x a = param_fixtures("a", ['aa', 'ab'], ids=mygen("a")) my_union = fixture_union("my_union", [a], ids=mygen("myunion")) def test_foo(my_fixture, my_union, a): pass def test_synthesis(module_results_dct): assert list(module_results_dct) == [ 'test_foo[x{0}-myunion{0}-a{0}]', 'test_foo[x{0}-myunion{0}-a{1}]', 'test_foo[x{1}-myunion{0}-a{0}]', 'test_foo[x{1}-myunion{0}-a{1}]' ] else: @fixture @parametrize("y", [0, 1], ids=("y{%i}" % i for i in itt.count())) @parametrize("x", [1, 2], ids=mygen("x"))
return "i" * request.param # @fixture(params=[1, 2]) # def upper(request): # return "I" * request.param @pytest.fixture(params=[1, 2]) def upper(request): # Just for the remark: this fixture does not use fixture # so we have to explicitly discard the 'NOT_USED' cases if request.param is not NOT_USED: return "I" * request.param fixture_union('all', ['lower', 'upper']) def test_all(all): print(all) def test_synthesis(module_results_dct): """Use pytest-harvest to check that the list of executed tests is correct """ assert list(module_results_dct) == [ 'test_all[/lower-1]', 'test_all[/lower-2]', 'test_all[/lower-3]', 'test_all[/upper-1]', 'test_all[/upper-2]'
@fixture def a(request): global state assert state == 3 state = 0 yield state = 1 @fixture def b(request): global state state = 2 yield state = 3 c = fixture_union('c', [b, a]) # @fixture # def c(b, a): # pass def test_all(c): pass def test_synthesis(): assert state == 1
has_pytest_param = hasattr(pytest, 'param') # pytest.param is not available in all versions if has_pytest_param: a = param_fixture("a", [1, pytest.param(2, id='22'), pytest.param(3, marks=pytest.mark.skip) ]) b = param_fixture("b", [3, 4]) c = fixture_union('c', [pytest.param('a', id='A'), pytest.param(b, marks=pytest.mark.skip) ], ids=['ignored', 'B'], ) def test_foo(c): pass def test_synthesis(module_results_dct): # TODO most probably the skip mark on b seeems to mess with the union behaviour. assert list(module_results_dct) == [ 'test_foo[A-1]', 'test_foo[A-22]', ]
used[name] = True yield name torn_down[name] += 1 @fixture(scope='function') def F4M2s1s3(M2s1s3): name = 'F4M2s1s3' global used, torn_down assert not used[name] used[name] = True yield name torn_down[name] += 1 d = fixture_union('d', (F1M1s1s2, F2, F3s2s3, F4M2s1s3)) super_closure = None def test_foo(d, request): # store closure for later analysis or test global super_closure super_closure = request._pyfuncitem._fixtureinfo.names_closure def test_synthesis(module_results_dct): assert all(torn_down.values()) assert list(module_results_dct) == [ 'test_foo[d_is_F1M1s1s2]', 'test_foo[d_is_F2-i=0]', 'test_foo[d_is_F2-i=1]', 'test_foo[d_is_F3s2s3]',
# Authors: Sylvain MARIE <*****@*****.**> # + All contributors to <https://github.com/smarie/python-pytest-cases> # # License: 3-clause BSD, <https://github.com/smarie/python-pytest-cases/blob/master/LICENSE> from pytest_cases import param_fixture, fixture_union a = param_fixture("a", [1, 2]) b = param_fixture("b", [3, 4]) c = fixture_union('c', ['a', b], ids=['c=A', 'c=B']) d = fixture_union('d', ['a'], idstyle='compact') e = fixture_union('e', ['a'], idstyle=None) f = fixture_union('f', ['a']) def test_the_ids(c, d, e, f): pass def test_synthesis(module_results_dct): assert list(module_results_dct) == [ 'test_the_ids[c=A-1-Ua-a-f_is_a]', 'test_the_ids[c=A-2-Ua-a-f_is_a]', 'test_the_ids[c=B-3-Ua-1-a-f_is_a]', 'test_the_ids[c=B-3-Ua-2-a-f_is_a]', 'test_the_ids[c=B-4-Ua-1-a-f_is_a]', 'test_the_ids[c=B-4-Ua-2-a-f_is_a]', ]
import pytest from pytest_cases import fixture_union @pytest.fixture def first(): return 'hello' @pytest.fixture(params=['a', 'b']) def second(request): return request.param c = fixture_union('c', [first, second]) def test_basic_union(c): print(c) def test_synthesis(module_results_dct): assert list(module_results_dct) == ["test_basic_union[c_is_first]", "test_basic_union[c_is_second-a]", "test_basic_union[c_is_second-b]"]
# Authors: Sylvain MARIE <*****@*****.**> # + All contributors to <https://github.com/smarie/python-pytest-cases> # # License: 3-clause BSD, <https://github.com/smarie/python-pytest-cases/blob/master/LICENSE> from pytest_cases.plugin import SuperClosure from pytest_cases import param_fixture, fixture_union # basic parametrized fixtures a = param_fixture('a', ['x', 'y']) b = param_fixture('b', [1, 2]) # union fixtures c = fixture_union('c', [a, b]) d = fixture_union('d', [b, a], idstyle="explicit") super_closure = None def test_fixture_union_harder(c, a, d, request): # save super closure for later global super_closure super_closure = request._pyfuncitem.fixturenames print(c, a, d) def test_synthesis(module_results_dct): assert list(module_results_dct) == [ "test_fixture_union_harder[/a-x-d/b-1]", "test_fixture_union_harder[/a-x-d/b-2]", "test_fixture_union_harder[/a-x-d/a]", "test_fixture_union_harder[/a-y-d/b-1]",