How to use register_cleanup_lock_removal method in Pytest

Best Python code snippet using pytest

test_tmpdir.py

Source:test_tmpdir.py Github

copy

Full Screen

...211 def test_lock_register_cleanup_removal(self, tmp_path):212 from _pytest.pathlib import create_cleanup_lock, register_cleanup_lock_removal213 lock = create_cleanup_lock(tmp_path)214 registry = []215 register_cleanup_lock_removal(lock, register=registry.append)216 cleanup_func, = registry217 assert lock.is_file()218 cleanup_func(original_pid="intentionally_different")219 assert lock.is_file()220 cleanup_func()221 assert not lock.exists()222 cleanup_func()223 assert not lock.exists()224 def _do_cleanup(self, tmp_path):225 self.test_make(tmp_path)226 from _pytest.pathlib import cleanup_numbered_dir227 cleanup_numbered_dir(228 root=tmp_path,229 prefix=self.PREFIX,...

Full Screen

Full Screen

pathlib.py

Source:pathlib.py Github

copy

Full Screen

...112 os.close(fd)113 if not lock_path.is_file():114 raise EnvironmentError("lock path got renamed after successful creation")115 return lock_path116def register_cleanup_lock_removal(lock_path, register=atexit.register):117 """registers a cleanup function for removing a lock, by default on atexit"""118 pid = os.getpid()119 def cleanup_on_exit(lock_path=lock_path, original_pid=pid):120 current_pid = os.getpid()121 if current_pid != original_pid:122 # fork123 return124 try:125 lock_path.unlink()126 except (OSError, IOError):127 pass128 return register(cleanup_on_exit)129def maybe_delete_a_numbered_dir(path):130 """removes a numbered directory if its lock can be obtained and it does not seem to be in use"""131 lock_path = None132 try:133 lock_path = create_cleanup_lock(path)134 parent = path.parent135 garbage = parent.joinpath("garbage-{}".format(uuid.uuid4()))136 path.rename(garbage)137 rmtree(garbage, force=True)138 except (OSError, EnvironmentError):139 # known races:140 # * other process did a cleanup at the same time141 # * deletable folder was found142 # * process cwd (Windows)143 return144 finally:145 # if we created the lock, ensure we remove it even if we failed146 # to properly remove the numbered dir147 if lock_path is not None:148 try:149 lock_path.unlink()150 except (OSError, IOError):151 pass152def ensure_deletable(path, consider_lock_dead_if_created_before):153 """checks if a lock exists and breaks it if its considered dead"""154 if path.is_symlink():155 return False156 lock = get_lock_path(path)157 if not lock.exists():158 return True159 try:160 lock_time = lock.stat().st_mtime161 except Exception:162 return False163 else:164 if lock_time < consider_lock_dead_if_created_before:165 lock.unlink()166 return True167 else:168 return False169def try_cleanup(path, consider_lock_dead_if_created_before):170 """tries to cleanup a folder if we can ensure it's deletable"""171 if ensure_deletable(path, consider_lock_dead_if_created_before):172 maybe_delete_a_numbered_dir(path)173def cleanup_candidates(root, prefix, keep):174 """lists candidates for numbered directories to be removed - follows py.path"""175 max_existing = max(map(parse_num, find_suffixes(root, prefix)), default=-1)176 max_delete = max_existing - keep177 paths = find_prefixed(root, prefix)178 paths, paths2 = itertools.tee(paths)179 numbers = map(parse_num, extract_suffixes(paths2, prefix))180 for path, number in zip(paths, numbers):181 if number <= max_delete:182 yield path183def cleanup_numbered_dir(root, prefix, keep, consider_lock_dead_if_created_before):184 """cleanup for lock driven numbered directories"""185 for path in cleanup_candidates(root, prefix, keep):186 try_cleanup(path, consider_lock_dead_if_created_before)187 for path in root.glob("garbage-*"):188 try_cleanup(path, consider_lock_dead_if_created_before)189def make_numbered_dir_with_cleanup(root, prefix, keep, lock_timeout):190 """creates a numbered dir with a cleanup lock and removes old ones"""191 e = None192 for i in range(10):193 try:194 p = make_numbered_dir(root, prefix)195 lock_path = create_cleanup_lock(p)196 register_cleanup_lock_removal(lock_path)197 except Exception as exc:198 e = exc199 else:200 consider_lock_dead_if_created_before = p.stat().st_mtime - lock_timeout201 cleanup_numbered_dir(202 root=root,203 prefix=prefix,204 keep=keep,205 consider_lock_dead_if_created_before=consider_lock_dead_if_created_before,206 )207 return p208 assert e is not None209 raise e210def resolve_from_str(input, root):...

Full Screen

Full Screen

Pytest Tutorial

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.

Chapters

  1. What is pytest
  2. Pytest installation: Want to start pytest from scratch? See how to install and configure pytest for Python automation testing.
  3. Run first test with pytest framework: Follow this step-by-step tutorial to write and run your first pytest script.
  4. Parallel testing with pytest: A hands-on guide to parallel testing with pytest to improve the scalability of your test automation.
  5. Generate pytest reports: Reports make it easier to understand the results of pytest-based test runs. Learn how to generate pytest reports.
  6. Pytest Parameterized tests: Create and run your pytest scripts while avoiding code duplication and increasing test coverage with parameterization.
  7. Pytest Fixtures: Check out how to implement pytest fixtures for your end-to-end testing needs.
  8. Execute Multiple Test Cases: Explore different scenarios for running multiple test cases in pytest from a single file.
  9. Stop Test Suite after N Test Failures: See how to stop your test suite after n test failures in pytest using the @pytest.mark.incremental decorator and maxfail command-line option.

YouTube

Skim our below pytest tutorial playlist to get started with automation testing using the pytest framework.

https://www.youtube.com/playlist?list=PLZMWkkQEwOPlcGgDmHl8KkXKeLF83XlrP

Run Pytest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful