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

Function evaluate_condition

src/_pytest/skipping.py:85–157  ·  view source on GitHub ↗

Evaluate a single skipif/xfail condition. If an old-style string condition is given, it is eval()'d, otherwise the condition is bool()'d. If this fails, an appropriately formatted pytest.fail is raised. Returns (result, reason). The reason is only relevant if the result is True.

(item: Item, mark: Mark, condition: object)

Source from the content-addressed store, hash-verified

83
84
85def evaluate_condition(item: Item, mark: Mark, condition: object) -> Tuple[bool, str]:
86 """Evaluate a single skipif/xfail condition.
87
88 If an old-style string condition is given, it is eval()'d, otherwise the
89 condition is bool()'d. If this fails, an appropriately formatted pytest.fail
90 is raised.
91
92 Returns (result, reason). The reason is only relevant if the result is True.
93 """
94 # String condition.
95 if isinstance(condition, str):
96 globals_ = {
97 "os": os,
98 "sys": sys,
99 "platform": platform,
100 "config": item.config,
101 }
102 for dictionary in reversed(
103 item.ihook.pytest_markeval_namespace(config=item.config)
104 ):
105 if not isinstance(dictionary, Mapping):
106 raise ValueError(
107 "pytest_markeval_namespace() needs to return a dict, got {!r}".format(
108 dictionary
109 )
110 )
111 globals_.update(dictionary)
112 if hasattr(item, "obj"):
113 globals_.update(item.obj.__globals__) # type: ignore[attr-defined]
114 try:
115 filename = f"<{mark.name} condition>"
116 condition_code = compile(condition, filename, "eval")
117 result = eval(condition_code, globals_)
118 except SyntaxError as exc:
119 msglines = [
120 "Error evaluating %r condition" % mark.name,
121 " " + condition,
122 " " + " " * (exc.offset or 0) + "^",
123 "SyntaxError: invalid syntax",
124 ]
125 fail("\n".join(msglines), pytrace=False)
126 except Exception as exc:
127 msglines = [
128 "Error evaluating %r condition" % mark.name,
129 " " + condition,
130 *traceback.format_exception_only(type(exc), exc),
131 ]
132 fail("\n".join(msglines), pytrace=False)
133
134 # Boolean condition.
135 else:
136 try:
137 result = bool(condition)
138 except Exception as exc:
139 msglines = [
140 "Error evaluating %r condition as a boolean" % mark.name,
141 *traceback.format_exception_only(type(exc), exc),
142 ]

Callers 2

evaluate_skip_marksFunction · 0.85
evaluate_xfail_marksFunction · 0.85

Calls 4

failFunction · 0.90
updateMethod · 0.80
formatMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected