diff --git a/changelog/11282.bugfix.rst b/changelog/11282.bugfix.rst new file mode 100644 index 00000000000..1712d2084d9 --- /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 447ebc42abb..646d7aa9618 100644 --- a/src/_pytest/config/__init__.py +++ b/src/_pytest/config/__init__.py @@ -1525,6 +1525,8 @@ def _getini(self, name: str): 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 cfc2d577b53..665d438fdaf 100644 --- a/testing/test_conftest.py +++ b/testing/test_conftest.py @@ -221,6 +221,33 @@ def test_setinitial_conftest_subdirs(pytester: Pytester, name: str) -> None: assert len(set(pm.get_plugins()) - {pm}) == 0 +def test_my_option(pytester: Pytester): + pytester.makeconftest( + """\ + 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") + """ + ) + pytester.makepyfile( + """\ + def test_x(my_option): + assert my_option is None + """ + ) + res = pytester.runpytest() + assert res.ret == 0 + + def test_conftest_confcutdir(pytester: Pytester) -> None: pytester.makeconftest("assert 0") x = pytester.mkdir("x")