|
3 | 3 | import os |
4 | 4 | import re |
5 | 5 | from pathlib import Path |
| 6 | +from textwrap import dedent |
6 | 7 | from unittest import mock |
7 | 8 |
|
8 | 9 | import pytest |
@@ -478,6 +479,57 @@ def test_env_via_env_file( # noqa: PLR0913, PLR0917 |
478 | 479 | result.assert_outcomes(passed=1) |
479 | 480 |
|
480 | 481 |
|
| 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 | + |
481 | 533 | def test_env_files_from_toml_bad_toml(tmp_path: Path) -> None: |
482 | 534 | toml_file = tmp_path / "pyproject.toml" |
483 | 535 | toml_file.write_text("bad toml", encoding="utf-8") |
|
0 commit comments