Best Python code snippet using pytest
selenium.py
Source:selenium.py
...30)31#-----------------------------------------------------------------------------32# General API33#-----------------------------------------------------------------------------34def pytest_report_collectionfinish(config, startdir, items):35 '''36 '''37 driver = config.getoption('driver', 'chrome').lower()38 asserts = "ON" if driver == "chrome" else "OFF"39 return ["", "Bokeh selenium tests using %r driver (no-console-error assertions: %s)" % (driver, asserts)]40@pytest.yield_fixture(scope="session")41def driver(pytestconfig):42 ''' Select and configure a Selenium webdriver for integration tests.43 '''44 driver_name = pytestconfig.getoption('driver', 'chrome').lower()45 if driver_name == "chrome":46 from selenium.webdriver.chrome.options import Options47 options = Options()48 options.add_argument("--headless")...
stepwise.py
Source:stepwise.py
...76 if report.when == "call":77 # Remove test from the failed ones, if exists.78 if report.nodeid == self.lastfailed:79 self.lastfailed = None80 def pytest_report_collectionfinish(self):81 if self.active and self.config.getoption("verbose") >= 0 and self.report_status:82 return "stepwise: %s" % self.report_status83 def pytest_sessionfinish(self, session):84 if self.active:85 self.config.cache.set("cache/stepwise", self.lastfailed)86 else:87 # Clear the list of failing tests if the plugin is not active....
compat.py
Source:compat.py
1import functools2import warnings3from pathlib import Path4from typing import Optional5from ..compat import LEGACY_PATH6from ..compat import legacy_path7from ..deprecated import HOOK_LEGACY_PATH_ARG8from _pytest.nodes import _check_path9# hookname: (Path, LEGACY_PATH)10imply_paths_hooks = {11 "pytest_ignore_collect": ("collection_path", "path"),12 "pytest_collect_file": ("file_path", "path"),13 "pytest_pycollect_makemodule": ("module_path", "path"),14 "pytest_report_header": ("start_path", "startdir"),15 "pytest_report_collectionfinish": ("start_path", "startdir"),16}17class PathAwareHookProxy:18 """19 this helper wraps around hook callers20 until pluggy supports fixingcalls, this one will do21 it currently doesn't return full hook caller proxies for fixed hooks,22 this may have to be changed later depending on bugs23 """24 def __init__(self, hook_caller):25 self.__hook_caller = hook_caller26 def __dir__(self):27 return dir(self.__hook_caller)28 def __getattr__(self, key, _wraps=functools.wraps):29 hook = getattr(self.__hook_caller, key)30 if key not in imply_paths_hooks:31 self.__dict__[key] = hook32 return hook33 else:34 path_var, fspath_var = imply_paths_hooks[key]35 @_wraps(hook)36 def fixed_hook(**kw):37 path_value: Optional[Path] = kw.pop(path_var, None)38 fspath_value: Optional[LEGACY_PATH] = kw.pop(fspath_var, None)39 if fspath_value is not None:40 warnings.warn(41 HOOK_LEGACY_PATH_ARG.format(42 pylib_path_arg=fspath_var, pathlib_path_arg=path_var43 ),44 stacklevel=2,45 )46 if path_value is not None:47 if fspath_value is not None:48 _check_path(path_value, fspath_value)49 else:50 fspath_value = legacy_path(path_value)51 else:52 assert fspath_value is not None53 path_value = Path(fspath_value)54 kw[path_var] = path_value55 kw[fspath_var] = fspath_value56 return hook(**kw)57 fixed_hook.__name__ = key58 self.__dict__[key] = fixed_hook...
pytest_shard.py
Source:pytest_shard.py
...19 type=positive_int,20 default=1,21 help="Total number of shards.",22 )23def pytest_report_collectionfinish(config, items: Sequence[nodes.Node]) -> str:24 """Log how many and, if verbose, which items are tested in this shard."""25 msg = f"Running {len(items)} items in this shard"26 if config.option.verbose > 0:27 msg += ": " + ", ".join([item.nodeid for item in items])28 return msg29def sha256hash(x: str) -> int:30 return int.from_bytes(hashlib.sha256(x.encode()).digest(), "little")31def filter_items_by_shard(32 items: Iterable[nodes.Node], shard_id: int, num_shards: int33) -> Sequence[nodes.Node]:34 """Computes `items` that should be tested in `shard_id` out of `num_shards` total shards."""35 shards = [sha256hash(item.nodeid) % num_shards for item in items]36 new_items = []37 for shard, item in zip(shards, items):...
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!!