From 1d6a6d95eefb055f7632a10aeb839a0f87a6763a Mon Sep 17 00:00:00 2001 From: TanyaAgarwal28 <8979149361t@gmail.com> Date: Wed, 11 Oct 2023 15:15:02 +0530 Subject: [PATCH] Bug Fix 11282: config.getini returns an empty list for an option of type string absent in INI file --- testing/conftest.py | 14 ++++++++++++++ testing/test_conftest.py | 35 +++++++---------------------------- 2 files changed, 21 insertions(+), 28 deletions(-) diff --git a/testing/conftest.py b/testing/conftest.py index 06116fee4..553cd45b5 100644 --- a/testing/conftest.py +++ b/testing/conftest.py @@ -147,6 +147,20 @@ def pytester(pytester: Pytester, monkeypatch: MonkeyPatch) -> Pytester: return pytester +def pytest_addoption(parser): + parser.addini( + "my_option", + type="string", + default=None, + help="My option", + ) + + +@pytest.fixture(scope="session") +def my_option(request): + return request.config.getini("my_option") + + @pytest.fixture(scope="session") def color_mapping(): """Returns a utility class which can replace keys in strings in the form "{NAME}" diff --git a/testing/test_conftest.py b/testing/test_conftest.py index f54ba2312..2760b14c9 100644 --- a/testing/test_conftest.py +++ b/testing/test_conftest.py @@ -222,35 +222,14 @@ def test_setinitial_conftest_subdirs(pytester: Pytester, name: str) -> None: def test_my_option(pytester: Pytester): - testdir = pytester.mkdir("test_my_option") - conftest_content = """ - import pytest - def pytest_addoption(parser): - parser.addini( - "my_option", - type="string", - default=None, - help="My option", - ) - @pytest.fixture(scope='session') - def my_option(request): - return request.config.getini("my_option") - """ - # Place conftest.py in the root directory of the project - testdir.parent.joinpath("conftest.py").write_text( - conftest_content, encoding="utf-8" + p = pytester.makepyfile( + """ + def test_x(my_option): + assert my_option is None + """ ) - - # Create a simple test function - test_content = """ - def test_example(my_option): - assert my_option is None - """ - testdir.joinpath("test_my_option.py").write_text(test_content, encoding="utf-8") - result = pytester.runpytest( - str(testdir.parent) - ) # Run pytest from the root directory - assert result.ret == 0 + result = pytester.runpytest(p) + result.assert_outcomes(failed=0, passed=1) def test_conftest_confcutdir(pytester: Pytester) -> None: