From 54b074bbe17664066358a3baaf8df248f9c2dafb Mon Sep 17 00:00:00 2001 From: Tanya Agarwal Date: Sun, 8 Oct 2023 17:30:38 +0530 Subject: [PATCH 01/12] Bug Fix 11456: Duplicated parameters in @pytest.mark.parametrize --- testing/test_python.py | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 testing/test_python.py diff --git a/testing/test_python.py b/testing/test_python.py new file mode 100644 index 00000000000..117571bc1ce --- /dev/null +++ b/testing/test_python.py @@ -0,0 +1,11 @@ +import pytest + +@pytest.mark.parametrize("a", [1, 2, 10, 11, 2, 1, 12, 11,2_1]) +def test_params(a): + print('a:', a) + assert a > 0 + +@pytest.mark.parametrize("a", [1, 2, 10, 11, 2, 1, 12, 11]) +def test_params(a): + print('a:', a) + assert a > 0 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 02/12] 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 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..09d2cbed929 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") From f61194f891c3c5aeffe38d0f89e5c3892bb25d92 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 11 Oct 2023 06:38:41 +0000 Subject: [PATCH 03/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- testing/test_python.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/testing/test_python.py b/testing/test_python.py index 117571bc1ce..4144dbf0119 100644 --- a/testing/test_python.py +++ b/testing/test_python.py @@ -1,11 +1,13 @@ import pytest -@pytest.mark.parametrize("a", [1, 2, 10, 11, 2, 1, 12, 11,2_1]) + +@pytest.mark.parametrize("a", [1, 2, 10, 11, 2, 1, 12, 11, 2_1]) def test_params(a): - print('a:', a) + print("a:", a) assert a > 0 + @pytest.mark.parametrize("a", [1, 2, 10, 11, 2, 1, 12, 11]) def test_params(a): - print('a:', a) + print("a:", a) assert a > 0 From 9629387e9a5604603f8f1b926690e9b3e3c828fa Mon Sep 17 00:00:00 2001 From: TanyaAgarwal28 <8979149361t@gmail.com> Date: Wed, 11 Oct 2023 12:20:07 +0530 Subject: [PATCH 04/12] Bug Fix 11282: config.getini returns an empty list for an option of type string absent in INI file --- testing/test_python.py | 11 ----------- 1 file changed, 11 deletions(-) delete mode 100644 testing/test_python.py diff --git a/testing/test_python.py b/testing/test_python.py deleted file mode 100644 index 117571bc1ce..00000000000 --- a/testing/test_python.py +++ /dev/null @@ -1,11 +0,0 @@ -import pytest - -@pytest.mark.parametrize("a", [1, 2, 10, 11, 2, 1, 12, 11,2_1]) -def test_params(a): - print('a:', a) - assert a > 0 - -@pytest.mark.parametrize("a", [1, 2, 10, 11, 2, 1, 12, 11]) -def test_params(a): - print('a:', a) - assert a > 0 From 888e9e82d2fa75e9b75274c3f59d4a97e729480f Mon Sep 17 00:00:00 2001 From: TanyaAgarwal28 <8979149361t@gmail.com> Date: Wed, 11 Oct 2023 12:35:14 +0530 Subject: [PATCH 05/12] Bug Fix 11282: config.getini returns an empty list for an option of type string absent in INI file --- testing/test_conftest.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/testing/test_conftest.py b/testing/test_conftest.py index 09d2cbed929..52df31dbdbd 100644 --- a/testing/test_conftest.py +++ b/testing/test_conftest.py @@ -240,6 +240,16 @@ def my_option(request): ), encoding="utf-8", ) + # Create a simple test function + testdir.joinpath("test_my_option.py").write_text( + textwrap.dedent( + """\ + def test_example(my_option): + assert my_option is None + """ + ), + encoding="utf-8", + ) result = pytester.runpytest(str(testdir)) assert result.ret == 0 captured_stdout = result.stdout.str() From b61719d479eb8c6c37234e7f865d187dbe50f7a9 Mon Sep 17 00:00:00 2001 From: TanyaAgarwal28 <8979149361t@gmail.com> Date: Wed, 11 Oct 2023 12:57:09 +0530 Subject: [PATCH 06/12] Bug Fix 11282: config.getini returns an empty list for an option of type string absent in INI file --- testing/test_conftest.py | 43 ++++++++++++++-------------------------- 1 file changed, 15 insertions(+), 28 deletions(-) diff --git a/testing/test_conftest.py b/testing/test_conftest.py index 52df31dbdbd..058b40c583b 100644 --- a/testing/test_conftest.py +++ b/testing/test_conftest.py @@ -223,37 +223,24 @@ def test_setinitial_conftest_subdirs(pytester: Pytester, name: str) -> None: 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", - ) + conftest_content = """ + def pytest_addoption(parser): + parser.addini( + "my_option", + type="string", + default=None, + help="My option", + ) + """ + testdir.joinpath("conftest.py").write_text(conftest_content, encoding="utf-8") # Create a simple test function - testdir.joinpath("test_my_option.py").write_text( - textwrap.dedent( - """\ - def test_example(my_option): - assert my_option is None - """ - ), - encoding="utf-8", - ) + 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)) assert result.ret == 0 - captured_stdout = result.stdout.str() - assert "1 passed" in captured_stdout def test_conftest_confcutdir(pytester: Pytester) -> None: From e07b993aca63cebf980a95296576817bfe7b658e Mon Sep 17 00:00:00 2001 From: TanyaAgarwal28 <8979149361t@gmail.com> Date: Wed, 11 Oct 2023 12:59:02 +0530 Subject: [PATCH 07/12] Bug Fix 11282: config.getini returns an empty list for an option of type string absent in INI file --- testing/test_conftest.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/testing/test_conftest.py b/testing/test_conftest.py index 058b40c583b..ca97b261100 100644 --- a/testing/test_conftest.py +++ b/testing/test_conftest.py @@ -224,6 +224,7 @@ 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", @@ -231,6 +232,9 @@ def pytest_addoption(parser): default=None, help="My option", ) + @pytest.fixture(scope='session') + def my_option(request): + return request.config.getini("my_option") """ testdir.joinpath("conftest.py").write_text(conftest_content, encoding="utf-8") # Create a simple test function From af8162b90eee8fc9cdfc9dacf452e66d9ca9661b Mon Sep 17 00:00:00 2001 From: TanyaAgarwal28 <8979149361t@gmail.com> Date: Wed, 11 Oct 2023 13:04:40 +0530 Subject: [PATCH 08/12] Bug Fix 11282: config.getini returns an empty list for an option of type string absent in INI file --- testing/test_conftest.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/testing/test_conftest.py b/testing/test_conftest.py index ca97b261100..0b671c6bf52 100644 --- a/testing/test_conftest.py +++ b/testing/test_conftest.py @@ -224,23 +224,23 @@ 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") + 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") """ testdir.joinpath("conftest.py").write_text(conftest_content, encoding="utf-8") # Create a simple test function test_content = """ - def test_example(my_option): - assert my_option is None + 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)) From 33eeda0339b84e555efc23f6cb334eeef55975e1 Mon Sep 17 00:00:00 2001 From: TanyaAgarwal28 <8979149361t@gmail.com> Date: Wed, 11 Oct 2023 14:37:29 +0530 Subject: [PATCH 09/12] Bug Fix 11282: config.getini returns an empty list for an option of type string absent in INI file --- testing/test_conftest.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/testing/test_conftest.py b/testing/test_conftest.py index 0b671c6bf52..f54ba2312a9 100644 --- a/testing/test_conftest.py +++ b/testing/test_conftest.py @@ -236,14 +236,20 @@ def pytest_addoption(parser): def my_option(request): return request.config.getini("my_option") """ - testdir.joinpath("conftest.py").write_text(conftest_content, encoding="utf-8") + # Place conftest.py in the root directory of the project + testdir.parent.joinpath("conftest.py").write_text( + conftest_content, encoding="utf-8" + ) + # 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)) + result = pytester.runpytest( + str(testdir.parent) + ) # Run pytest from the root directory assert result.ret == 0 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 10/12] 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 06116fee49d..553cd45b5dd 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 f54ba2312a9..2760b14c9a2 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: From cfa662e493c7968b636bec300bdc0cfd1ecaeeb0 Mon Sep 17 00:00:00 2001 From: TanyaAgarwal28 <8979149361t@gmail.com> Date: Wed, 11 Oct 2023 15:23:31 +0530 Subject: [PATCH 11/12] 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 | 40 +++++++++++++++++++++++++++++++++------- 2 files changed, 33 insertions(+), 21 deletions(-) diff --git a/testing/conftest.py b/testing/conftest.py index 553cd45b5dd..06116fee49d 100644 --- a/testing/conftest.py +++ b/testing/conftest.py @@ -147,20 +147,6 @@ 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 2760b14c9a2..fc6f6f3ad8e 100644 --- a/testing/test_conftest.py +++ b/testing/test_conftest.py @@ -222,14 +222,40 @@ def test_setinitial_conftest_subdirs(pytester: Pytester, name: str) -> None: def test_my_option(pytester: Pytester): - p = pytester.makepyfile( - """ - def test_x(my_option): - assert my_option is None - """ + 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" ) - result = pytester.runpytest(p) - result.assert_outcomes(failed=0, passed=1) + + # 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 def test_conftest_confcutdir(pytester: Pytester) -> None: From 2bb9e30e088c5e4e397c2e19bbe695773f1fa3c9 Mon Sep 17 00:00:00 2001 From: TanyaAgarwal28 <8979149361t@gmail.com> Date: Wed, 11 Oct 2023 15:32:25 +0530 Subject: [PATCH 12/12] Bug Fix 11282: config.getini returns an empty list for an option of type string absent in INI file --- testing/test_conftest.py | 56 +++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 33 deletions(-) diff --git a/testing/test_conftest.py b/testing/test_conftest.py index fc6f6f3ad8e..665d438fdaf 100644 --- a/testing/test_conftest.py +++ b/testing/test_conftest.py @@ -222,40 +222,30 @@ 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" + 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") + """ ) - - # 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 + 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: