MCPcopy
hub / github.com/huggingface/transformers / _patched_tearDown

Function _patched_tearDown

src/transformers/testing_utils.py:3622–3717  ·  view source on GitHub ↗

Used to report a test that has failures captured and handled by patched functions/methods (without re-raise). The patched functions/methods refer to the `patched` defined in `_patch_with_call_info`, which is applied to `torch.testing.assert_close` and `unittest.case.TestCase.assertEqual`.

(self, *args, **kwargs)

Source from the content-addressed store, hash-verified

3620
3621
3622def _patched_tearDown(self, *args, **kwargs):
3623 """Used to report a test that has failures captured and handled by patched functions/methods (without re-raise).
3624
3625 The patched functions/methods refer to the `patched` defined in `_patch_with_call_info`, which is applied to
3626 `torch.testing.assert_close` and `unittest.case.TestCase.assertEqual`.
3627
3628 The objective is to avoid a failure being silence after being processed.
3629
3630 If there is any failure that is not handled by the patched functions/methods, we add custom error message for them
3631 along with the usual pytest failure report.
3632 """
3633
3634 # Check for regular failures before clearing:
3635 # when `_patched_tearDown` is called, the current test fails due to an assertion error given by a method being
3636 # patched by `_patch_with_call_info`. The patched method catches such an error and continue running the remaining
3637 # statements within the test. If the test fails with another error not handled by the patched methods, we don't let
3638 # pytest to fail and report it but the original failure (the first one that was processed) instead.
3639 # We still record those failures not handled by the patched methods, and add custom messages along with the usual
3640 # pytest failure report.
3641 regular_failures_info = []
3642
3643 errors = None
3644 if hasattr(self._outcome, "errors"):
3645 errors = self._outcome.errors
3646 elif hasattr(self._outcome, "result") and hasattr(self._outcome.result, "errors"):
3647 errors = self._outcome.result.errors
3648
3649 if hasattr(self, "_outcome") and errors:
3650 for error_entry in errors:
3651 test_instance, (exc_type, exc_obj, exc_tb) = error_entry
3652 # breakpoint()
3653 regular_failures_info.append(
3654 {
3655 "message": f"{str(exc_obj)}\n\n",
3656 "type": exc_type.__name__,
3657 "file": "test_modeling_vit.py",
3658 "line": 237, # get_deepest_frame_line(exc_tb) # Your helper function
3659 }
3660 )
3661
3662 # Clear the regular failure (i.e. that is not from any of our patched assertion methods) from pytest's records.
3663 if hasattr(self._outcome, "errors"):
3664 self._outcome.errors.clear()
3665 elif hasattr(self._outcome, "result") and hasattr(self._outcome.result, "errors"):
3666 self._outcome.result.errors.clear()
3667
3668 # reset back to the original tearDown method, so `_patched_tearDown` won't be run by the subsequent tests if they
3669 # have only test failures that are not handle by the patched methods (or no test failure at all).
3670 orig_tearDown = _patched_tearDown.orig_tearDown
3671 type(self).tearDown = orig_tearDown
3672
3673 # Call the original tearDown
3674 orig_tearDown(self, *args, **kwargs)
3675
3676 # Get the failure
3677 test_method = getattr(self, self._testMethodName)
3678 captured_failures = test_method.__func__.captured_failures[id(test_method)]
3679

Callers

nothing calls this directly

Calls 2

joinMethod · 0.80
clearMethod · 0.45

Tested by

no test coverage detected