From baa938eea534b996066ce38f47a672253b346161 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Wed, 18 Jan 2023 06:42:46 +0100 Subject: [PATCH 01/17] [7.2.x] Derive pytest.raises from AbstractContextManager --- changelog/10660.bugfix.rst | 2 ++ src/_pytest/python_api.py | 4 ++-- testing/typing_checks.py | 11 +++++++++++ 3 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 changelog/10660.bugfix.rst diff --git a/changelog/10660.bugfix.rst b/changelog/10660.bugfix.rst new file mode 100644 index 00000000000..62e3549413b --- /dev/null +++ b/changelog/10660.bugfix.rst @@ -0,0 +1,2 @@ +Fix :py:func:`pytest.raises` to return a 'ContextManager' so that type-checkers could narrow +:code:`pytest.raises(...) if ... else nullcontext()` down to 'ContextManager' rather than 'object'. diff --git a/src/_pytest/python_api.py b/src/_pytest/python_api.py index 515d437f0d8..20908cb67b8 100644 --- a/src/_pytest/python_api.py +++ b/src/_pytest/python_api.py @@ -8,7 +8,7 @@ from typing import Any from typing import Callable from typing import cast -from typing import Generic +from typing import ContextManager from typing import List from typing import Mapping from typing import Optional @@ -957,7 +957,7 @@ def raises( # noqa: F811 @final -class RaisesContext(Generic[E]): +class RaisesContext(ContextManager[_pytest._code.ExceptionInfo[E]]): def __init__( self, expected_exception: Union[Type[E], Tuple[Type[E], ...]], diff --git a/testing/typing_checks.py b/testing/typing_checks.py index 0a6b5ad2841..d15b3988bb5 100644 --- a/testing/typing_checks.py +++ b/testing/typing_checks.py @@ -3,6 +3,11 @@ This file is not executed, it is only checked by mypy to ensure that none of the code triggers any mypy errors. """ +import contextlib +from typing import Optional + +from typing_extensions import assert_type + import pytest @@ -22,3 +27,9 @@ def check_fixture_ids_callable() -> None: @pytest.mark.parametrize("func", [str, int], ids=lambda x: str(x.__name__)) def check_parametrize_ids_callable(func) -> None: pass + + +def check_raises_is_a_context_manager(val: bool) -> None: + with pytest.raises(RuntimeError) if val else contextlib.nullcontext() as excinfo: + pass + assert_type(excinfo, Optional[pytest.ExceptionInfo[RuntimeError]]) From 5bf361f24e27aa9d89b086bc44925bb689b7406f Mon Sep 17 00:00:00 2001 From: Ramsey <37647536@qq.com> Date: Thu, 19 Jan 2023 20:44:57 +0800 Subject: [PATCH 02/17] Fix crash if `--cache-show` and `--help` are passed at the same time Closes #10592 (cherry picked from commit 4d4ed42c34fbd6b30bff1bbdf664aeac3300a83a) --- AUTHORS | 2 ++ changelog/10592.bugfix.rst | 1 + src/_pytest/cacheprovider.py | 3 +-- testing/test_cacheprovider.py | 5 +++++ 4 files changed, 9 insertions(+), 2 deletions(-) create mode 100644 changelog/10592.bugfix.rst diff --git a/AUTHORS b/AUTHORS index fa99cd7dd0e..e36a10edbb0 100644 --- a/AUTHORS +++ b/AUTHORS @@ -374,6 +374,8 @@ Xixi Zhao Xuan Luong Xuecong Liao Yoav Caspi +Yuliang Shao +Yusuke Kadowaki Yuval Shimon Zac Hatfield-Dodds Zachary Kneupper diff --git a/changelog/10592.bugfix.rst b/changelog/10592.bugfix.rst new file mode 100644 index 00000000000..62c1c7e3c34 --- /dev/null +++ b/changelog/10592.bugfix.rst @@ -0,0 +1 @@ +Fixed crash if `--cache-show` and `--help` are passed at the same time. diff --git a/src/_pytest/cacheprovider.py b/src/_pytest/cacheprovider.py index 777c1b0b05a..c236dd4176b 100755 --- a/src/_pytest/cacheprovider.py +++ b/src/_pytest/cacheprovider.py @@ -32,7 +32,6 @@ from _pytest.python import Package from _pytest.reports import TestReport - README_CONTENT = """\ # pytest cache directory # @@ -492,7 +491,7 @@ def pytest_addoption(parser: Parser) -> None: def pytest_cmdline_main(config: Config) -> Optional[Union[int, ExitCode]]: - if config.option.cacheshow: + if config.option.cacheshow and not config.option.help: from _pytest.main import wrap_session return wrap_session(config, cacheshow) diff --git a/testing/test_cacheprovider.py b/testing/test_cacheprovider.py index 2baa3c8f189..c381a8448ce 100644 --- a/testing/test_cacheprovider.py +++ b/testing/test_cacheprovider.py @@ -1249,3 +1249,8 @@ def test_cachedir_tag(pytester: Pytester) -> None: cache.set("foo", "bar") cachedir_tag_path = cache._cachedir.joinpath("CACHEDIR.TAG") assert cachedir_tag_path.read_bytes() == CACHEDIR_TAG_CONTENT + + +def test_clioption_with_cacheshow_and_help(pytester: Pytester) -> None: + result = pytester.runpytest("--cache-show", "--help") + assert result.ret == 0 From d1b9660402ca38863126298266512aebb53cbefe Mon Sep 17 00:00:00 2001 From: q0w <43147888+q0w@users.noreply.github.com> Date: Sat, 21 Jan 2023 14:19:54 +0300 Subject: [PATCH 03/17] [7.2.x] Check if config args and args_source exist --- AUTHORS | 1 + changelog/10626.bugfix.rst | 1 + src/_pytest/config/__init__.py | 6 ++++-- testing/python/fixtures.py | 4 ++++ 4 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 changelog/10626.bugfix.rst diff --git a/AUTHORS b/AUTHORS index e36a10edbb0..f048657d7de 100644 --- a/AUTHORS +++ b/AUTHORS @@ -286,6 +286,7 @@ Prashant Sharma Pulkit Goyal Punyashloka Biswal Quentin Pradet +q0w Ralf Schmitt Ram Rachum Ralph Giles diff --git a/changelog/10626.bugfix.rst b/changelog/10626.bugfix.rst new file mode 100644 index 00000000000..c3c7ef77840 --- /dev/null +++ b/changelog/10626.bugfix.rst @@ -0,0 +1 @@ +Fix crash if ``--fixtures`` and ``--help`` are passed at the same time. diff --git a/src/_pytest/config/__init__.py b/src/_pytest/config/__init__.py index 25f156f8b20..bd2611df6b1 100644 --- a/src/_pytest/config/__init__.py +++ b/src/_pytest/config/__init__.py @@ -998,6 +998,8 @@ def __init__( self.hook.pytest_addoption.call_historic( kwargs=dict(parser=self._parser, pluginmanager=self.pluginmanager) ) + self.args_source = Config.ArgsSource.ARGS + self.args: List[str] = [] if TYPE_CHECKING: from _pytest.cacheprovider import Cache @@ -1337,8 +1339,8 @@ def _get_unknown_ini_keys(self) -> List[str]: def parse(self, args: List[str], addopts: bool = True) -> None: # Parse given cmdline arguments into this config object. - assert not hasattr( - self, "args" + assert ( + self.args == [] ), "can only parse cmdline args at most once per Config object" self.hook.pytest_addhooks.call_historic( kwargs=dict(pluginmanager=self.pluginmanager) diff --git a/testing/python/fixtures.py b/testing/python/fixtures.py index 3ce5cb34ddd..d996f80bb93 100644 --- a/testing/python/fixtures.py +++ b/testing/python/fixtures.py @@ -3338,6 +3338,10 @@ def test_funcarg_compat(self, pytester: Pytester) -> None: config = pytester.parseconfigure("--funcargs") assert config.option.showfixtures + def test_show_help(self, pytester: Pytester) -> None: + result = pytester.runpytest("--fixtures", "--help") + assert not result.ret + def test_show_fixtures(self, pytester: Pytester) -> None: result = pytester.runpytest("--fixtures") result.stdout.fnmatch_lines( From 76bef68f3e135f26d6afe444c9676a22c5cc01b6 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue, 24 Jan 2023 10:30:54 +0000 Subject: [PATCH 04/17] [7.2.x] Add check for zero denominator in approx (#10689) Co-authored-by: Jay <43951088+jayendra-patil33@users.noreply.github.com> --- changelog/10533.bugfix.rst | 1 + src/_pytest/python_api.py | 14 ++++++++++---- testing/python/approx.py | 13 +++++++++++++ 3 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 changelog/10533.bugfix.rst diff --git a/changelog/10533.bugfix.rst b/changelog/10533.bugfix.rst new file mode 100644 index 00000000000..c49e4c3aabf --- /dev/null +++ b/changelog/10533.bugfix.rst @@ -0,0 +1 @@ +Fix :func:`pytest.approx` handling of dictionaries containing one or more values of `0.0` in class ApproxMapping. diff --git a/src/_pytest/python_api.py b/src/_pytest/python_api.py index 20908cb67b8..7afc2d96570 100644 --- a/src/_pytest/python_api.py +++ b/src/_pytest/python_api.py @@ -269,10 +269,16 @@ def _repr_compare(self, other_side: Mapping[object, float]) -> List[str]: max_abs_diff = max( max_abs_diff, abs(approx_value.expected - other_value) ) - max_rel_diff = max( - max_rel_diff, - abs((approx_value.expected - other_value) / approx_value.expected), - ) + if approx_value.expected == 0.0: + max_rel_diff = math.inf + else: + max_rel_diff = max( + max_rel_diff, + abs( + (approx_value.expected - other_value) + / approx_value.expected + ), + ) different_ids.append(approx_key) message_data = [ diff --git a/testing/python/approx.py b/testing/python/approx.py index 6acb466ffb1..631e52b56ac 100644 --- a/testing/python/approx.py +++ b/testing/python/approx.py @@ -630,6 +630,19 @@ def test_dict_nonnumeric(self): def test_dict_vs_other(self): assert 1 != approx({"a": 0}) + def test_dict_for_div_by_zero(self, assert_approx_raises_regex): + assert_approx_raises_regex( + {"foo": 42.0}, + {"foo": 0.0}, + [ + r" comparison failed. Mismatched elements: 1 / 1:", + rf" Max absolute difference: {SOME_FLOAT}", + r" Max relative difference: inf", + r" Index \| Obtained\s+\| Expected ", + rf" foo | {SOME_FLOAT} \| {SOME_FLOAT} ± {SOME_FLOAT}", + ], + ) + def test_numpy_array(self): np = pytest.importorskip("numpy") From 4e14609b998f8198ce6b6f4177b1b34c8cb05ba4 Mon Sep 17 00:00:00 2001 From: vin01 <30344579+vin01@users.noreply.github.com> Date: Fri, 27 Jan 2023 12:11:00 +0100 Subject: [PATCH 05/17] [7.2.x] Clarify docs for `match` regarding escaping --- doc/en/how-to/capture-warnings.rst | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/doc/en/how-to/capture-warnings.rst b/doc/en/how-to/capture-warnings.rst index 91565002ccc..51e85badd2a 100644 --- a/doc/en/how-to/capture-warnings.rst +++ b/doc/en/how-to/capture-warnings.rst @@ -270,20 +270,34 @@ which works in a similar manner to :ref:`raises ` (except that warnings.warn("my warning", UserWarning) The test will fail if the warning in question is not raised. Use the keyword -argument ``match`` to assert that the warning matches a text or regex:: +argument ``match`` to assert that the warning matches a text or regex. +To match a literal string that may contain regular expression metacharacters like ``(`` or ``.``, the pattern can +first be escaped with ``re.escape``. - >>> with warns(UserWarning, match='must be 0 or None'): +Some examples: + +.. code-block:: pycon + + + >>> with warns(UserWarning, match="must be 0 or None"): ... warnings.warn("value must be 0 or None", UserWarning) + ... - >>> with warns(UserWarning, match=r'must be \d+$'): + >>> with warns(UserWarning, match=r"must be \d+$"): ... warnings.warn("value must be 42", UserWarning) + ... - >>> with warns(UserWarning, match=r'must be \d+$'): + >>> with warns(UserWarning, match=r"must be \d+$"): ... warnings.warn("this is not here", UserWarning) + ... Traceback (most recent call last): ... Failed: DID NOT WARN. No warnings of type ...UserWarning... were emitted... + >>> with warns(UserWarning, match=re.escape("issue with foo() func")): + ... warnings.warn("issue with foo() func") + ... + You can also call :func:`pytest.warns` on a function or code string: .. code-block:: python From 432a60bd522d18a1f8c976e9a72e2dcf70f3aef0 Mon Sep 17 00:00:00 2001 From: Teejay Date: Fri, 27 Jan 2023 09:33:46 -0800 Subject: [PATCH 06/17] [7.2.x] Fix fixtures named teardown being considered by nose --- AUTHORS | 1 + changelog/10597.bugfix.rst | 1 + src/_pytest/python.py | 6 +++--- testing/test_nose.py | 21 +++++++++++++++++++++ 4 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 changelog/10597.bugfix.rst diff --git a/AUTHORS b/AUTHORS index f048657d7de..697cca65be5 100644 --- a/AUTHORS +++ b/AUTHORS @@ -344,6 +344,7 @@ Thomas Grainger Thomas Hisch Tim Hoffmann Tim Strazny +TJ Bruno Tobias Diez Tom Dalton Tom Viner diff --git a/changelog/10597.bugfix.rst b/changelog/10597.bugfix.rst new file mode 100644 index 00000000000..9070e74a310 --- /dev/null +++ b/changelog/10597.bugfix.rst @@ -0,0 +1 @@ +Fix bug where a fixture method named ``teardown`` would be called as part of ``nose`` teardown stage. diff --git a/src/_pytest/python.py b/src/_pytest/python.py index 1e30d42ce9c..75d64fbdf67 100644 --- a/src/_pytest/python.py +++ b/src/_pytest/python.py @@ -848,7 +848,7 @@ def _inject_setup_class_fixture(self) -> None: other fixtures (#517). """ setup_class = _get_first_non_fixture_func(self.obj, ("setup_class",)) - teardown_class = getattr(self.obj, "teardown_class", None) + teardown_class = _get_first_non_fixture_func(self.obj, ("teardown_class",)) if setup_class is None and teardown_class is None: return @@ -885,12 +885,12 @@ def _inject_setup_method_fixture(self) -> None: emit_nose_setup_warning = True setup_method = _get_first_non_fixture_func(self.obj, (setup_name,)) teardown_name = "teardown_method" - teardown_method = getattr(self.obj, teardown_name, None) + teardown_method = _get_first_non_fixture_func(self.obj, (teardown_name,)) emit_nose_teardown_warning = False if teardown_method is None and has_nose: teardown_name = "teardown" emit_nose_teardown_warning = True - teardown_method = getattr(self.obj, teardown_name, None) + teardown_method = _get_first_non_fixture_func(self.obj, (teardown_name,)) if setup_method is None and teardown_method is None: return diff --git a/testing/test_nose.py b/testing/test_nose.py index 92d6b95fd87..e838e79ddd5 100644 --- a/testing/test_nose.py +++ b/testing/test_nose.py @@ -496,3 +496,24 @@ def test_it(): ) result = pytester.runpytest(p, "-p", "nose") assert result.ret == 0 + + +@pytest.mark.parametrize("fixture_name", ("teardown", "teardown_class")) +def test_teardown_fixture_not_called_directly(fixture_name, pytester: Pytester) -> None: + """Regression test for #10597.""" + p = pytester.makepyfile( + f""" + import pytest + + class TestHello: + + @pytest.fixture + def {fixture_name}(self): + yield + + def test_hello(self, {fixture_name}): + assert True + """ + ) + result = pytester.runpytest(p, "-p", "nose") + assert result.ret == 0 From 7e1549dfb67d45c3e9e75e4112a89b4bacff51bf Mon Sep 17 00:00:00 2001 From: Mahesh Vashishtha Date: Tue, 7 Feb 2023 16:27:34 -0600 Subject: [PATCH 07/17] [7.2.x] DOCS-#10687: Add a note about -W vs filterwarnings. --- doc/en/how-to/capture-warnings.rst | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/doc/en/how-to/capture-warnings.rst b/doc/en/how-to/capture-warnings.rst index 51e85badd2a..0390230b8ea 100644 --- a/doc/en/how-to/capture-warnings.rst +++ b/doc/en/how-to/capture-warnings.rst @@ -109,6 +109,18 @@ When a warning matches more than one option in the list, the action for the last is performed. +.. note:: + + The ``-W`` flag and the ``filterwarnings`` ini option use warning filters that are + similar in structure, but each configuration option interprets its filter + differently. For example, *message* in ``filterwarnings`` is a string containing a + regular expression that the start of the warning message must match, + case-insensitively, while *message* in ``-W`` is a literal string that the start of + the warning message must contain (case-insensitively), ignoring any whitespace at + the start or end of message. Consult the `warning filter`_ documentation for more + details. + + .. _`filterwarnings`: ``@pytest.mark.filterwarnings`` From 1e83bd83867a4f8c283c9080668769cdd5900526 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Sun, 12 Feb 2023 21:37:40 -0300 Subject: [PATCH 08/17] Use build-and-inspect-python-package action (#10722) This uses https://github.com/hynek/build-and-inspect-python-package to ensure our package is correct, both during testing and deploy, --- .github/workflows/deploy.yml | 29 +++++++++++++++++------------ .github/workflows/test.yml | 12 ++++++++++++ pyproject.toml | 5 +++++ 3 files changed, 34 insertions(+), 12 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 01b6fb4089b..ecfa3071f58 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -28,25 +28,30 @@ jobs: fetch-depth: 0 persist-credentials: false - - name: Set up Python - uses: actions/setup-python@v2 - with: - python-version: "3.7" - - - name: Install dependencies - run: | - python -m pip install --upgrade pip - pip install --upgrade build tox + - name: Build and Check Package + uses: hynek/build-and-inspect-python-package@v1.5 - - name: Build package - run: | - python -m build + - name: Download Package + uses: actions/download-artifact@v3 + with: + name: Packages + path: dist - name: Publish package to PyPI uses: pypa/gh-action-pypi-publish@release/v1 with: password: ${{ secrets.pypi_token }} + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: "3.7" + + - name: Install tox + run: | + python -m pip install --upgrade pip + pip install --upgrade tox + - name: Publish GitHub release notes env: GH_RELEASE_NOTES_TOKEN: ${{ github.token }} diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 524260961b4..cd1ffdbf9d4 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -18,6 +18,11 @@ on: env: PYTEST_ADDOPTS: "--color=yes" +# Cancel running jobs for the same workflow and branch. +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + # Set permissions at the job level. permissions: {} @@ -189,3 +194,10 @@ jobs: fail_ci_if_error: true files: ./coverage.xml verbose: true + + check-package: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Build and Check Package + uses: hynek/build-and-inspect-python-package@v1.5 diff --git a/pyproject.toml b/pyproject.toml index fc9a119f6f0..a4139a5c051 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -114,3 +114,8 @@ template = "changelog/_template.rst" [tool.black] target-version = ['py37'] + +# check-wheel-contents is executed by the build-and-inspect-python-package action. +[tool.check-wheel-contents] +# W009: Wheel contains multiple toplevel library entries +ignore = "W009" From 37e410fce841635c6d3b4ba6b37c6a8df360b40a Mon Sep 17 00:00:00 2001 From: Garvit Shubham <70941313+itsmegarvi@users.noreply.github.com> Date: Tue, 14 Feb 2023 19:27:32 +0530 Subject: [PATCH 09/17] [7.2.x] Fix entry-points declaration in the documentation example using Hatch --- changelog/10721.doc.rst | 1 + doc/en/how-to/writing_plugins.rst | 9 ++------- 2 files changed, 3 insertions(+), 7 deletions(-) create mode 100644 changelog/10721.doc.rst diff --git a/changelog/10721.doc.rst b/changelog/10721.doc.rst new file mode 100644 index 00000000000..33ba405124c --- /dev/null +++ b/changelog/10721.doc.rst @@ -0,0 +1 @@ +Fixed entry-points declaration in the documentation example using Hatch. diff --git a/doc/en/how-to/writing_plugins.rst b/doc/en/how-to/writing_plugins.rst index f15b69c2317..4eee1b2e120 100644 --- a/doc/en/how-to/writing_plugins.rst +++ b/doc/en/how-to/writing_plugins.rst @@ -167,13 +167,8 @@ it in your ``pyproject.toml`` file. "Framework :: Pytest", ] - [tool.setuptools] - packages = ["myproject"] - - [project.entry_points] - pytest11 = [ - "myproject = myproject.pluginmodule", - ] + [project.entry-points.pytest11] + myproject = "myproject.pluginmodule" If a package is installed this way, ``pytest`` will load ``myproject.pluginmodule`` as a plugin which can define From 779a87aada33af444f14841a04344016a087669e Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed, 15 Feb 2023 16:07:30 +0100 Subject: [PATCH 10/17] [7.2.x] Update open training (#10740) Co-authored-by: Florian Bruhin --- doc/en/index.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/en/index.rst b/doc/en/index.rst index 0c94d9d4625..c277cc30721 100644 --- a/doc/en/index.rst +++ b/doc/en/index.rst @@ -2,7 +2,7 @@ .. sidebar:: Next Open Trainings - - `Professional Testing with Python `_, via `Python Academy `_, March 7th to 9th 2023 (3 day in-depth training), Remote and Leipzig, Germany + - `Professional Testing with Python `_, via `Python Academy `_, March 7th to 9th 2023 (3 day in-depth training), Remote Also see :doc:`previous talks and blogposts `. From 79108bf9a37aebe1879c545cdf926251772cc05e Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sat, 18 Feb 2023 18:57:28 -0300 Subject: [PATCH 11/17] [7.2.x] add CI and BUILD_NUMBER env var in docs (#10750) Co-authored-by: bitzge <4791819+bitzge@users.noreply.github.com> --- changelog/10690.doc.rst | 1 + doc/en/reference/reference.rst | 8 ++++++++ 2 files changed, 9 insertions(+) create mode 100644 changelog/10690.doc.rst diff --git a/changelog/10690.doc.rst b/changelog/10690.doc.rst new file mode 100644 index 00000000000..faf61b3cc44 --- /dev/null +++ b/changelog/10690.doc.rst @@ -0,0 +1 @@ +Added `CI` and `BUILD_NUMBER` environment variables to the documentation. diff --git a/doc/en/reference/reference.rst b/doc/en/reference/reference.rst index b7bd4d1199a..bbad27e09cd 100644 --- a/doc/en/reference/reference.rst +++ b/doc/en/reference/reference.rst @@ -1047,6 +1047,14 @@ Environment Variables Environment variables that can be used to change pytest's behavior. +.. envvar:: CI + +When set (regardless of value), pytest acknowledges that is running in a CI process. Alterative to ``BUILD_NUMBER`` variable. + +.. envvar:: BUILD_NUMBER + +When set (regardless of value), pytest acknowledges that is running in a CI process. Alterative to CI variable. + .. envvar:: PYTEST_ADDOPTS This contains a command-line (parsed by the py:mod:`shlex` module) that will be **prepended** to the command line given From cee8d6f274213f790ca286cb2d0b9390c6234c6c Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sat, 18 Feb 2023 18:57:56 -0300 Subject: [PATCH 12/17] [7.2.x] Update import mode documentation to not refer to __import__() anymore. (#10751) Co-authored-by: Manuel Jacob --- doc/en/explanation/pythonpath.rst | 6 +++--- src/_pytest/pathlib.py | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/doc/en/explanation/pythonpath.rst b/doc/en/explanation/pythonpath.rst index e1d240b498a..5b533f47fdc 100644 --- a/doc/en/explanation/pythonpath.rst +++ b/doc/en/explanation/pythonpath.rst @@ -16,7 +16,7 @@ import process can be controlled through the ``--import-mode`` command-line flag these values: * ``prepend`` (default): the directory path containing each module will be inserted into the *beginning* - of :py:data:`sys.path` if not already there, and then imported with the :func:`__import__ <__import__>` builtin. + of :py:data:`sys.path` if not already there, and then imported with the :func:`importlib.import_module ` function. This requires test module names to be unique when the test directory tree is not arranged in packages, because the modules will put in :py:data:`sys.modules` after importing. @@ -24,7 +24,7 @@ these values: This is the classic mechanism, dating back from the time Python 2 was still supported. * ``append``: the directory containing each module is appended to the end of :py:data:`sys.path` if not already - there, and imported with ``__import__``. + there, and imported with :func:`importlib.import_module `. This better allows to run test modules against installed versions of a package even if the package under test has the same import root. For example: @@ -43,7 +43,7 @@ these values: Same as ``prepend``, requires test module names to be unique when the test directory tree is not arranged in packages, because the modules will put in :py:data:`sys.modules` after importing. -* ``importlib``: new in pytest-6.0, this mode uses :mod:`importlib` to import test modules. This gives full control over the import process, and doesn't require changing :py:data:`sys.path`. +* ``importlib``: new in pytest-6.0, this mode uses more fine control mechanisms provided by :mod:`importlib` to import test modules. This gives full control over the import process, and doesn't require changing :py:data:`sys.path`. For this reason this doesn't require test module names to be unique. diff --git a/src/_pytest/pathlib.py b/src/_pytest/pathlib.py index c5a411b5963..3086fa78250 100644 --- a/src/_pytest/pathlib.py +++ b/src/_pytest/pathlib.py @@ -464,14 +464,14 @@ def import_path( * `mode == ImportMode.prepend`: the directory containing the module (or package, taking `__init__.py` files into account) will be put at the *start* of `sys.path` before - being imported with `__import__. + being imported with `importlib.import_module`. * `mode == ImportMode.append`: same as `prepend`, but the directory will be appended to the end of `sys.path`, if not already in `sys.path`. * `mode == ImportMode.importlib`: uses more fine control mechanisms provided by `importlib` - to import the module, which avoids having to use `__import__` and muck with `sys.path` - at all. It effectively allows having same-named test modules in different places. + to import the module, which avoids having to muck with `sys.path` at all. It effectively + allows having same-named test modules in different places. :param root: Used as an anchor when mode == ImportMode.importlib to obtain From 2fd4549db50e0086ce163654c8e060478111f05a Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue, 28 Feb 2023 16:03:47 +0000 Subject: [PATCH 13/17] [7.2.x] docs: be more explicit about module level skip preventing collection (#10777) Co-authored-by: Ronny Pfannschmidt --- changelog/10753.doc.rst | 2 ++ src/_pytest/outcomes.py | 8 ++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) create mode 100644 changelog/10753.doc.rst diff --git a/changelog/10753.doc.rst b/changelog/10753.doc.rst new file mode 100644 index 00000000000..701d81a7c2c --- /dev/null +++ b/changelog/10753.doc.rst @@ -0,0 +1,2 @@ +Change wording of the module level skip to be very explicit +about not collecting and not executing the rest of the module. diff --git a/src/_pytest/outcomes.py b/src/_pytest/outcomes.py index e46b663dd50..3efd1de7fbb 100644 --- a/src/_pytest/outcomes.py +++ b/src/_pytest/outcomes.py @@ -157,8 +157,12 @@ def skip( The message to show the user as reason for the skip. :param allow_module_level: - Allows this function to be called at module level, skipping the rest - of the module. Defaults to False. + Allows this function to be called at module level. + Raising the skip exception at module level will stop + the execution of the module and prevent the collection of all tests in the module, + even those defined before the `skip` call. + + Defaults to False. :param msg: Same as ``reason``, but deprecated. Will be removed in a future version, use ``reason`` instead. From e8e7d44a4c28fc608ada23030f2883b922155218 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue, 28 Feb 2023 16:08:54 +0000 Subject: [PATCH 14/17] [7.2.x] made minor updates to fixtures docs (#10778) Co-authored-by: Billy --- doc/en/example/fixtures/test_fixtures_order_dependencies.py | 2 +- doc/en/reference/fixtures.rst | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/en/example/fixtures/test_fixtures_order_dependencies.py b/doc/en/example/fixtures/test_fixtures_order_dependencies.py index b3512c2a64d..e76e3f93c62 100644 --- a/doc/en/example/fixtures/test_fixtures_order_dependencies.py +++ b/doc/en/example/fixtures/test_fixtures_order_dependencies.py @@ -17,7 +17,7 @@ def b(a, order): @pytest.fixture -def c(a, b, order): +def c(b, order): order.append("c") diff --git a/doc/en/reference/fixtures.rst b/doc/en/reference/fixtures.rst index d25979ab95d..01f825222ea 100644 --- a/doc/en/reference/fixtures.rst +++ b/doc/en/reference/fixtures.rst @@ -335,7 +335,7 @@ For example: .. literalinclude:: /example/fixtures/test_fixtures_order_dependencies.py -If we map out what depends on what, we get something that look like this: +If we map out what depends on what, we get something that looks like this: .. image:: /example/fixtures/test_fixtures_order_dependencies.* :align: center From 9693556f27ee77ac74c380943b9b8e5b926edcf1 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Fri, 3 Mar 2023 15:52:49 +0000 Subject: [PATCH 15/17] [7.2.x] Fix test_cmdline_python_namespace_package (#10789) Co-authored-by: Bruno Oliveira --- testing/acceptance_test.py | 9 ++++++++- testing/test_monkeypatch.py | 3 +++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/testing/acceptance_test.py b/testing/acceptance_test.py index c7139b538b2..73d0cc785c2 100644 --- a/testing/acceptance_test.py +++ b/testing/acceptance_test.py @@ -694,7 +694,14 @@ def test_cmdline_python_namespace_package( # mixed module and filenames: monkeypatch.chdir("world") - result = pytester.runpytest("--pyargs", "-v", "ns_pkg.hello", "ns_pkg/world") + + # pgk_resources.declare_namespace has been deprecated in favor of implicit namespace packages. + # While we could change the test to use implicit namespace packages, seems better + # to still ensure the old declaration via declare_namespace still works. + ignore_w = r"-Wignore:Deprecated call to `pkg_resources.declare_namespace" + result = pytester.runpytest( + "--pyargs", "-v", "ns_pkg.hello", "ns_pkg/world", ignore_w + ) assert result.ret == 0 result.stdout.fnmatch_lines( [ diff --git a/testing/test_monkeypatch.py b/testing/test_monkeypatch.py index 49635f95e79..b46ec05bbd2 100644 --- a/testing/test_monkeypatch.py +++ b/testing/test_monkeypatch.py @@ -425,6 +425,9 @@ class A: assert A.x == 1 +@pytest.mark.filterwarnings( + "ignore:Deprecated call to `pkg_resources.declare_namespace" +) def test_syspath_prepend_with_namespace_packages( pytester: Pytester, monkeypatch: MonkeyPatch ) -> None: From eb50c6ce9990c322f7ec1d25c8433456aa8fd7f6 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Fri, 3 Mar 2023 16:26:35 +0000 Subject: [PATCH 16/17] [7.2.x] Normalize how changelog entries are written (#10790) Co-authored-by: Bruno Oliveira --- changelog/10533.bugfix.rst | 2 +- changelog/10597.bugfix.rst | 2 +- changelog/10626.bugfix.rst | 2 +- changelog/10660.bugfix.rst | 2 +- changelog/10753.doc.rst | 4 ++-- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/changelog/10533.bugfix.rst b/changelog/10533.bugfix.rst index c49e4c3aabf..c7e49189d1f 100644 --- a/changelog/10533.bugfix.rst +++ b/changelog/10533.bugfix.rst @@ -1 +1 @@ -Fix :func:`pytest.approx` handling of dictionaries containing one or more values of `0.0` in class ApproxMapping. +Fixed :func:`pytest.approx` handling of dictionaries containing one or more values of `0.0`. diff --git a/changelog/10597.bugfix.rst b/changelog/10597.bugfix.rst index 9070e74a310..793d35b3620 100644 --- a/changelog/10597.bugfix.rst +++ b/changelog/10597.bugfix.rst @@ -1 +1 @@ -Fix bug where a fixture method named ``teardown`` would be called as part of ``nose`` teardown stage. +Fixed bug where a fixture method named ``teardown`` would be called as part of ``nose`` teardown stage. diff --git a/changelog/10626.bugfix.rst b/changelog/10626.bugfix.rst index c3c7ef77840..4032f69e02d 100644 --- a/changelog/10626.bugfix.rst +++ b/changelog/10626.bugfix.rst @@ -1 +1 @@ -Fix crash if ``--fixtures`` and ``--help`` are passed at the same time. +Fixed crash if ``--fixtures`` and ``--help`` are passed at the same time. diff --git a/changelog/10660.bugfix.rst b/changelog/10660.bugfix.rst index 62e3549413b..84a913fe980 100644 --- a/changelog/10660.bugfix.rst +++ b/changelog/10660.bugfix.rst @@ -1,2 +1,2 @@ -Fix :py:func:`pytest.raises` to return a 'ContextManager' so that type-checkers could narrow +Fixed :py:func:`pytest.raises` to return a 'ContextManager' so that type-checkers could narrow :code:`pytest.raises(...) if ... else nullcontext()` down to 'ContextManager' rather than 'object'. diff --git a/changelog/10753.doc.rst b/changelog/10753.doc.rst index 701d81a7c2c..13b021ea75b 100644 --- a/changelog/10753.doc.rst +++ b/changelog/10753.doc.rst @@ -1,2 +1,2 @@ -Change wording of the module level skip to be very explicit -about not collecting and not executing the rest of the module. +Changed wording of the module level skip to be very explicit +about not collecting tests and not executing the rest of the module. From 4191e0259805acbc499d326441c98e2b950637f1 Mon Sep 17 00:00:00 2001 From: pytest bot Date: Fri, 3 Mar 2023 17:05:52 +0000 Subject: [PATCH 17/17] Prepare release version 7.2.2 --- changelog/10533.bugfix.rst | 1 - changelog/10592.bugfix.rst | 1 - changelog/10597.bugfix.rst | 1 - changelog/10626.bugfix.rst | 1 - changelog/10660.bugfix.rst | 2 -- changelog/10690.doc.rst | 1 - changelog/10721.doc.rst | 1 - changelog/10753.doc.rst | 2 -- doc/en/announce/index.rst | 1 + doc/en/announce/release-7.2.2.rst | 25 +++++++++++++++++++++ doc/en/builtin.rst | 2 +- doc/en/changelog.rst | 36 +++++++++++++++++++++++++++++++ doc/en/getting-started.rst | 2 +- 13 files changed, 64 insertions(+), 12 deletions(-) delete mode 100644 changelog/10533.bugfix.rst delete mode 100644 changelog/10592.bugfix.rst delete mode 100644 changelog/10597.bugfix.rst delete mode 100644 changelog/10626.bugfix.rst delete mode 100644 changelog/10660.bugfix.rst delete mode 100644 changelog/10690.doc.rst delete mode 100644 changelog/10721.doc.rst delete mode 100644 changelog/10753.doc.rst create mode 100644 doc/en/announce/release-7.2.2.rst diff --git a/changelog/10533.bugfix.rst b/changelog/10533.bugfix.rst deleted file mode 100644 index c7e49189d1f..00000000000 --- a/changelog/10533.bugfix.rst +++ /dev/null @@ -1 +0,0 @@ -Fixed :func:`pytest.approx` handling of dictionaries containing one or more values of `0.0`. diff --git a/changelog/10592.bugfix.rst b/changelog/10592.bugfix.rst deleted file mode 100644 index 62c1c7e3c34..00000000000 --- a/changelog/10592.bugfix.rst +++ /dev/null @@ -1 +0,0 @@ -Fixed crash if `--cache-show` and `--help` are passed at the same time. diff --git a/changelog/10597.bugfix.rst b/changelog/10597.bugfix.rst deleted file mode 100644 index 793d35b3620..00000000000 --- a/changelog/10597.bugfix.rst +++ /dev/null @@ -1 +0,0 @@ -Fixed bug where a fixture method named ``teardown`` would be called as part of ``nose`` teardown stage. diff --git a/changelog/10626.bugfix.rst b/changelog/10626.bugfix.rst deleted file mode 100644 index 4032f69e02d..00000000000 --- a/changelog/10626.bugfix.rst +++ /dev/null @@ -1 +0,0 @@ -Fixed crash if ``--fixtures`` and ``--help`` are passed at the same time. diff --git a/changelog/10660.bugfix.rst b/changelog/10660.bugfix.rst deleted file mode 100644 index 84a913fe980..00000000000 --- a/changelog/10660.bugfix.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fixed :py:func:`pytest.raises` to return a 'ContextManager' so that type-checkers could narrow -:code:`pytest.raises(...) if ... else nullcontext()` down to 'ContextManager' rather than 'object'. diff --git a/changelog/10690.doc.rst b/changelog/10690.doc.rst deleted file mode 100644 index faf61b3cc44..00000000000 --- a/changelog/10690.doc.rst +++ /dev/null @@ -1 +0,0 @@ -Added `CI` and `BUILD_NUMBER` environment variables to the documentation. diff --git a/changelog/10721.doc.rst b/changelog/10721.doc.rst deleted file mode 100644 index 33ba405124c..00000000000 --- a/changelog/10721.doc.rst +++ /dev/null @@ -1 +0,0 @@ -Fixed entry-points declaration in the documentation example using Hatch. diff --git a/changelog/10753.doc.rst b/changelog/10753.doc.rst deleted file mode 100644 index 13b021ea75b..00000000000 --- a/changelog/10753.doc.rst +++ /dev/null @@ -1,2 +0,0 @@ -Changed wording of the module level skip to be very explicit -about not collecting tests and not executing the rest of the module. diff --git a/doc/en/announce/index.rst b/doc/en/announce/index.rst index 4df3228822c..fa57ca5c8af 100644 --- a/doc/en/announce/index.rst +++ b/doc/en/announce/index.rst @@ -6,6 +6,7 @@ Release announcements :maxdepth: 2 + release-7.2.2 release-7.2.1 release-7.2.0 release-7.1.3 diff --git a/doc/en/announce/release-7.2.2.rst b/doc/en/announce/release-7.2.2.rst new file mode 100644 index 00000000000..b34a6ff5c1e --- /dev/null +++ b/doc/en/announce/release-7.2.2.rst @@ -0,0 +1,25 @@ +pytest-7.2.2 +======================================= + +pytest 7.2.2 has just been released to PyPI. + +This is a bug-fix release, being a drop-in replacement. To upgrade:: + + pip install --upgrade pytest + +The full changelog is available at https://docs.pytest.org/en/stable/changelog.html. + +Thanks to all of the contributors to this release: + +* Bruno Oliveira +* Garvit Shubham +* Mahesh Vashishtha +* Ramsey +* Ronny Pfannschmidt +* Teejay +* q0w +* vin01 + + +Happy testing, +The pytest Development Team diff --git a/doc/en/builtin.rst b/doc/en/builtin.rst index 4b6d8d84457..453be4ad320 100644 --- a/doc/en/builtin.rst +++ b/doc/en/builtin.rst @@ -22,7 +22,7 @@ For information about fixtures, see :ref:`fixtures`. To see a complete list of a cachedir: .pytest_cache rootdir: /home/sweet/project collected 0 items - cache -- .../_pytest/cacheprovider.py:510 + cache -- .../_pytest/cacheprovider.py:509 Return a cache object that can persist state between testing sessions. cache.get(key, default) diff --git a/doc/en/changelog.rst b/doc/en/changelog.rst index 988e083d0e5..020e6289e48 100644 --- a/doc/en/changelog.rst +++ b/doc/en/changelog.rst @@ -28,6 +28,42 @@ with advance notice in the **Deprecations** section of releases. .. towncrier release notes start +pytest 7.2.2 (2023-03-03) +========================= + +Bug Fixes +--------- + +- `#10533 `_: Fixed :func:`pytest.approx` handling of dictionaries containing one or more values of `0.0`. + + +- `#10592 `_: Fixed crash if `--cache-show` and `--help` are passed at the same time. + + +- `#10597 `_: Fixed bug where a fixture method named ``teardown`` would be called as part of ``nose`` teardown stage. + + +- `#10626 `_: Fixed crash if ``--fixtures`` and ``--help`` are passed at the same time. + + +- `#10660 `_: Fixed :py:func:`pytest.raises` to return a 'ContextManager' so that type-checkers could narrow + :code:`pytest.raises(...) if ... else nullcontext()` down to 'ContextManager' rather than 'object'. + + + +Improved Documentation +---------------------- + +- `#10690 `_: Added `CI` and `BUILD_NUMBER` environment variables to the documentation. + + +- `#10721 `_: Fixed entry-points declaration in the documentation example using Hatch. + + +- `#10753 `_: Changed wording of the module level skip to be very explicit + about not collecting tests and not executing the rest of the module. + + pytest 7.2.1 (2023-01-13) ========================= diff --git a/doc/en/getting-started.rst b/doc/en/getting-started.rst index e109839bf3e..b36c2e3ccae 100644 --- a/doc/en/getting-started.rst +++ b/doc/en/getting-started.rst @@ -22,7 +22,7 @@ Install ``pytest`` .. code-block:: bash $ pytest --version - pytest 7.2.1 + pytest 7.2.2 .. _`simpletest`: