Skip to content

Commit 59db65e

Browse files
gaborbernatclaude
andauthored
Add test for env vars available during conftest imports (#194)
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 16158a4 commit 59db65e

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

tests/test_env.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
import re
55
from pathlib import Path
6+
from textwrap import dedent
67
from unittest import mock
78

89
import pytest
@@ -478,6 +479,57 @@ def test_env_via_env_file( # noqa: PLR0913, PLR0917
478479
result.assert_outcomes(passed=1)
479480

480481

482+
@pytest.mark.parametrize(
483+
("config_content", "config_file"),
484+
[
485+
pytest.param(
486+
"[pytest]\nenv = MAGIC=alpha",
487+
"pytest.ini",
488+
id="ini",
489+
),
490+
pytest.param(
491+
'[tool.pytest_env]\nMAGIC = "alpha"',
492+
"pyproject.toml",
493+
id="pyproject toml",
494+
),
495+
pytest.param(
496+
'[pytest_env]\nMAGIC = "alpha"',
497+
"pytest.toml",
498+
id="pytest toml",
499+
),
500+
],
501+
)
502+
def test_conftest_import_uses_env(
503+
pytester: pytest.Pytester,
504+
config_content: str,
505+
config_file: str,
506+
) -> None:
507+
(pytester.path / config_file).write_text(config_content, encoding="utf-8")
508+
(pytester.path / "app_config.py").write_text(
509+
dedent("""\
510+
import os
511+
512+
VALUE = os.environ['MAGIC']
513+
"""),
514+
encoding="utf-8",
515+
)
516+
pytester.makeconftest("from app_config import VALUE")
517+
pytester.makepyfile(
518+
test_it=dedent("""\
519+
from conftest import VALUE
520+
521+
def test_it() -> None:
522+
assert VALUE == 'alpha'
523+
"""),
524+
)
525+
526+
new_env = {"PYTEST_DISABLE_PLUGIN_AUTOLOAD": "1", "PYTEST_PLUGINS": "pytest_env.plugin"}
527+
with mock.patch.dict(os.environ, new_env, clear=True):
528+
result = pytester.runpytest()
529+
530+
result.assert_outcomes(passed=1)
531+
532+
481533
def test_env_files_from_toml_bad_toml(tmp_path: Path) -> None:
482534
toml_file = tmp_path / "pyproject.toml"
483535
toml_file.write_text("bad toml", encoding="utf-8")

0 commit comments

Comments
 (0)