← 返回首页
Defer xfail condition evaluation with xfail_if_raises context manager by elovelan · Pull Request #2153 · gitpython-developers/GitPython · GitHub
Skip to content

Navigation Menu

Toggle navigation
Sign in
Appearance settings
Search or jump to...

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Include my email address so I can be contacted

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Resetting focus
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected Viewed files
Conversations
Failed to load comments. Retry
Loading
Jump to
Jump to file
Failed to load files. Retry
Loading
Diff view
Unified
Split
Hide whitespace
Apply and reload
Show whitespace
Diff view
Unified
Split
Hide whitespace
Apply and reload
27 changes: 27 additions & 0 deletions test/lib/helper.py
Show comments View file Edit file Delete file Open in desktop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"skipIf",
"GIT_REPO",
"GIT_DAEMON_PORT",
"xfail_if_raises",
]

import contextlib
Expand All @@ -35,8 +36,10 @@
import time
import unittest
import venv
from typing import Union, Type, Tuple

import gitdb
import pytest

from git.util import rmtree, cwd

Expand Down Expand Up @@ -465,3 +468,27 @@ def _executable(self, basename):
if osp.isfile(path) or osp.islink(path):
return path
raise RuntimeError(f"no regular file or symlink {path!r}")


@contextlib.contextmanager
def xfail_if_raises(
condition: bool,
*,
raises: Union[Type[BaseException], Tuple[Type[BaseException], ...]],
reason: str = "",
strict: bool = False,
):
"""Approximates the behavior of @pytest.mark.xfail(..., raises=...) as a context
manager that can be used within a test, such as when the condition is complex or has
side effects

One difference is it will not report XPASS if the test passes, but setting `strict`
simulates it by raising an exception"""
try:
yield
except raises:
if condition:
pytest.xfail(reason)
raise
if strict and condition:
pytest.fail("[XPASS(strict)] " + reason)
Loading
Loading
Toggle all file notes Toggle all file annotations

Footer

© 2026 GitHub, Inc.