Best Python code snippet using slash
test_method_ordering.py
Source:test_method_ordering.py  
...7@pytest.fixture8def hc(pm):9    class Hooks(object):10        @hookspec11        def he_method1(self, arg):12            pass13    pm.add_hookspecs(Hooks)14    return pm.hook.he_method115@pytest.fixture16def addmeth(hc):17    def addmeth(tryfirst=False, trylast=False, hookwrapper=False):18        def wrap(func):19            hookimpl(tryfirst=tryfirst, trylast=trylast,20                     hookwrapper=hookwrapper)(func)21            hc._add_hookimpl(HookImpl(None, "<temp>", func, func.example_impl))22            return func23        return wrap24    return addmeth25def funcs(hookmethods):26    return [hookmethod.function for hookmethod in hookmethods]27def test_adding_nonwrappers(hc, addmeth):28    @addmeth()29    def he_method1():30        pass31    @addmeth()32    def he_method2():33        pass34    @addmeth()35    def he_method3():36        pass37    assert funcs(hc._nonwrappers) == [he_method1, he_method2, he_method3]38def test_adding_nonwrappers_trylast(hc, addmeth):39    @addmeth()40    def he_method1_middle():41        pass42    @addmeth(trylast=True)43    def he_method1():44        pass45    @addmeth()46    def he_method1_b():47        pass48    assert funcs(hc._nonwrappers) == [he_method1, he_method1_middle, he_method1_b]49def test_adding_nonwrappers_trylast3(hc, addmeth):50    @addmeth()51    def he_method1_a():52        pass53    @addmeth(trylast=True)54    def he_method1_b():55        pass56    @addmeth()57    def he_method1_c():58        pass59    @addmeth(trylast=True)60    def he_method1_d():61        pass62    assert funcs(hc._nonwrappers) == \63        [he_method1_d, he_method1_b, he_method1_a, he_method1_c]64def test_adding_nonwrappers_trylast2(hc, addmeth):65    @addmeth()66    def he_method1_middle():67        pass68    @addmeth()69    def he_method1_b():70        pass71    @addmeth(trylast=True)72    def he_method1():73        pass74    assert funcs(hc._nonwrappers) == \75        [he_method1, he_method1_middle, he_method1_b]76def test_adding_nonwrappers_tryfirst(hc, addmeth):77    @addmeth(tryfirst=True)78    def he_method1():79        pass80    @addmeth()81    def he_method1_middle():82        pass83    @addmeth()84    def he_method1_b():85        pass86    assert funcs(hc._nonwrappers) == [87        he_method1_middle, he_method1_b, he_method1]88def test_adding_wrappers_ordering(hc, addmeth):89    @addmeth(hookwrapper=True)90    def he_method1():91        pass92    @addmeth()93    def he_method1_middle():94        pass95    @addmeth(hookwrapper=True)96    def he_method3():97        pass98    assert funcs(hc._nonwrappers) == [he_method1_middle]99    assert funcs(hc._wrappers) == [he_method1, he_method3]100def test_adding_wrappers_ordering_tryfirst(hc, addmeth):101    @addmeth(hookwrapper=True, tryfirst=True)102    def he_method1():103        pass104    @addmeth(hookwrapper=True)105    def he_method2():106        pass107    assert hc._nonwrappers == []108    assert funcs(hc._wrappers) == [he_method2, he_method1]109def test_hookspec(pm):110    class HookSpec(object):111        @hookspec()112        def he_myhook1(arg1):113            pass114        @hookspec(firstresult=True)115        def he_myhook2(arg1):116            pass117        @hookspec(firstresult=False)118        def he_myhook3(arg1):119            pass120    pm.add_hookspecs(HookSpec)121    assert not pm.hook.he_myhook1.spec_opts["firstresult"]122    assert pm.hook.he_myhook2.spec_opts["firstresult"]123    assert not pm.hook.he_myhook3.spec_opts["firstresult"]124@pytest.mark.parametrize('name', ["hookwrapper", "optionalhook", "tryfirst", "trylast"])125@pytest.mark.parametrize('val', [True, False])126def test_hookimpl(name, val):127    @hookimpl(**{name: val})128    def he_myhook1(arg1):129        pass130    if val:131        assert he_myhook1.example_impl.get(name)132    else:133        assert not hasattr(he_myhook1, name)134def test_load_setuptools_instantiation(monkeypatch, pm):135    pkg_resources = pytest.importorskip("pkg_resources")136    def my_iter(name):137        assert name == "hello"138        class EntryPoint(object):139            name = "myname"140            dist = None141            def load(self):142                class PseudoPlugin(object):143                    x = 42144                return PseudoPlugin()145        return iter([EntryPoint()])146    monkeypatch.setattr(pkg_resources, 'iter_entry_points', my_iter)147    num = pm.load_setuptools_entrypoints("hello")148    assert num == 1149    plugin = pm.get_plugin("myname")150    assert plugin.x == 42151    assert pm.list_plugin_distinfo() == [(plugin, None)]152def test_load_setuptools_not_installed(monkeypatch, pm):153    monkeypatch.setitem(154        sys.modules, 'pkg_resources',155        types.ModuleType("pkg_resources"))156    with pytest.raises(ImportError):157        pm.load_setuptools_entrypoints("qwe")158def test_add_tracefuncs(he_pm):159    out = []160    class api1(object):161        @hookimpl162        def he_method1(self):163            out.append("he_method1-api1")164    class api2(object):165        @hookimpl166        def he_method1(self):167            out.append("he_method1-api2")168    he_pm.register(api1())169    he_pm.register(api2())170    def before(hook_name, hook_impls, kwargs):171        out.append((hook_name, list(hook_impls), kwargs))172    def after(outcome, hook_name, hook_impls, kwargs):173        out.append((outcome, hook_name, list(hook_impls), kwargs))174    undo = he_pm.add_hookcall_monitoring(before, after)175    he_pm.hook.he_method1(arg=1)176    assert len(out) == 4177    assert out[0][0] == "he_method1"178    assert len(out[0][1]) == 2179    assert isinstance(out[0][2], dict)180    assert out[1] == "he_method1-api2"181    assert out[2] == "he_method1-api1"182    assert len(out[3]) == 4183    assert out[3][1] == out[0][0]184    undo()185    he_pm.hook.he_method1(arg=1)186    assert len(out) == 4 + 2187def test_hook_tracing(he_pm):188    saveindent = []189    class api1(object):190        @hookimpl191        def he_method1(self):192            saveindent.append(he_pm.trace.root.indent)193    class api2(object):194        @hookimpl195        def he_method1(self):196            saveindent.append(he_pm.trace.root.indent)197            raise ValueError()198    he_pm.register(api1())199    out = []200    he_pm.trace.root.setwriter(out.append)201    undo = he_pm.enable_tracing()202    try:203        indent = he_pm.trace.root.indent204        he_pm.hook.he_method1(arg=1)205        assert indent == he_pm.trace.root.indent206        assert len(out) == 2207        assert 'he_method1' in out[0]208        assert 'finish' in out[1]209        out[:] = []210        he_pm.register(api2())211        with pytest.raises(ValueError):212            he_pm.hook.he_method1(arg=1)213        assert he_pm.trace.root.indent == indent214        assert saveindent[0] > indent215    finally:216        undo()217@pytest.mark.parametrize('include_hookspec', [True, False])218def test_prefix_hookimpl(include_hookspec):219    pm = PluginManager(hookspec.project_name, "hello_")220    if include_hookspec:221        class HookSpec(object):222            @hookspec223            def hello_myhook(self, arg1):224                """ add to arg1 """225        pm.add_hookspecs(HookSpec)226    class Plugin(object):...test_hookcaller.py
Source:test_hookcaller.py  
...6@pytest.fixture7def hc(pm):8    class Hooks(object):9        @hookspec10        def he_method1(self, arg):11            pass12    pm.add_hookspecs(Hooks)13    return pm.hook.he_method114@pytest.fixture15def addmeth(hc):16    def addmeth(tryfirst=False, trylast=False, hookwrapper=False):17        def wrap(func):18            hookimpl(tryfirst=tryfirst, trylast=trylast, hookwrapper=hookwrapper)(func)19            hc._add_hookimpl(HookImpl(None, "<temp>", func, func.example_impl))20            return func21        return wrap22    return addmeth23def funcs(hookmethods):24    return [hookmethod.function for hookmethod in hookmethods]25def test_adding_nonwrappers(hc, addmeth):26    @addmeth()27    def he_method1():28        pass29    @addmeth()30    def he_method2():31        pass32    @addmeth()33    def he_method3():34        pass35    assert funcs(hc._nonwrappers) == [he_method1, he_method2, he_method3]36def test_adding_nonwrappers_trylast(hc, addmeth):37    @addmeth()38    def he_method1_middle():39        pass40    @addmeth(trylast=True)41    def he_method1():42        pass43    @addmeth()44    def he_method1_b():45        pass46    assert funcs(hc._nonwrappers) == [he_method1, he_method1_middle, he_method1_b]47def test_adding_nonwrappers_trylast3(hc, addmeth):48    @addmeth()49    def he_method1_a():50        pass51    @addmeth(trylast=True)52    def he_method1_b():53        pass54    @addmeth()55    def he_method1_c():56        pass57    @addmeth(trylast=True)58    def he_method1_d():59        pass60    assert funcs(hc._nonwrappers) == [61        he_method1_d,62        he_method1_b,63        he_method1_a,64        he_method1_c,65    ]66def test_adding_nonwrappers_trylast2(hc, addmeth):67    @addmeth()68    def he_method1_middle():69        pass70    @addmeth()71    def he_method1_b():72        pass73    @addmeth(trylast=True)74    def he_method1():75        pass76    assert funcs(hc._nonwrappers) == [he_method1, he_method1_middle, he_method1_b]77def test_adding_nonwrappers_tryfirst(hc, addmeth):78    @addmeth(tryfirst=True)79    def he_method1():80        pass81    @addmeth()82    def he_method1_middle():83        pass84    @addmeth()85    def he_method1_b():86        pass87    assert funcs(hc._nonwrappers) == [he_method1_middle, he_method1_b, he_method1]88def test_adding_wrappers_ordering(hc, addmeth):89    @addmeth(hookwrapper=True)90    def he_method1():91        pass92    @addmeth()93    def he_method1_middle():94        pass95    @addmeth(hookwrapper=True)96    def he_method3():97        pass98    assert funcs(hc._nonwrappers) == [he_method1_middle]99    assert funcs(hc._wrappers) == [he_method1, he_method3]100def test_adding_wrappers_ordering_tryfirst(hc, addmeth):101    @addmeth(hookwrapper=True, tryfirst=True)102    def he_method1():103        pass104    @addmeth(hookwrapper=True)105    def he_method2():106        pass107    assert hc._nonwrappers == []108    assert funcs(hc._wrappers) == [he_method2, he_method1]109def test_hookspec(pm):110    class HookSpec(object):111        @hookspec()112        def he_myhook1(arg1):113            pass114        @hookspec(firstresult=True)115        def he_myhook2(arg1):116            pass...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
