Best Python code snippet using pytest
test_pathlib.py
Source:test_pathlib.py
...246 importlib.util, "spec_from_file_location", lambda *args: None247 )248 with pytest.raises(ImportError):249 import_path(simple_module, mode="importlib")250def test_resolve_package_path(tmp_path):251 pkg = tmp_path / "pkg1"252 pkg.mkdir()253 (pkg / "__init__.py").touch()254 (pkg / "subdir").mkdir()255 (pkg / "subdir/__init__.py").touch()256 assert resolve_package_path(pkg) == pkg257 assert resolve_package_path(pkg.joinpath("subdir", "__init__.py")) == pkg258def test_package_unimportable(tmp_path):259 pkg = tmp_path / "pkg1-1"260 pkg.mkdir()261 pkg.joinpath("__init__.py").touch()262 subdir = pkg.joinpath("subdir")263 subdir.mkdir()264 pkg.joinpath("subdir/__init__.py").touch()265 assert resolve_package_path(subdir) == subdir266 xyz = subdir.joinpath("xyz.py")267 xyz.touch()268 assert resolve_package_path(xyz) == subdir269 assert not resolve_package_path(pkg)270def test_access_denied_during_cleanup(tmp_path, monkeypatch):271 """Ensure that deleting a numbered dir does not fail because of OSErrors (#4262)."""272 path = tmp_path / "temp-1"273 path.mkdir()274 def renamed_failed(*args):275 raise OSError("access denied")276 monkeypatch.setattr(Path, "rename", renamed_failed)277 lock_path = get_lock_path(path)278 maybe_delete_a_numbered_dir(path)279 assert not lock_path.is_file()280def test_long_path_during_cleanup(tmp_path):281 """Ensure that deleting long path works (particularly on Windows (#6775))."""282 path = (tmp_path / ("a" * 250)).resolve()283 if sys.platform == "win32":...
ansible_pytest_collections.py
Source:ansible_pytest_collections.py
...5# set by ansible-test to a single directory, rather than a list of directories as supported by Ansible itself6ANSIBLE_COLLECTIONS_PATH = os.path.join(os.environ['ANSIBLE_COLLECTIONS_PATH'], 'ansible_collections')7# this monkeypatch to _pytest.pathlib.resolve_package_path fixes PEP420 resolution for collections in pytest >= 6.0.08# NB: this code should never run under py29def collection_resolve_package_path(path):10 """Configure the Python package path so that pytest can find our collections."""11 for parent in path.parents:12 if str(parent) == ANSIBLE_COLLECTIONS_PATH:13 return parent14 raise Exception('File "%s" not found in collection path "%s".' % (path, ANSIBLE_COLLECTIONS_PATH))15# this monkeypatch to py.path.local.LocalPath.pypkgpath fixes PEP420 resolution for collections in pytest < 6.0.016def collection_pypkgpath(self):17 """Configure the Python package path so that pytest can find our collections."""18 for parent in self.parts(reverse=True):19 if str(parent) == ANSIBLE_COLLECTIONS_PATH:20 return parent21 raise Exception('File "%s" not found in collection path "%s".' % (self.strpath, ANSIBLE_COLLECTIONS_PATH))22def pytest_configure():23 """Configure this pytest plugin."""...
pytest_collection_loader.py
Source:pytest_collection_loader.py
...5import os.path6import sys7ANSIBLE_COLLECTIONS_PATH = os.path.abspath(os.path.join(__file__, '..', '..', '..', '..', '..', '..'))8# this monkeypatch to _pytest.pathlib.resolve_package_path fixes PEP420 resolution for collections in pytest >= 6.0.09def collection_resolve_package_path(path):10 """Configure the Python package path so that pytest can find our collections."""11 for parent in path.parents:12 if str(parent) == ANSIBLE_COLLECTIONS_PATH:13 return parent14 raise Exception('File "%s" not found in collection path "%s".' % (path, ANSIBLE_COLLECTIONS_PATH))15def pytest_configure():16 """Configure this pytest plugin."""17 try:18 if pytest_configure.executed:19 return20 except AttributeError:21 pytest_configure.executed = True22 # If ANSIBLE_HOME is set make sure we add it to the PYTHONPATH to ensure it is picked up. Not all env vars are23 # picked up by vscode (.bashrc is a notable one) so a user can define it manually in their .env file....
namespace_modules_patch.py
Source:namespace_modules_patch.py
...19# we consider all dirs in tests/ to be namespace packages20rootdir = pathlib.Path(__file__).parent.resolve()21namespace_pkg_dirs = [str(d) for d in rootdir.iterdir() if d.is_dir()]22# patched method23def resolve_package_path(path):24 # call original lookup25 result = resolve_pkg_path_orig(path)26 if result is not None:27 return result28 # original lookup failed, check if we are subdir of a namespace package29 # if yes, return the namespace package we belong to30 for parent in path.parents:31 if str(parent) in namespace_pkg_dirs:32 return parent33 return None34def apply_patch():...
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!!