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

Method setenv

src/_pytest/monkeypatch.py:282–302  ·  view source on GitHub ↗

Set environment variable ``name`` to ``value``. If ``prepend`` is a character, read the current environment variable value and prepend the ``value`` adjoined with the ``prepend`` character.

(self, name: str, value: str, prepend: Optional[str] = None)

Source from the content-addressed store, hash-verified

280 del dic[name]
281
282 def setenv(self, name: str, value: str, prepend: Optional[str] = None) -> None:
283 """Set environment variable ``name`` to ``value``.
284
285 If ``prepend`` is a character, read the current environment variable
286 value and prepend the ``value`` adjoined with the ``prepend``
287 character.
288 """
289 if not isinstance(value, str):
290 warnings.warn( # type: ignore[unreachable]
291 PytestWarning(
292 "Value of environment variable {name} type should be str, but got "
293 "{value!r} (type: {type}); converted to str implicitly".format(
294 name=name, value=value, type=type(value).__name__
295 )
296 ),
297 stacklevel=2,
298 )
299 value = str(value)
300 if prepend and name in os.environ:
301 value = value + prepend + os.environ[name]
302 self.setitem(os.environ, name, value)
303
304 def delenv(self, name: str, raising: bool = True) -> None:
305 """Delete ``name`` from the environment.

Calls 4

setitemMethod · 0.95
PytestWarningClass · 0.90
warnMethod · 0.45
formatMethod · 0.45