MCPcopy Create free account
hub / github.com/pytest-dev/pytest / undo

Method undo

src/_pytest/monkeypatch.py:347–383  ·  view source on GitHub ↗

Undo previous changes. This call consumes the undo stack. Calling it a second time has no effect unless you do more monkeypatching after the undo call. There is generally no need to call `undo()`, since it is called automatically during tear-down. Note that

(self)

Source from the content-addressed store, hash-verified

345 os.chdir(path)
346
347 def undo(self) -> None:
348 """Undo previous changes.
349
350 This call consumes the undo stack. Calling it a second time has no
351 effect unless you do more monkeypatching after the undo call.
352
353 There is generally no need to call `undo()`, since it is
354 called automatically during tear-down.
355
356 Note that the same `monkeypatch` fixture is used across a
357 single test function invocation. If `monkeypatch` is used both by
358 the test function itself and one of the test fixtures,
359 calling `undo()` will undo all of the changes made in
360 both functions.
361 """
362 for obj, name, value in reversed(self._setattr):
363 if value is not notset:
364 setattr(obj, name, value)
365 else:
366 delattr(obj, name)
367 self._setattr[:] = []
368 for dictionary, key, value in reversed(self._setitem):
369 if value is notset:
370 try:
371 del dictionary[key]
372 except KeyError:
373 pass # Was already deleted, so we have the desired state.
374 else:
375 dictionary[key] = value
376 self._setitem[:] = []
377 if self._savesyspath is not None:
378 sys.path[:] = self._savesyspath
379 self._savesyspath = None
380
381 if self._cwd is not None:
382 os.chdir(self._cwd)
383 self._cwd = None

Callers 15

monkeypatchFunction · 0.95
test_setattrFunction · 0.95
test_delattrFunction · 0.95
test_setitemFunction · 0.95
test_delitemFunction · 0.95
test_setenvFunction · 0.95
test_delenvFunction · 0.95
test_setenv_prependFunction · 0.95

Calls 1

chdirMethod · 0.45

Tested by 15

test_setattrFunction · 0.76
test_delattrFunction · 0.76
test_setitemFunction · 0.76
test_delitemFunction · 0.76
test_setenvFunction · 0.76
test_delenvFunction · 0.76
test_setenv_prependFunction · 0.76