Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[torchlib] Improve xfail decorator to be more robust and accurate #794

Open
BowenBao opened this issue Jun 19, 2023 · 2 comments
Open

[torchlib] Improve xfail decorator to be more robust and accurate #794

BowenBao opened this issue Jun 19, 2023 · 2 comments
Labels
topic: torch_lib Related to the torch/aten function lib in development

Comments

@BowenBao
Copy link
Contributor

xfail now is powered by pytest.xfail(reason=reason). While it is great and better than skip, it does not offer check on the actual failure reason. Hence through time the descriptive reason may become outdated and misleading.

An option is to rewrite decorator with self.assertRaiseRegex.

cc @justinchuby, @titaiwangms

@BowenBao BowenBao added the topic: torch_lib Related to the torch/aten function lib in development label Jun 19, 2023
@justinchuby justinchuby self-assigned this Jun 19, 2023
@titaiwangms
Copy link
Contributor

titaiwangms commented Jun 19, 2023

Code is here:

@contextlib.contextmanager
def normal_xfail_skip_test_behaviors(
test_behavior: Optional[str] = None, reason: Optional[str] = None
):
"""This context manager is used to handle the different behaviors of xfail and skip.
Args:
test_behavior (optional[str]): From DecorateMeta name, can be 'skip', 'xfail', or None.
reason (optional[str]): The reason for the failure or skip.
Raises:
e: Any exception raised by the test case if it's not an expected failure.
"""
# We need to skip as soon as possible, as SegFault might also be a case.
if test_behavior == "skip":
pytest.skip(reason=reason)
try:
yield
# We could use `except (AssertionError, RuntimeError, ...) as e:`, but it needs
# to go over all test cases to find the right exception type.
except Exception as e: # pylint: disable=broad-exception-caught
if test_behavior is None:
raise e
if test_behavior == "xfail":
pytest.xfail(reason=reason)
else:
if test_behavior == "xfail":
pytest.fail("Test unexpectedly passed")

@justinchuby
Copy link
Contributor

#799

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: torch_lib Related to the torch/aten function lib in development
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants