From 6c1eaf6e7f60f8e9b0d487d52741a93f1fb879ca Mon Sep 17 00:00:00 2001 From: TanyaAgarwal28 <8979149361t@gmail.com> Date: Wed, 11 Oct 2023 12:06:51 +0530 Subject: [PATCH] Bug Fix 11282: config.getini returns an empty list for an option of type string absent in INI file --- changelog/11282.bugfix.rst | 1 + src/_pytest/config/__init__.py | 2 ++ testing/test_conftest.py | 25 +++++++++++++++++++++++++ 3 files changed, 28 insertions(+) create mode 100644 changelog/11282.bugfix.rst diff --git a/changelog/11282.bugfix.rst b/changelog/11282.bugfix.rst new file mode 100644 index 000000000..1712d2084 --- /dev/null +++ b/changelog/11282.bugfix.rst @@ -0,0 +1 @@ +Return "None" as the default value if "None" or no default value if provided by the developer.In this approach, existing calls to the getini function would need to check for "None" values. diff --git a/src/_pytest/config/__init__.py b/src/_pytest/config/__init__.py index 447ebc42a..646d7aa96 100644 --- a/src/_pytest/config/__init__.py +++ b/src/_pytest/config/__init__.py @@ -1525,6 +1525,8 @@ class Config: return default if type is None: return "" + if type == "string": + return None return [] else: value = override_value diff --git a/testing/test_conftest.py b/testing/test_conftest.py index cfc2d577b..09d2cbed9 100644 --- a/testing/test_conftest.py +++ b/testing/test_conftest.py @@ -221,6 +221,31 @@ def test_setinitial_conftest_subdirs(pytester: Pytester, name: str) -> None: assert len(set(pm.get_plugins()) - {pm}) == 0 +def test_my_option(pytester: Pytester): + testdir = pytester.mkdir("test_my_option") + testdir.joinpath("conftest.py").write_text( + textwrap.dedent( + """\ + 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") + """ + ), + encoding="utf-8", + ) + result = pytester.runpytest(str(testdir)) + assert result.ret == 0 + captured_stdout = result.stdout.str() + assert "1 passed" in captured_stdout + + def test_conftest_confcutdir(pytester: Pytester) -> None: pytester.makeconftest("assert 0") x = pytester.mkdir("x")