Best Python code snippet using pytest
31674_compat.py
Source:31674_compat.py
...41 lineno = py.builtin._getcode(function).co_firstlineno42 if fn.relto(curdir):43 fn = fn.relto(curdir)44 return "%s:%d" %(fn, lineno+1)45def num_mock_patch_args(function):46 """ return number of arguments used up by mock arguments (if any) """47 patchings = getattr(function, "patchings", None)48 if not patchings:49 return 050 mock = sys.modules.get("mock", sys.modules.get("unittest.mock", None))51 if mock is not None:52 return len([p for p in patchings53 if not p.attribute_name and p.new is mock.DEFAULT])54 return len(patchings)55def getfuncargnames(function, startindex=None):56 # XXX merge with main.py's varnames57 #assert not isclass(function)58 realfunction = function59 while hasattr(realfunction, "__wrapped__"):60 realfunction = realfunction.__wrapped__61 if startindex is None:62 startindex = inspect.ismethod(function) and 1 or 063 if realfunction != function:64 startindex += num_mock_patch_args(function)65 function = realfunction66 if isinstance(function, functools.partial):67 argnames = inspect.getargs(_pytest._code.getrawcode(function.func))[0]68 partial = function69 argnames = argnames[len(partial.args):]70 if partial.keywords:71 for kw in partial.keywords:72 argnames.remove(kw)73 else:74 argnames = inspect.getargs(_pytest._code.getrawcode(function))[0]75 defaults = getattr(function, 'func_defaults',76 getattr(function, '__defaults__', None)) or ()77 numdefaults = len(defaults)78 if numdefaults:...
compat.py
Source:compat.py
...41 lineno = py.builtin._getcode(function).co_firstlineno42 if fn.relto(curdir):43 fn = fn.relto(curdir)44 return "%s:%d" %(fn, lineno+1)45def num_mock_patch_args(function):46 """ return number of arguments used up by mock arguments (if any) """47 patchings = getattr(function, "patchings", None)48 if not patchings:49 return 050 mock = sys.modules.get("mock", sys.modules.get("unittest.mock", None))51 if mock is not None:52 return len([p for p in patchings53 if not p.attribute_name and p.new is mock.DEFAULT])54 return len(patchings)55def getfuncargnames(function, startindex=None):56 # XXX merge with main.py's varnames57 #assert not isclass(function)58 realfunction = function59 while hasattr(realfunction, "__wrapped__"):60 realfunction = realfunction.__wrapped__61 if startindex is None:62 startindex = inspect.ismethod(function) and 1 or 063 if realfunction != function:64 startindex += num_mock_patch_args(function)65 function = realfunction66 if isinstance(function, functools.partial):67 argnames = inspect.getargs(_pytest._code.getrawcode(function.func))[0]68 partial = function69 argnames = argnames[len(partial.args):]70 if partial.keywords:71 for kw in partial.keywords:72 argnames.remove(kw)73 else:74 argnames = inspect.getargs(_pytest._code.getrawcode(function))[0]75 defaults = getattr(function, 'func_defaults',76 getattr(function, '__defaults__', None)) or ()77 numdefaults = len(defaults)78 if numdefaults:...
fixtures.py
Source:fixtures.py
...18 test.nonmagic_fixtures_count = len(fixtures)19 test_func.nonmagic_fixtures_count = len(fixtures)20 return test_func21 return apply_fixtures22def _patch_pytest_num_mock_patch_args():23 # HACK support @pytest.mark.parametrize(...) on @using(...)-wrapped tests24 def num_mock_patch_args(function):25 num = getattr(function, "nonmagic_fixtures_count", 0)26 return num + real_num_mock_patch_args(function)27 import _pytest.compat as compat28 real_num_mock_patch_args = compat.num_mock_patch_args29 compat.num_mock_patch_args = num_mock_patch_args...
Looking for an in-depth tutorial around pytest? LambdaTest covers the detailed pytest tutorial that has everything related to the pytest, from setting up the pytest framework to automation testing. Delve deeper into pytest testing by exploring advanced use cases like parallel testing, pytest fixtures, parameterization, executing multiple test cases from a single file, and more.
Skim our below pytest tutorial playlist to get started with automation testing using the pytest framework.
https://www.youtube.com/playlist?list=PLZMWkkQEwOPlcGgDmHl8KkXKeLF83XlrP
Get 100 minutes of automation test minutes FREE!!