diff --git a/.coveragerc b/.coveragerc new file mode 100644 index 00000000000..27fc47686bd --- /dev/null +++ b/.coveragerc @@ -0,0 +1,34 @@ +[run] +include = + src/* + testing/* + */lib/python*/site-packages/_pytest/* + */lib/python*/site-packages/pytest.py + */pypy*/site-packages/_pytest/* + */pypy*/site-packages/pytest.py + *\Lib\site-packages\_pytest\* + *\Lib\site-packages\pytest.py +parallel = 1 +branch = 1 + +[paths] +source = src/ + */lib/python*/site-packages/ + */pypy*/site-packages/ + *\Lib\site-packages\ + +[report] +skip_covered = True +show_missing = True +exclude_lines = + \#\s*pragma: no cover + ^\s*raise NotImplementedError\b + ^\s*return NotImplemented\b + ^\s*assert False(,|$) + ^\s*case unreachable: + ^\s*assert_never\( + + ^\s*if TYPE_CHECKING: + ^\s*@overload( |$) + + ^\s*@pytest\.mark\.xfail diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index be0519a16e2..9e8abfa998b 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -31,7 +31,7 @@ jobs: persist-credentials: false - name: Build and Check Package - uses: hynek/build-and-inspect-python-package@efb823f52190ad02594531168b7a2d5790e66516 + uses: hynek/build-and-inspect-python-package@v2.12.0 with: attest-build-provenance-github: 'true' @@ -81,6 +81,10 @@ jobs: permissions: id-token: write steps: + - uses: actions/checkout@v4 + with: + persist-credentials: true + - name: Download Package uses: actions/download-artifact@37930b1c2abaa49bbe596cd826c3c89aef350131 # v7.0.0 with: @@ -88,12 +92,25 @@ jobs: path: dist - name: Publish package to PyPI - uses: pypa/gh-action-pypi-publish@ed0c53931b1dc9bd32cbe73a98c7f6766f8a527e + uses: pypa/gh-action-pypi-publish@v1.12.3 with: attestations: true - push-tag: - needs: [publish-to-pypi] + - name: Push tag + env: + VERSION: ${{ github.event.inputs.version }} + run: | + git config user.name "pytest bot" + git config user.email "pytestbot@gmail.com" + git tag --annotate --message=v"$VERSION" "$VERSION" ${{ github.sha }} + git push origin "$VERSION" + + release-notes: + + # todo: generate the content in the build job + # the goal being of using a github action script to push the release data + # after success instead of creating a complete python/tox env + needs: [deploy] runs-on: ubuntu-latest timeout-minutes: 10 permissions: @@ -129,8 +146,19 @@ jobs: - name: Download release notes uses: actions/download-artifact@37930b1c2abaa49bbe596cd826c3c89aef350131 # v7.0.0 with: - name: release-notes - path: . + python-version: "3.11" + + - name: Install tox + run: | + python -m pip install --upgrade pip + pip install --upgrade tox + + - name: Generate release notes + env: + VERSION: ${{ github.event.inputs.version }} + run: | + sudo apt-get install pandoc + tox -e generate-gh-release-notes -- "$VERSION" scripts/latest-release-notes.md - name: Publish GitHub Release env: diff --git a/.github/workflows/prepare-release-pr.yml b/.github/workflows/prepare-release-pr.yml index 1c0e869a512..15717eed7fd 100644 --- a/.github/workflows/prepare-release-pr.yml +++ b/.github/workflows/prepare-release-pr.yml @@ -30,8 +30,7 @@ jobs: - uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1 with: fetch-depth: 0 - # persist-credentials is needed in order for us to push the release branch. - persist-credentials: true + persist-credentials: false - name: Set up Python uses: actions/setup-python@83679a892e2d95755f2dac6acb0bfd1e9ac5d548 # v6.1.0 @@ -48,15 +47,13 @@ jobs: env: BRANCH: ${{ github.event.inputs.branch }} PRERELEASE: ${{ github.event.inputs.prerelease }} - GH_TOKEN: ${{ github.token }} run: | - tox -e prepare-release-pr -- "$BRANCH" --prerelease="$PRERELEASE" + tox -e prepare-release-pr -- "$BRANCH" ${{ github.token }} --prerelease="$PRERELEASE" - name: Prepare release PR (major release) if: github.event.inputs.major == 'yes' env: BRANCH: ${{ github.event.inputs.branch }} PRERELEASE: ${{ github.event.inputs.prerelease }} - GH_TOKEN: ${{ github.token }} run: | - tox -e prepare-release-pr -- "$BRANCH" --major --prerelease="$PRERELEASE" + tox -e prepare-release-pr -- "$BRANCH" ${{ github.token }} --major --prerelease="$PRERELEASE" diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index d9dca4964ae..78d31b52712 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -15,12 +15,12 @@ on: - main - "[0-9]+.[0-9]+.x" types: - - opened # default - - synchronize # default - - reopened # default - - ready_for_review # used in PRs created from the release workflow + - opened # default + - synchronize # default + - reopened # default + - ready_for_review # used in PRs created from the release workflow - workflow_dispatch: # allows manual triggering of the workflow + workflow_dispatch: # allows manual triggering of the workflow env: PYTEST_ADDOPTS: "--color=yes" @@ -37,12 +37,12 @@ jobs: package: runs-on: ubuntu-latest steps: - - uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1 - with: - fetch-depth: 0 - persist-credentials: false - - name: Build and Check Package - uses: hynek/build-and-inspect-python-package@efb823f52190ad02594531168b7a2d5790e66516 + - uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1 + with: + fetch-depth: 0 + persist-credentials: false + - name: Build and Check Package + uses: hynek/build-and-inspect-python-package@v2.14.0 build: needs: [package] @@ -55,38 +55,36 @@ jobs: strategy: fail-fast: false matrix: - name: [ - "windows-py310-unittest-asynctest", - "windows-py310-unittest-twisted24", - "windows-py310-unittest-twisted25", - "windows-py310-pluggy", - "windows-py310-xdist", - "windows-py311", - "windows-py312", - "windows-py313", - "windows-py314", - - "ubuntu-py310-unittest-asynctest", - "ubuntu-py310-unittest-twisted24", - "ubuntu-py310-unittest-twisted25", - "ubuntu-py310-lsof-numpy-pexpect", - "ubuntu-py310-pluggy", - "ubuntu-py310-freeze", - "ubuntu-py310-xdist", - "ubuntu-py311", - "ubuntu-py312", - "ubuntu-py313-pexpect", - "ubuntu-py314", - "ubuntu-pypy3-xdist", - - "macos-py310", - "macos-py312", - "macos-py313", - "macos-py314", - - "doctesting", - "plugins", - ] + name: + [ + "windows-py38", + "windows-py39-pluggy", + "windows-py39", + "windows-py310", + "windows-py311", + "windows-py312", + "windows-py313", + "windows-py314", + + "ubuntu-py38", + "ubuntu-py39-pluggy", + "ubuntu-py38-freeze", + "ubuntu-py39", + "ubuntu-py310", + "ubuntu-py311", + "ubuntu-py312", + "ubuntu-py313-pexpect", + "ubuntu-py314", + "ubuntu-pypy3-xdist", + + "macos-py310", + "macos-py312", + "macos-py313", + "macos-py314", + + "doctesting", + "plugins", + ] include: # Use separate jobs for different unittest flavors (twisted, asynctest) to ensure proper coverage. @@ -95,15 +93,12 @@ jobs: os: windows-latest tox_env: "py310-asynctest" use_coverage: true - - - name: "windows-py310-unittest-twisted24" - python: "3.10" + - name: "windows-py39-pluggy" + python: "3.9" os: windows-latest - tox_env: "py310-twisted24" - use_coverage: true - - - name: "windows-py310-unittest-twisted25" - python: "3.10" + tox_env: "py39-pluggymain-pylib-xdist" + - name: "windows-py39" + python: "3.9" os: windows-latest tox_env: "py310-twisted25" use_coverage: true @@ -140,16 +135,12 @@ jobs: os: windows-latest tox_env: "py314" use_coverage: true - - # Use separate jobs for different unittest flavors (twisted, asynctest) to ensure proper coverage. - - name: "ubuntu-py310-unittest-asynctest" - python: "3.10" + - name: "ubuntu-py39-pluggy" + python: "3.9" os: ubuntu-latest - tox_env: "py310-asynctest" - use_coverage: true - - - name: "ubuntu-py310-unittest-twisted24" - python: "3.10" + tox_env: "py39-pluggymain-pylib-xdist" + - name: "ubuntu-py38-freeze" + python: "3.8" os: ubuntu-latest tox_env: "py310-twisted24" use_coverage: true @@ -213,7 +204,6 @@ jobs: os: ubuntu-latest tox_env: "pypy3-xdist" - - name: "macos-py310" python: "3.10" os: macos-latest @@ -241,67 +231,84 @@ jobs: os: ubuntu-latest tox_env: "plugins" - - name: "doctesting" python: "3.10" os: ubuntu-latest tox_env: "doctesting" use_coverage: true - continue-on-error: ${{ matrix.xfail && true || false }} + continue-on-error: >- + ${{ + contains( + fromJSON( + '[ + "windows-py39-pluggy", + "windows-py313", + "ubuntu-py39-pluggy", + "ubuntu-py38-freeze", + "ubuntu-py313", + "macos-py38", + "macos-py313" + ]' + ), + matrix.name + ) + && true + || false + }} steps: - - uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1 - with: - fetch-depth: 0 - persist-credentials: false - - - name: Download Package - uses: actions/download-artifact@37930b1c2abaa49bbe596cd826c3c89aef350131 # v7.0.0 - with: - name: Packages - path: dist - - - name: Set up Python ${{ matrix.python }} - uses: actions/setup-python@83679a892e2d95755f2dac6acb0bfd1e9ac5d548 # v6.1.0 - with: - python-version: ${{ matrix.python }} - check-latest: true - allow-prereleases: true - - - name: Install tox - run: | - python -m pip install --upgrade pip - pip install tox - - - name: Test without coverage - if: "! matrix.use_coverage" - shell: bash - run: tox run -e ${{ matrix.tox_env }} --installpkg `find dist/*.tar.gz` - - - name: Test with coverage - if: "matrix.use_coverage" - shell: bash - run: tox run -e ${{ matrix.tox_env }}-coverage --installpkg `find dist/*.tar.gz` - - - name: Upload coverage to Codecov - if: "matrix.use_coverage" - uses: codecov/codecov-action@671740ac38dd9b0130fbe1cec585b89eea48d3de - with: - fail_ci_if_error: false - files: ./coverage.xml - verbose: true - - check: # This job does nothing and is only used for the branch protection + - uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1 + with: + fetch-depth: 0 + persist-credentials: false + + - name: Download Package + uses: actions/download-artifact@37930b1c2abaa49bbe596cd826c3c89aef350131 # v7.0.0 + with: + name: Packages + path: dist + + - name: Set up Python ${{ matrix.python }} + uses: actions/setup-python@83679a892e2d95755f2dac6acb0bfd1e9ac5d548 # v6.1.0 + with: + python-version: ${{ matrix.python }} + check-latest: true + allow-prereleases: true + + - name: Install tox + run: | + python -m pip install --upgrade pip + pip install tox + + - name: Test without coverage + if: "! matrix.use_coverage" + shell: bash + run: tox run -e ${{ matrix.tox_env }} --installpkg `find dist/*.tar.gz` + + - name: Test with coverage + if: "matrix.use_coverage" + shell: bash + run: tox run -e ${{ matrix.tox_env }}-coverage --installpkg `find dist/*.tar.gz` + + - name: Upload coverage to Codecov + if: "matrix.use_coverage" + uses: codecov/codecov-action@671740ac38dd9b0130fbe1cec585b89eea48d3de + with: + fail_ci_if_error: false + files: ./coverage.xml + verbose: true + + check: # This job does nothing and is only used for the branch protection if: always() needs: - - build + - build runs-on: ubuntu-latest steps: - - name: Decide whether the needed jobs succeeded or failed - uses: re-actors/alls-green@2765efec08f0fd63e83ad900f5fd75646be69ff6 - with: - jobs: ${{ toJSON(needs) }} + - name: Decide whether the needed jobs succeeded or failed + uses: re-actors/alls-green@2765efec08f0fd63e83ad900f5fd75646be69ff6 + with: + jobs: ${{ toJSON(needs) }} diff --git a/.github/workflows/update-plugin-list.yml b/.github/workflows/update-plugin-list.yml index bc1e1dd5923..b3a014bbed9 100644 --- a/.github/workflows/update-plugin-list.yml +++ b/.github/workflows/update-plugin-list.yml @@ -28,7 +28,8 @@ jobs: - name: Setup Python uses: actions/setup-python@83679a892e2d95755f2dac6acb0bfd1e9ac5d548 # v6.1.0 with: - python-version: "3.13" + python-version: "3.11" + cache: pip - name: requests-cache uses: actions/cache@9255dc7a253b0ccc959486e2bca901246202afeb # v5.0.1 @@ -40,7 +41,7 @@ jobs: - name: Install tox run: | python -m pip install --upgrade pip - pip install tox + pip install packaging requests tabulate[widechars] tqdm requests-cache platformdirs - name: Update Plugin List run: tox -e update-plugin-list diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index b6ac238aca8..9c7a8019086 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,7 +1,7 @@ minimum_pre_commit_version: "4.4.0" repos: - repo: https://github.com/astral-sh/ruff-pre-commit - rev: "v0.14.11" + rev: "v0.6.2" hooks: - id: ruff-check args: ["--fix"] @@ -13,17 +13,16 @@ repos: - id: end-of-file-fixer - id: check-yaml - repo: https://github.com/woodruffw/zizmor-pre-commit - rev: v1.20.0 + rev: v0.9.2 hooks: - id: zizmor - args: ["--fix", "--no-progress"] - repo: https://github.com/adamchainz/blacken-docs rev: 1.20.0 hooks: - id: blacken-docs additional_dependencies: [black==24.1.1] - repo: https://github.com/codespell-project/codespell - rev: v2.4.1 + rev: v2.3.0 hooks: - id: codespell args: ["--toml=pyproject.toml"] @@ -34,7 +33,7 @@ repos: hooks: - id: python-use-type-annotations - repo: https://github.com/pre-commit/mirrors-mypy - rev: v1.19.1 + rev: v1.11.2 hooks: - id: mypy files: ^(src/|testing/|scripts/) @@ -68,13 +67,13 @@ repos: # Manual because passing pyright is a work in progress. stages: [manual] - repo: https://github.com/tox-dev/pyproject-fmt - rev: "v2.11.1" + rev: "2.2.1" hooks: - id: pyproject-fmt # https://pyproject-fmt.readthedocs.io/en/latest/#calculating-max-supported-python-version additional_dependencies: ["tox>=4.9"] - repo: https://github.com/asottile/pyupgrade - rev: v3.21.2 + rev: v3.17.0 hooks: - id: pyupgrade args: diff --git a/AUTHORS b/AUTHORS index 09c5872fe07..85f0e8d3c40 100644 --- a/AUTHORS +++ b/AUTHORS @@ -390,7 +390,6 @@ Reza Mousavi Raquel Alegre Ravi Chandra Reagan Lee -Reilly Brogan Rob Arrow Robert Holt Roberto Aldera diff --git a/changelog/1367.feature.rst b/changelog/1367.feature.rst new file mode 100644 index 00000000000..b88480338b5 --- /dev/null +++ b/changelog/1367.feature.rst @@ -0,0 +1,28 @@ +**Support for subtests** has been added. + +:ref:`subtests ` are an alternative to parametrization, useful in situations where the parametrization values are not all known at collection time. + +**Example** + +.. code-block:: python + + def contains_docstring(p: Path) -> bool: + """Return True if the given Python file contains a top-level docstring.""" + ... + + + def test_py_files_contain_docstring(subtests: pytest.Subtests) -> None: + for path in Path.cwd().glob("*.py"): + with subtests.test(path=str(path)): + assert contains_docstring(path) + + +Each assert failure or error is caught by the context manager and reported individually, giving a clear picture of all files that are missing a docstring. + +In addition, :meth:`unittest.TestCase.subTest` is now also supported. + +This feature was originally implemented as a separate plugin in `pytest-subtests `__, but since then has been merged into the core. + +.. note:: + + This feature is experimental and will likely evolve in future releases. By that we mean that we might change how subtests are reported on failure, but the functionality and how to use it are stable. diff --git a/doc/en/announce/index.rst b/doc/en/announce/index.rst index b92b8d4a56b..51edc964a0c 100644 --- a/doc/en/announce/index.rst +++ b/doc/en/announce/index.rst @@ -6,12 +6,6 @@ Release announcements :maxdepth: 2 - release-9.0.2 - release-9.0.1 - release-9.0.0 - release-8.4.2 - release-8.4.1 - release-8.4.0 release-8.3.5 release-8.3.4 release-8.3.3 diff --git a/doc/en/builtin.rst b/doc/en/builtin.rst index 6a96bb0a304..3ccbad3cae4 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:566 + cache -- .../_pytest/cacheprovider.py:556 Return a cache object that can persist state between testing sessions. cache.get(key, default) @@ -33,48 +33,7 @@ For information about fixtures, see :ref:`fixtures`. To see a complete list of a Values can be any object handled by the json stdlib module. - capsys -- .../_pytest/capture.py:1000 - Enable text capturing of writes to ``sys.stdout`` and ``sys.stderr``. - - The captured output is made available via ``capsys.readouterr()`` method - calls, which return a ``(out, err)`` namedtuple. - ``out`` and ``err`` will be ``text`` objects. - - Returns an instance of :class:`CaptureFixture[str] `. - - Example: - - .. code-block:: python - - def test_output(capsys): - print("hello") - captured = capsys.readouterr() - assert captured.out == "hello\n" - - capteesys -- .../_pytest/capture.py:1028 - Enable simultaneous text capturing and pass-through of writes - to ``sys.stdout`` and ``sys.stderr`` as defined by ``--capture=``. - - - The captured output is made available via ``capteesys.readouterr()`` method - calls, which return a ``(out, err)`` namedtuple. - ``out`` and ``err`` will be ``text`` objects. - - The output is also passed-through, allowing it to be "live-printed", - reported, or both as defined by ``--capture=``. - - Returns an instance of :class:`CaptureFixture[str] `. - - Example: - - .. code-block:: python - - def test_output(capteesys): - print("hello") - captured = capteesys.readouterr() - assert captured.out == "hello\n" - - capsysbinary -- .../_pytest/capture.py:1063 + capsysbinary -- .../_pytest/capture.py:1024 Enable bytes capturing of writes to ``sys.stdout`` and ``sys.stderr``. The captured output is made available via ``capsysbinary.readouterr()`` @@ -92,7 +51,7 @@ For information about fixtures, see :ref:`fixtures`. To see a complete list of a captured = capsysbinary.readouterr() assert captured.out == b"hello\n" - capfd -- .../_pytest/capture.py:1091 + capfd -- .../_pytest/capture.py:1052 Enable text capturing of writes to file descriptors ``1`` and ``2``. The captured output is made available via ``capfd.readouterr()`` method @@ -110,7 +69,7 @@ For information about fixtures, see :ref:`fixtures`. To see a complete list of a captured = capfd.readouterr() assert captured.out == "hello\n" - capfdbinary -- .../_pytest/capture.py:1119 + capfdbinary -- .../_pytest/capture.py:1080 Enable bytes capturing of writes to file descriptors ``1`` and ``2``. The captured output is made available via ``capfd.readouterr()`` method @@ -128,7 +87,25 @@ For information about fixtures, see :ref:`fixtures`. To see a complete list of a captured = capfdbinary.readouterr() assert captured.out == b"hello\n" - doctest_namespace [session scope] -- .../_pytest/doctest.py:722 + capsys -- .../_pytest/capture.py:996 + Enable text capturing of writes to ``sys.stdout`` and ``sys.stderr``. + + The captured output is made available via ``capsys.readouterr()`` method + calls, which return a ``(out, err)`` namedtuple. + ``out`` and ``err`` will be ``text`` objects. + + Returns an instance of :class:`CaptureFixture[str] `. + + Example: + + .. code-block:: python + + def test_output(capsys): + print("hello") + captured = capsys.readouterr() + assert captured.out == "hello\n" + + doctest_namespace [session scope] -- .../_pytest/doctest.py:741 Fixture that returns a :py:class:`dict` that will be injected into the namespace of doctests. @@ -142,7 +119,7 @@ For information about fixtures, see :ref:`fixtures`. To see a complete list of a For more details: :ref:`doctest_namespace`. - pytestconfig [session scope] -- .../_pytest/fixtures.py:1431 + pytestconfig [session scope] -- .../_pytest/fixtures.py:1345 Session-scoped fixture that returns the session's :class:`pytest.Config` object. @@ -152,7 +129,7 @@ For information about fixtures, see :ref:`fixtures`. To see a complete list of a if pytestconfig.get_verbosity() > 0: ... - record_property -- .../_pytest/junitxml.py:277 + record_property -- .../_pytest/junitxml.py:280 Add extra properties to the calling test. User properties become part of the test report and are available to the @@ -166,13 +143,13 @@ For information about fixtures, see :ref:`fixtures`. To see a complete list of a def test_function(record_property): record_property("example_key", 1) - record_xml_attribute -- .../_pytest/junitxml.py:300 + record_xml_attribute -- .../_pytest/junitxml.py:303 Add extra xml attributes to the tag for the calling test. The fixture is callable with ``name, value``. The value is automatically XML-encoded. - record_testsuite_property [session scope] -- .../_pytest/junitxml.py:338 + record_testsuite_property [session scope] -- .../_pytest/junitxml.py:341 Record a new ```` tag as child of the root ````. This is suitable to writing global information regarding the entire test @@ -214,7 +191,7 @@ For information about fixtures, see :ref:`fixtures`. To see a complete list of a .. _legacy_path: https://py.readthedocs.io/en/latest/path.html - caplog -- .../_pytest/logging.py:596 + caplog -- .../_pytest/logging.py:598 Access and control log capturing. Captured logs are available through the following properties/methods:: @@ -225,7 +202,7 @@ For information about fixtures, see :ref:`fixtures`. To see a complete list of a * caplog.record_tuples -> list of (logger_name, level, message) tuples * caplog.clear() -> clear captured records and formatted log output string - monkeypatch -- .../_pytest/monkeypatch.py:33 + monkeypatch -- .../_pytest/monkeypatch.py:31 A convenient fixture for monkey-patching. The fixture provides these methods to modify objects, dictionaries, or @@ -249,18 +226,15 @@ For information about fixtures, see :ref:`fixtures`. To see a complete list of a To undo modifications done by the fixture in a contained scope, use :meth:`context() `. - recwarn -- .../_pytest/recwarn.py:34 + recwarn -- .../_pytest/recwarn.py:35 Return a :class:`WarningsRecorder` instance that records all warnings emitted by test functions. See :ref:`warnings` for information on warning categories. - subtests -- .../_pytest/subtests.py:129 - Provides subtests functionality. - - tmp_path_factory [session scope] -- .../_pytest/tmpdir.py:243 + tmp_path_factory [session scope] -- .../_pytest/tmpdir.py:241 Return a :class:`pytest.TempPathFactory` instance for the test session. - tmp_path -- .../_pytest/tmpdir.py:258 + tmp_path -- .../_pytest/tmpdir.py:256 Return a temporary directory (as :class:`pathlib.Path` object) which is unique to each test function invocation. The temporary directory is created as a subdirectory diff --git a/doc/en/changelog.rst b/doc/en/changelog.rst index b4e5cee694e..3074124465a 100644 --- a/doc/en/changelog.rst +++ b/doc/en/changelog.rst @@ -31,880 +31,6 @@ with advance notice in the **Deprecations** section of releases. .. towncrier release notes start -pytest 9.0.2 (2025-12-06) -========================= - -Bug fixes ---------- - -- `#13896 `_: The terminal progress feature added in pytest 9.0.0 has been disabled by default, except on Windows, due to compatibility issues with some terminal emulators. - - You may enable it again by passing ``-p terminalprogress``. We may enable it by default again once compatibility improves in the future. - - Additionally, when the environment variable ``TERM`` is ``dumb``, the escape codes are no longer emitted, even if the plugin is enabled. - - -- `#13904 `_: Fixed the TOML type of the :confval:`tmp_path_retention_count` settings in the API reference from number to string. - - -- `#13946 `_: The private ``config.inicfg`` attribute was changed in a breaking manner in pytest 9.0.0. - Due to its usage in the ecosystem, it is now restored to working order using a compatibility shim. - It will be deprecated in pytest 9.1 and removed in pytest 10. - - -- `#13965 `_: Fixed quadratic-time behavior when handling ``unittest`` subtests in Python 3.10. - - - -Improved documentation ----------------------- - -- `#4492 `_: The API Reference now contains cross-reference-able documentation of :ref:`pytest's command-line flags `. - - -pytest 9.0.1 (2025-11-12) -========================= - -Bug fixes ---------- - -- `#13895 `_: Restore support for skipping tests via ``raise unittest.SkipTest``. - - -- `#13896 `_: The terminal progress plugin added in pytest 9.0 is now automatically disabled when iTerm2 is detected, it generated desktop notifications instead of the desired functionality. - - -- `#13904 `_: Fixed the TOML type of the verbosity settings in the API reference from number to string. - - -- `#13910 `_: Fixed `UserWarning: Do not expect file_or_dir` on some earlier Python 3.12 and 3.13 point versions. - - - -Packaging updates and notes for downstreams -------------------------------------------- - -- `#13933 `_: The tox configuration has been adjusted to make sure the desired - version string can be passed into its :ref:`package_env` through - the ``SETUPTOOLS_SCM_PRETEND_VERSION_FOR_PYTEST`` environment - variable as a part of the release process -- by :user:`webknjaz`. - - - -Contributor-facing changes --------------------------- - -- `#13891 `_, `#13942 `_: The CI/CD part of the release automation is now capable of - creating GitHub Releases without having a Git checkout on - disk -- by :user:`bluetech` and :user:`webknjaz`. - - -- `#13933 `_: The tox configuration has been adjusted to make sure the desired - version string can be passed into its :ref:`package_env` through - the ``SETUPTOOLS_SCM_PRETEND_VERSION_FOR_PYTEST`` environment - variable as a part of the release process -- by :user:`webknjaz`. - - -pytest 9.0.0 (2025-11-05) -========================= - -New features ------------- - - -- `#1367 `_: **Support for subtests** has been added. - - :ref:`subtests ` are an alternative to parametrization, useful in situations where the parametrization values are not all known at collection time. - - Example: - - .. code-block:: python - - def contains_docstring(p: Path) -> bool: - """Return True if the given Python file contains a top-level docstring.""" - ... - - - def test_py_files_contain_docstring(subtests: pytest.Subtests) -> None: - for path in Path.cwd().glob("*.py"): - with subtests.test(path=str(path)): - assert contains_docstring(path) - - - Each assert failure or error is caught by the context manager and reported individually, giving a clear picture of all files that are missing a docstring. - - In addition, :meth:`unittest.TestCase.subTest` is now also supported. - - This feature was originally implemented as a separate plugin in `pytest-subtests `__, but since then has been merged into the core. - - .. note:: - - This feature is experimental and will likely evolve in future releases. By that we mean that we might change how subtests are reported on failure, but the functionality and how to use it are stable. - - -- `#13743 `_: Added support for **native TOML configuration files**. - - While pytest, since version 6, supports configuration in ``pyproject.toml`` files under ``[tool.pytest.ini_options]``, - it does so in an "INI compatibility mode", where all configuration values are treated as strings or list of strings. - Now, pytest supports the native TOML data model. - - In ``pyproject.toml``, the native TOML configuration is under the ``[tool.pytest]`` table. - - .. code-block:: toml - - # pyproject.toml - [tool.pytest] - minversion = "9.0" - addopts = ["-ra", "-q"] - testpaths = [ - "tests", - "integration", - ] - - The ``[tool.pytest.ini_options]`` table remains supported, but both tables cannot be used at the same time. - - If you prefer to use a separate configuration file, or don't use ``pyproject.toml``, you can use ``pytest.toml`` or ``.pytest.toml``: - - .. code-block:: toml - - # pytest.toml or .pytest.toml - [pytest] - minversion = "9.0" - addopts = ["-ra", "-q"] - testpaths = [ - "tests", - "integration", - ] - - The documentation now (sometimes) shows configuration snippets in both TOML and INI formats, in a tabbed interface. - - See :ref:`config file formats` for full details. - - -- `#13823 `_: Added a **"strict mode"** enabled by the :confval:`strict` configuration option. - - When set to ``true``, the :confval:`strict` option currently enables - - * :confval:`strict_config` - * :confval:`strict_markers` - * :confval:`strict_parametrization_ids` - * :confval:`strict_xfail` - - The individual strictness options can be explicitly set to override the global :confval:`strict` setting. - - The previously-deprecated ``--strict`` command-line flag now enables strict mode. - - If pytest adds new strictness options in the future, they will also be enabled in strict mode. - Therefore, you should only enable strict mode if you use a pinned/locked version of pytest, - or if you want to proactively adopt new strictness options as they are added. - - See :ref:`strict mode` for more details. - - -- `#13737 `_: Added the :confval:`strict_parametrization_ids` configuration option. - - When set, pytest emits an error if it detects non-unique parameter set IDs, - rather than automatically making the IDs unique by adding `0`, `1`, ... to them. - This can be particularly useful for catching unintended duplicates. - - -- `#13072 `_: Added support for displaying test session **progress in the terminal tab** using the `OSC 9;4; `_ ANSI sequence. - - **Note**: *This feature has been disabled by default in version 9.0.2, except on Windows, due to compatibility issues with some terminal emulators. - You may enable it again by passing* ``-p terminalprogress``. *We may enable it by default again once compatibility improves in the future.* - - When pytest runs in a supported terminal emulator like ConEmu, Gnome Terminal, Ptyxis, Windows Terminal, Kitty or Ghostty, - you'll see the progress in the terminal tab or window, - allowing you to monitor pytest's progress at a glance. - - This feature is automatically enabled when running in a TTY. It is implemented as an internal plugin. If needed, it can be disabled as follows: - - On a user level, using ``-p no:terminalprogress`` on the command line or via an environment variable ``PYTEST_ADDOPTS='-p no:terminalprogress'``. - - On a project configuration level, using ``addopts = "-p no:terminalprogress"``. - - -- `#478 `_: Support PEP420 (implicit namespace packages) as `--pyargs` target when :confval:`consider_namespace_packages` is `true` in the config. - - Previously, this option only impacted package imports, now it also impacts tests discovery. - - -- `#13678 `_: Added a new :confval:`faulthandler_exit_on_timeout` configuration option set to "false" by default to let `faulthandler` interrupt the `pytest` process after a timeout in case of deadlock. - - Previously, a `faulthandler` timeout would only dump the traceback of all threads to stderr, but would not interrupt the `pytest` process. - - -- by :user:`ogrisel`. - - -- `#13829 `_: Added support for configuration option aliases via the ``aliases`` parameter in :meth:`Parser.addini() `. - - Plugins can now register alternative names for configuration options, - allowing for more flexibility in configuration naming and supporting backward compatibility when renaming options. - The canonical name always takes precedence if both the canonical name and an alias are specified in the configuration file. - - - -Improvements in existing functionality --------------------------------------- - -- `#13330 `_: Having pytest configuration spread over more than one file (for example having both a ``pytest.ini`` file and ``pyproject.toml`` with a ``[tool.pytest.ini_options]`` table) will now print a warning to make it clearer to the user that only one of them is actually used. - - -- by :user:`sgaist` - - -- `#13574 `_: The single argument ``--version`` no longer loads the entire plugin infrastructure, making it faster and more reliable when displaying only the pytest version. - - Passing ``--version`` twice (e.g., ``pytest --version --version``) retains the original behavior, showing both the pytest version and plugin information. - - .. note:: - - Since ``--version`` is now processed early, it only takes effect when passed directly via the command line. It will not work if set through other mechanisms, such as :envvar:`PYTEST_ADDOPTS` or :confval:`addopts`. - - -- `#13823 `_: Added :confval:`strict_xfail` as an alias to the ``xfail_strict`` option, - :confval:`strict_config` as an alias to the ``--strict-config`` flag, - and :confval:`strict_markers` as an alias to the ``--strict-markers`` flag. - This makes all strictness options consistently have configuration options with the prefix ``strict_``. - -- `#13700 `_: `--junitxml` no longer prints the `generated xml file` summary at the end of the pytest session when `--quiet` is given. - - -- `#13732 `_: Previously, when filtering warnings, pytest would fail if the filter referenced a class that could not be imported. Now, this only outputs a message indicating the problem. - - -- `#13859 `_: Clarify the error message for `pytest.raises()` when a regex `match` fails. - - -- `#13861 `_: Better sentence structure in a test's expected error message. Previously, the error message would be "expected exception must be , but got ". Now, it is "Expected , but got ". - - -Removals and backward incompatible breaking changes ---------------------------------------------------- - -- `#12083 `_: Fixed a bug where an invocation such as `pytest a/ a/b` would cause only tests from `a/b` to run, and not other tests under `a/`. - - The fix entails a few breaking changes to how such overlapping arguments and duplicates are handled: - - 1. `pytest a/b a/` or `pytest a/ a/b` are equivalent to `pytest a`; if an argument overlaps another arguments, only the prefix remains. - - 2. `pytest x.py x.py` is equivalent to `pytest x.py`; previously such an invocation was taken as an explicit request to run the tests from the file twice. - - If you rely on these behaviors, consider using :ref:`--keep-duplicates `, which retains its existing behavior (including the bug). - - -- `#13719 `_: Support for Python 3.9 is dropped following its end of life. - - -- `#13766 `_: Previously, pytest would assume it was running in a CI/CD environment if either of the environment variables `$CI` or `$BUILD_NUMBER` was defined; - now, CI mode is only activated if at least one of those variables is defined and set to a *non-empty* value. - - -- The non-public ``config.args`` attribute used to be able to contain ``pathlib.Path`` instances; now it can only contain strings. - - -- `#13779 `_: **PytestRemovedIn9Warning deprecation warnings are now errors by default.** - - Following our plan to remove deprecated features with as little disruption as - possible, all warnings of type ``PytestRemovedIn9Warning`` now generate errors - instead of warning messages by default. - - **The affected features will be effectively removed in pytest 9.1**, so please consult the - :ref:`deprecations` section in the docs for directions on how to update existing code. - - In the pytest ``9.0.X`` series, it is possible to change the errors back into warnings as a - stopgap measure by adding this to your ``pytest.ini`` file: - - .. code-block:: ini - - [pytest] - filterwarnings = - ignore::pytest.PytestRemovedIn9Warning - - But this will stop working when pytest ``9.1`` is released. - - **If you have concerns** about the removal of a specific feature, please add a - comment to :issue:`13779`. - - - -Deprecations (removal in next major release) --------------------------------------------- - -- `#13807 `_: :meth:`monkeypatch.syspath_prepend() ` now issues a deprecation warning when the prepended path contains legacy namespace packages (those using ``pkg_resources.declare_namespace()``). - Users should migrate to native namespace packages (:pep:`420`). - See :ref:`monkeypatch-fixup-namespace-packages` for details. - - -Bug fixes ---------- - -- `#13445 `_: Made the type annotations of :func:`pytest.skip` and friends more spec-complaint to have them work across more type checkers. - - -- `#13537 `_: Fixed a bug in which :class:`ExceptionGroup` with only ``Skipped`` exceptions in teardown was not handled correctly and showed as error. - - -- `#13598 `_: Fixed possible collection confusion on Windows when short paths and symlinks are involved. - - -- `#13716 `_: Fixed a bug where a nonsensical invocation like ``pytest x.py[a]`` (a file cannot be parametrized) was silently treated as ``pytest x.py``. This is now a usage error. - - -- `#13722 `_: Fixed a misleading assertion failure message when using :func:`pytest.approx` on mappings with differing lengths. - - -- `#13773 `_: Fixed the static fixture closure calculation to properly consider transitive dependencies requested by overridden fixtures. - - -- `#13816 `_: Fixed :func:`pytest.approx` which now returns a clearer error message when comparing mappings with different keys. - - -- `#13849 `_: Hidden ``.pytest.ini`` files are now picked up as the config file even if empty. - This was an inconsistency with non-hidden ``pytest.ini``. - - -- `#13865 `_: Fixed `--show-capture` with `--tb=line`. - - -- `#13522 `_: Fixed :fixture:`pytester` in subprocess mode ignored all :attr:`pytester.plugins ` except the first. - - Fixed :fixture:`pytester` in subprocess mode silently ignored non-str :attr:`pytester.plugins `. - Now it errors instead. - If you are affected by this, specify the plugin by name, or switch the affected tests to use :func:`pytester.runpytest_inprocess ` explicitly instead. - - - -Packaging updates and notes for downstreams -------------------------------------------- - -- `#13791 `_: Minimum requirements on ``iniconfig`` and ``packaging`` were bumped to ``1.0.1`` and ``22.0.0``, respectively. - - - -Contributor-facing changes --------------------------- - -- `#12244 `_: Fixed self-test failures when `TERM=dumb`. - - -- `#12474 `_: Added scheduled GitHub Action Workflow to run Sphinx linkchecks in repo documentation. - - -- `#13621 `_: pytest's own testsuite now handles the ``lsof`` command hanging (e.g. due to unreachable network filesystems), with the affected selftests being skipped after 10 seconds. - - -- `#13638 `_: Fixed deprecated :command:`gh pr new` command in :file:`scripts/prepare-release-pr.py`. - The script now uses :command:`gh pr create` which is compatible with GitHub CLI v2.0+. - - -- `#13695 `_: Flush `stdout` and `stderr` in `Pytester.run` to avoid truncated outputs in `test_faulthandler.py::test_timeout` on CI -- by :user:`ogrisel`. - - -- `#13771 `_: Skip `test_do_not_collect_symlink_siblings` on Windows environments without symlink support to avoid false negatives. - - -- `#13841 `_: ``tox>=4`` is now required when contributing to pytest. - -- `#13625 `_: Added missing docstrings to ``pytest_addoption()``, ``pytest_configure()``, and ``cacheshow()`` functions in ``cacheprovider.py``. - - - -Miscellaneous internal changes ------------------------------- - -- `#13830 `_: Configuration overrides (``-o``/``--override-ini``) are now processed during startup rather than during :func:`config.getini() `. - - -pytest 8.4.2 (2025-09-03) -========================= - -Bug fixes ---------- - -- `#13478 `_: Fixed a crash when using :confval:`console_output_style` with ``times`` and a module is skipped. - - -- `#13530 `_: Fixed a crash when using :func:`pytest.approx` and :class:`decimal.Decimal` instances with the :class:`decimal.FloatOperation` trap set. - - -- `#13549 `_: No longer evaluate type annotations in Python ``3.14`` when inspecting function signatures. - - This prevents crashes during module collection when modules do not explicitly use ``from __future__ import annotations`` and import types for annotations within a ``if TYPE_CHECKING:`` block. - - -- `#13559 `_: Added missing `int` and `float` variants to the `Literal` type annotation of the `type` parameter in :meth:`pytest.Parser.addini`. - - -- `#13563 `_: :func:`pytest.approx` now only imports ``numpy`` if NumPy is already in ``sys.modules``. This fixes unconditional import behavior introduced in `8.4.0`. - - - -Improved documentation ----------------------- - -- `#13577 `_: Clarify that ``pytest_generate_tests`` is discovered in test modules/classes; other hooks must be in ``conftest.py`` or plugins. - - - -Contributor-facing changes --------------------------- - -- `#13480 `_: Self-testing: fixed a few test failures when run with ``-Wdefault`` or a similar override. - - -- `#13547 `_: Self-testing: corrected expected message for ``test_doctest_unexpected_exception`` in Python ``3.14``. - - -- `#13684 `_: Make pytest's own testsuite insensitive to the presence of the ``CI`` environment variable -- by :user:`ogrisel`. - - -pytest 8.4.1 (2025-06-17) -========================= - -Bug fixes ---------- - -- `#13461 `_: Corrected ``_pytest.terminal.TerminalReporter.isatty`` to support - being called as a method. Before it was just a boolean which could - break correct code when using ``-o log_cli=true``). - - -- `#13477 `_: Reintroduced :class:`pytest.PytestReturnNotNoneWarning` which was removed by accident in pytest `8.4`. - - This warning is raised when a test functions returns a value other than ``None``, which is often a mistake made by beginners. - - See :ref:`return-not-none` for more information. - - -- `#13497 `_: Fixed compatibility with ``Twisted 25+``. - - - -Improved documentation ----------------------- - -- `#13492 `_: Fixed outdated warning about ``faulthandler`` not working on Windows. - - -pytest 8.4.0 (2025-06-02) -========================= - -Removals and backward incompatible breaking changes ---------------------------------------------------- - -- `#11372 `_: Async tests will now fail, instead of warning+skipping, if you don't have any suitable plugin installed. - - -- `#12346 `_: Tests will now fail, instead of raising a warning, if they return any value other than None. - - -- `#12874 `_: We dropped support for Python 3.8 following its end of life (2024-10-07). - - -- `#12960 `_: Test functions containing a yield now cause an explicit error. They have not been run since pytest 4.0, and were previously marked as an expected failure and deprecation warning. - - See :ref:`the docs ` for more information. - - - -Deprecations (removal in next major release) --------------------------------------------- - -- `#10839 `_: Requesting an asynchronous fixture without a `pytest_fixture_setup` hook that resolves it will now give a DeprecationWarning. This most commonly happens if a sync test requests an async fixture. This should have no effect on a majority of users with async tests or fixtures using async pytest plugins, but may affect non-standard hook setups or ``autouse=True``. For guidance on how to work around this warning see :ref:`sync-test-async-fixture`. - - - -New features ------------- - -- `#11538 `_: Added :class:`pytest.RaisesGroup` as an equivalent to :func:`pytest.raises` for expecting :exc:`ExceptionGroup`. Also adds :class:`pytest.RaisesExc` which is now the logic behind :func:`pytest.raises` and used as parameter to :class:`pytest.RaisesGroup`. ``RaisesGroup`` includes the ability to specify multiple different expected exceptions, the structure of nested exception groups, and flags for emulating :ref:`except* `. See :ref:`assert-matching-exception-groups` and docstrings for more information. - - -- `#12081 `_: Added :fixture:`capteesys` to capture AND pass output to next handler set by ``--capture=``. - - -- `#12504 `_: :func:`pytest.mark.xfail` now accepts :class:`pytest.RaisesGroup` for the ``raises`` parameter when you expect an exception group. You can also pass a :class:`pytest.RaisesExc` if you e.g. want to make use of the ``check`` parameter. - - -- `#12713 `_: New `--force-short-summary` option to force condensed summary output regardless of verbosity level. - - This lets users still see condensed summary output of failures for quick reference in log files from job outputs, being especially useful if non-condensed output is very verbose. - - -- `#12749 `_: pytest traditionally collects classes/functions in the test module namespace even if they are imported from another file. - - For example: - - .. code-block:: python - - # contents of src/domain.py - class Testament: ... - - - # contents of tests/test_testament.py - from domain import Testament - - - def test_testament(): ... - - In this scenario with the default options, pytest will collect the class `Testament` from `tests/test_testament.py` because it starts with `Test`, even though in this case it is a production class being imported in the test module namespace. - - This behavior can now be prevented by setting the new :confval:`collect_imported_tests` configuration option to ``false``, which will make pytest collect classes/functions from test files **only** if they are defined in that file. - - -- by :user:`FreerGit` - - -- `#12765 `_: Thresholds to trigger snippet truncation can now be set with :confval:`truncation_limit_lines` and :confval:`truncation_limit_chars`. - - See :ref:`truncation-params` for more information. - - -- `#13125 `_: :confval:`console_output_style` now supports ``times`` to show execution time of each test. - - -- `#13192 `_: :func:`pytest.raises` will now raise a warning when passing an empty string to ``match``, as this will match against any value. Use ``match="^$"`` if you want to check that an exception has no message. - - -- `#13192 `_: :func:`pytest.raises` will now print a helpful string diff if matching fails and the match parameter has ``^`` and ``$`` and is otherwise escaped. - - -- `#13192 `_: You can now pass :func:`with pytest.raises(check=fn): `, where ``fn`` is a function which takes a raised exception and returns a boolean. The ``raises`` fails if no exception was raised (as usual), passes if an exception is raised and ``fn`` returns ``True`` (as well as ``match`` and the type matching, if specified, which are checked before), and propagates the exception if ``fn`` returns ``False`` (which likely also fails the test). - - -- `#13228 `_: :ref:`hidden-param` can now be used in ``id`` of :func:`pytest.param` or in - ``ids`` of :py:func:`Metafunc.parametrize `. - It hides the parameter set from the test name. - - -- `#13253 `_: New flag: :ref:`--disable-plugin-autoload ` which works as an alternative to :envvar:`PYTEST_DISABLE_PLUGIN_AUTOLOAD` when setting environment variables is inconvenient; and allows setting it in config files with :confval:`addopts`. - - - -Improvements in existing functionality --------------------------------------- - -- `#10224 `_: pytest's ``short`` and ``long`` traceback styles (:ref:`how-to-modifying-python-tb-printing`) - now have partial :pep:`657` support and will show specific code segments in the - traceback. - - .. code-block:: pytest - - ================================= FAILURES ================================= - _______________________ test_gets_correct_tracebacks _______________________ - - test_tracebacks.py:12: in test_gets_correct_tracebacks - assert manhattan_distance(p1, p2) == 1 - ^^^^^^^^^^^^^^^^^^^^^^^^^^ - test_tracebacks.py:6: in manhattan_distance - return abs(point_1.x - point_2.x) + abs(point_1.y - point_2.y) - ^^^^^^^^^ - E AttributeError: 'NoneType' object has no attribute 'x' - - -- by :user:`ammaraskar` - - -- `#11118 `_: Now :confval:`pythonpath` configures `$PYTHONPATH` earlier than before during the initialization process, which now also affects plugins loaded via the `-p` command-line option. - - -- by :user:`millerdev` - - -- `#11381 `_: The ``type`` parameter of the ``parser.addini`` method now accepts `"int"` and ``"float"`` parameters, facilitating the parsing of configuration values in the configuration file. - - Example: - - .. code-block:: python - - def pytest_addoption(parser): - parser.addini("int_value", type="int", default=2, help="my int value") - parser.addini("float_value", type="float", default=4.2, help="my float value") - - The `pytest.ini` file: - - .. code-block:: ini - - [pytest] - int_value = 3 - float_value = 5.4 - - -- `#11525 `_: Fixtures are now clearly represented in the output as a "fixture object", not as a normal function as before, making it easy for beginners to catch mistakes such as referencing a fixture declared in the same module but not requested in the test function. - - -- by :user:`the-compiler` and :user:`glyphack` - - -- `#12426 `_: A warning is now issued when :ref:`pytest.mark.usefixtures ref` is used without specifying any fixtures. Previously, empty usefixtures markers were silently ignored. - - -- `#12707 `_: Exception chains can be navigated when dropped into Pdb in Python 3.13+. - - -- `#12736 `_: Added a new attribute `name` with the fixed value `"pytest tests"` to the root tag `testsuites` of the junit-xml generated by pytest. - - This attribute is part of many junit-xml specifications and is even part of the `junit-10.xsd` specification that pytest's implementation is based on. - - -- `#12943 `_: If a test fails with an exceptiongroup with a single exception, the contained exception will now be displayed in the short test summary info. - - -- `#12958 `_: A number of :ref:`unraisable ` enhancements: - - * Set the unraisable hook as early as possible and unset it as late as possible, to collect the most possible number of unraisable exceptions. - * Call the garbage collector just before unsetting the unraisable hook, to collect any straggling exceptions. - * Collect multiple unraisable exceptions per test phase. - * Report the :mod:`tracemalloc` allocation traceback (if available). - * Avoid using a generator based hook to allow handling :class:`StopIteration` in test failures. - * Report the unraisable exception as the cause of the :class:`pytest.PytestUnraisableExceptionWarning` exception if raised. - * Compute the ``repr`` of the unraisable object in the unraisable hook so you get the latest information if available, and should help with resurrection of the object. - - -- `#13010 `_: :func:`pytest.approx` now can compare collections that contain numbers and non-numbers mixed. - - -- `#13016 `_: A number of :ref:`threadexception ` enhancements: - - * Set the excepthook as early as possible and unset it as late as possible, to collect the most possible number of unhandled exceptions from threads. - * Collect multiple thread exceptions per test phase. - * Report the :mod:`tracemalloc` allocation traceback (if available). - * Avoid using a generator based hook to allow handling :class:`StopIteration` in test failures. - * Report the thread exception as the cause of the :class:`pytest.PytestUnhandledThreadExceptionWarning` exception if raised. - * Extract the ``name`` of the thread object in the excepthook which should help with resurrection of the thread. - - -- `#13031 `_: An empty parameter set as in ``pytest.mark.parametrize([], ids=idfunc)`` will no longer trigger a call to ``idfunc`` with internal objects. - - -- `#13115 `_: Allows supplying ``ExceptionGroup[Exception]`` and ``BaseExceptionGroup[BaseException]`` to ``pytest.raises`` to keep full typing on :class:`ExceptionInfo `: - - .. code-block:: python - - with pytest.raises(ExceptionGroup[Exception]) as exc_info: - some_function() - - Parametrizing with other exception types remains an error - we do not check the types of child exceptions and thus do not permit code that might look like we do. - - -- `#13122 `_: The ``--stepwise`` mode received a number of improvements: - - * It no longer forgets the last failed test in case pytest is executed later without the flag. - - This enables the following workflow: - - 1. Execute pytest with ``--stepwise``, pytest then stops at the first failing test; - 2. Iteratively update the code and run the test in isolation, without the ``--stepwise`` flag (for example in an IDE), until it is fixed. - 3. Execute pytest with ``--stepwise`` again and pytest will continue from the previously failed test, and if it passes, continue on to the next tests. - - Previously, at step 3, pytest would start from the beginning, forgetting the previously failed test. - - This change however might cause issues if the ``--stepwise`` mode is used far apart in time, as the state might get stale, so the internal state will be reset automatically in case the test suite changes (for now only the number of tests are considered for this, we might change/improve this on the future). - - * New ``--stepwise-reset``/``--sw-reset`` flag, allowing the user to explicitly reset the stepwise state and restart the workflow from the beginning. - - -- `#13308 `_: Added official support for Python 3.14. - - -- `#13380 `_: Fix :class:`ExceptionGroup` traceback filtering to exclude pytest internals. - - -- `#13415 `_: The author metadata of the BibTex example is now correctly formatted with last names following first names. - An example of BibLaTex has been added. - BibTex and BibLaTex examples now clearly indicate that what is cited is software. - - -- by :user:`willynilly` - - -- `#13420 `_: Improved test collection performance by optimizing path resolution used in ``FSCollector``. - - -- `#13457 `_: The error message about duplicate parametrization no longer displays an internal stack trace. - - -- `#4112 `_: Using :ref:`pytest.mark.usefixtures ` on :func:`pytest.param` now produces an error instead of silently doing nothing. - - -- `#5473 `_: Replace `:` with `;` in the assertion rewrite warning message so it can be filtered using standard Python warning filters before calling :func:`pytest.main`. - - -- `#6985 `_: Improved :func:`pytest.approx` to enhance the readability of value ranges and tolerances between 0.001 and 1000. - * The `repr` method now provides clearer output for values within those ranges, making it easier to interpret the results. - * Previously, the output for those ranges of values and tolerances was displayed in scientific notation (e.g., `42 ± 1.0e+00`). The updated method now presents the tolerance as a decimal for better readability (e.g., `42 ± 1`). - - Example: - - **Previous Output:** - - .. code-block:: console - - >>> pytest.approx(42, abs=1) - 42 ± 1.0e+00 - - **Current Output:** - - .. code-block:: console - - >>> pytest.approx(42, abs=1) - 42 ± 1 - - -- by :user:`fazeelghafoor` - - -- `#7683 `_: The formerly optional ``pygments`` dependency is now required, causing output always to be source-highlighted (unless disabled via the ``--code-highlight=no`` CLI option). - - - -Bug fixes ---------- - -- `#10404 `_: Apply filterwarnings from config/cli as soon as possible, and revert them as late as possible - so that warnings as errors are collected throughout the pytest run and before the - unraisable and threadexcept hooks are removed. - - This allows very late warnings and unraisable/threadexcept exceptions to fail the test suite. - - This also changes the warning that the lsof plugin issues from PytestWarning to the new warning PytestFDWarning so it can be more easily filtered. - - -- `#11067 `_: The test report is now consistent regardless if the test xfailed via :ref:`pytest.mark.xfail ` or :func:`pytest.fail`. - - Previously, *xfailed* tests via the marker would have the string ``"reason: "`` prefixed to the message, while those *xfailed* via the function did not. The prefix has been removed. - - -- `#12008 `_: In :pr:`11220`, an unintended change in reordering was introduced by changing the way indices were assigned to direct params. More specifically, before that change, the indices of direct params to metafunc's callspecs were assigned after all parametrizations took place. Now, that change is reverted. - - -- `#12863 `_: Fix applying markers, including :ref:`pytest.mark.parametrize ` when placed above `@staticmethod` or `@classmethod`. - - -- `#12929 `_: Handle StopIteration from test cases, setup and teardown correctly. - - -- `#12938 `_: Fixed ``--durations-min`` argument not respected if ``-vv`` is used. - - -- `#12946 `_: Fixed missing help for :mod:`pdb` commands wrapped by pytest -- by :user:`adamchainz`. - - -- `#12981 `_: Prevent exceptions in :func:`pytest.Config.add_cleanup` callbacks preventing further cleanups. - - -- `#13047 `_: Restore :func:`pytest.approx` handling of equality checks between `bool` and `numpy.bool_` types. - - Comparing `bool` and `numpy.bool_` using :func:`pytest.approx` accidentally changed in version `8.3.4` and `8.3.5` to no longer match: - - .. code-block:: pycon - - >>> import numpy as np - >>> from pytest import approx - >>> [np.True_, np.True_] == pytest.approx([True, True]) - False - - This has now been fixed: - - .. code-block:: pycon - - >>> [np.True_, np.True_] == pytest.approx([True, True]) - True - - -- `#13119 `_: Improved handling of invalid regex patterns for filter warnings by providing a clear error message. - - -- `#13175 `_: The diff is now also highlighted correctly when comparing two strings. - - -- `#13248 `_: Fixed an issue where passing a ``scope`` in :py:func:`Metafunc.parametrize ` with ``indirect=True`` - could result in other fixtures being unable to depend on the parametrized fixture. - - -- `#13291 `_: Fixed ``repr`` of ``attrs`` objects in assertion failure messages when using ``attrs>=25.2``. - - -- `#13312 `_: Fixed a possible ``KeyError`` crash on PyPy during collection of tests involving higher-scoped parameters. - - -- `#13345 `_: Fix type hints for :attr:`pytest.TestReport.when` and :attr:`pytest.TestReport.location`. - - -- `#13377 `_: Fixed handling of test methods with positional-only parameter syntax. - - Now, methods are supported that formally define ``self`` as positional-only - and/or fixture parameters as keyword-only, e.g.: - - .. code-block:: python - - class TestClass: - - def test_method(self, /, *, fixture): ... - - Before, this caused an internal error in pytest. - - -- `#13384 `_: Fixed an issue where pytest could report negative durations. - - -- `#13420 `_: Added ``lru_cache`` to ``nodes._check_initialpaths_for_relpath``. - - -- `#9037 `_: Honor :confval:`disable_test_id_escaping_and_forfeit_all_rights_to_community_support` when escaping ids in parametrized tests. - - - -Improved documentation ----------------------- - -- `#12535 `_: `This - example` - showed ``print`` statements that do not exactly reflect what the - different branches actually do. The fix makes the example more precise. - - -- `#13218 `_: Pointed out in the :func:`pytest.approx` documentation that it considers booleans unequal to numeric zero or one. - - -- `#13221 `_: Improved grouping of CLI options in the ``--help`` output. - - -- `#6649 `_: Added :class:`~pytest.TerminalReporter` to the :ref:`api-reference` documentation page. - - -- `#8612 `_: Add a recipe for handling abstract test classes in the documentation. - - A new example has been added to the documentation to demonstrate how to use a mixin class to handle abstract - test classes without manually setting the ``__test__`` attribute for subclasses. - This ensures that subclasses of abstract test classes are automatically collected by pytest. - - - -Packaging updates and notes for downstreams -------------------------------------------- - -- `#13317 `_: Specified minimum allowed versions of ``colorama``, ``iniconfig``, - and ``packaging``; and bumped the minimum allowed version - of ``exceptiongroup`` for ``python_version<'3.11'`` from a release - candidate to a full release. - - - -Contributor-facing changes --------------------------- - -- `#12017 `_: Mixed internal improvements: - - * Migrate formatting to f-strings in some tests. - * Use type-safe constructs in JUnitXML tests. - * Moved`` MockTiming`` into ``_pytest.timing``. - - -- by :user:`RonnyPfannschmidt` - - -- `#12647 `_: Fixed running the test suite with the ``hypothesis`` pytest plugin. - - - -Miscellaneous internal changes ------------------------------- - -- `#6649 `_: Added :class:`~pytest.TerminalReporter` to the public pytest API, as it is part of the signature of the :hook:`pytest_terminal_summary` hook. - - pytest 8.3.5 (2025-03-02) ========================= @@ -942,7 +68,7 @@ Contributor-facing changes - `#13112 `_: Fixed selftest failures in ``test_terminal.py`` with Pygments >= 2.19.0 -- `#13256 `_: Support for Towncrier versions released in 2024 has been re-enabled +- `#13256 `_: Support for Towncier versions released in 2024 has been re-enabled when building Sphinx docs -- by :user:`webknjaz`. diff --git a/doc/en/example/parametrize.rst b/doc/en/example/parametrize.rst index 7aec1364953..a582285269e 100644 --- a/doc/en/example/parametrize.rst +++ b/doc/en/example/parametrize.rst @@ -162,7 +162,7 @@ objects, they are still using the default pytest representation: rootdir: /home/sweet/project collected 8 items - + @@ -239,7 +239,7 @@ If you just collect tests you'll also nicely see 'advanced' and 'basic' as varia rootdir: /home/sweet/project collected 4 items - + @@ -318,7 +318,7 @@ Let's first see how it looks like at collection time: rootdir: /home/sweet/project collected 2 items - + @@ -503,10 +503,12 @@ Running it results in some skips if we don't have all the python interpreters in .. code-block:: pytest . $ pytest -rs -q multipython.py - ssssssssssss......sss...... [100%] + sssssssssssssssssssssssssss [100%] ========================= short test summary info ========================== - SKIPPED [15] multipython.py:67: 'python3.11' not found - 12 passed, 15 skipped in 0.12s + SKIPPED [9] multipython.py:67: 'python3.9' not found + SKIPPED [9] multipython.py:67: 'python3.10' not found + SKIPPED [9] multipython.py:67: 'python3.11' not found + 27 skipped in 0.12s Parametrization of optional implementations/imports --------------------------------------------------- diff --git a/doc/en/example/pythoncollection.rst b/doc/en/example/pythoncollection.rst index 339944c4758..47a8cf71b70 100644 --- a/doc/en/example/pythoncollection.rst +++ b/doc/en/example/pythoncollection.rst @@ -142,7 +142,7 @@ The test collection would look like this: configfile: pytest.toml collected 2 items - + @@ -205,7 +205,7 @@ You can always peek at the collection tree without running tests like this: configfile: pytest.toml collected 3 items - + diff --git a/doc/en/example/reportingdemo.rst b/doc/en/example/reportingdemo.rst index 29ba190b7e7..92b470ff8de 100644 --- a/doc/en/example/reportingdemo.rst +++ b/doc/en/example/reportingdemo.rst @@ -574,12 +574,12 @@ Here is a nice run of several failures and how ``pytest`` presents things: E + where False = ('456') E + where = '123'.startswith E + where '123' = .f at 0xdeadbeef0029>() - E + and '456' = .g at 0xdeadbeef002a>() + E + and '456' = .g at 0xdeadbeef0003>() failure_demo.py:237: AssertionError _____________________ TestMoreErrors.test_global_func ______________________ - self = + self = def test_global_func(self): > assert isinstance(globf(42), float) @@ -590,18 +590,18 @@ Here is a nice run of several failures and how ``pytest`` presents things: failure_demo.py:240: AssertionError _______________________ TestMoreErrors.test_instance _______________________ - self = + self = def test_instance(self): self.x = 6 * 7 > assert self.x != 42 E assert 42 != 42 - E + where 42 = .x + E + where 42 = .x failure_demo.py:244: AssertionError _______________________ TestMoreErrors.test_compare ________________________ - self = + self = def test_compare(self): > assert globf(10) < 5 @@ -611,7 +611,7 @@ Here is a nice run of several failures and how ``pytest`` presents things: failure_demo.py:247: AssertionError _____________________ TestMoreErrors.test_try_finally ______________________ - self = + self = def test_try_finally(self): x = 1 @@ -622,7 +622,7 @@ Here is a nice run of several failures and how ``pytest`` presents things: failure_demo.py:252: AssertionError ___________________ TestCustomAssertMsg.test_single_line ___________________ - self = + self = def test_single_line(self): class A: @@ -637,7 +637,7 @@ Here is a nice run of several failures and how ``pytest`` presents things: failure_demo.py:263: AssertionError ____________________ TestCustomAssertMsg.test_multiline ____________________ - self = + self = def test_multiline(self): class A: @@ -656,7 +656,7 @@ Here is a nice run of several failures and how ``pytest`` presents things: failure_demo.py:270: AssertionError ___________________ TestCustomAssertMsg.test_custom_repr ___________________ - self = + self = def test_custom_repr(self): class JSON: diff --git a/doc/en/example/simple.rst b/doc/en/example/simple.rst index 0640160e3bb..19baa8680a7 100644 --- a/doc/en/example/simple.rst +++ b/doc/en/example/simple.rst @@ -166,8 +166,6 @@ Now we'll get feedback on a bad argument: $ pytest -q --cmdopt=type3 ERROR: usage: pytest [options] [file_or_dir] [file_or_dir] [...] pytest: error: argument --cmdopt: invalid choice: 'type3' (choose from type1, type2) - inifile: None - rootdir: /home/sweet/project If you need to provide more detailed error messages, you can use the diff --git a/doc/en/explanation/goodpractices.rst b/doc/en/explanation/goodpractices.rst index bbc64ec662d..57a52833cf9 100644 --- a/doc/en/explanation/goodpractices.rst +++ b/doc/en/explanation/goodpractices.rst @@ -217,7 +217,7 @@ Note that this layout also works in conjunction with the ``src`` layout mentione from each other and thus deriving a canonical import name helps to avoid surprises such as a test module getting imported twice. - With :option:`--import-mode=importlib` things are less convoluted because + With ``--import-mode=importlib`` things are less convoluted because pytest doesn't need to change ``sys.path``, making things much less surprising. diff --git a/doc/en/getting-started.rst b/doc/en/getting-started.rst index 3ba30a90b34..50f640ede86 100644 --- a/doc/en/getting-started.rst +++ b/doc/en/getting-started.rst @@ -20,7 +20,7 @@ Install ``pytest`` .. code-block:: bash $ pytest --version - pytest 9.0.2 + pytest 8.3.5 .. _`simpletest`: diff --git a/doc/en/how-to/capture-stdout-stderr.rst b/doc/en/how-to/capture-stdout-stderr.rst index 8a6a42d4134..eda5ef012bf 100644 --- a/doc/en/how-to/capture-stdout-stderr.rst +++ b/doc/en/how-to/capture-stdout-stderr.rst @@ -114,8 +114,8 @@ of the failing function and hide the other one: Accessing captured output from a test function --------------------------------------------------- -The :fixture:`capsys`, :fixture:`capteesys`, :fixture:`capsysbinary`, :fixture:`capfd`, and :fixture:`capfdbinary` -fixtures allow access to ``stdout``/``stderr`` output created during test execution. +The :fixture:`capsys`, :fixture:`capsysbinary`, :fixture:`capfd`, and :fixture:`capfdbinary` fixtures +allow access to ``stdout``/``stderr`` output created during test execution. Here is an example test function that performs some output related checks: diff --git a/doc/en/how-to/capture-warnings.rst b/doc/en/how-to/capture-warnings.rst index ae9e71a2750..6ad073953be 100644 --- a/doc/en/how-to/capture-warnings.rst +++ b/doc/en/how-to/capture-warnings.rst @@ -171,7 +171,7 @@ You can specify multiple filters with separate decorators: Filters applied using a mark take precedence over filters passed on the command line or configured -by the :confval:`filterwarnings` configuration option. +by the :confval:`filterwarnings` ini option. You may apply a filter to all tests of a class by using the :ref:`filterwarnings ` mark as a class decorator or to all tests in a module by setting the :globalvar:`pytestmark` variable: diff --git a/doc/en/how-to/fixtures.rst b/doc/en/how-to/fixtures.rst index 5c5a239e8d4..4ed5c0ded40 100644 --- a/doc/en/how-to/fixtures.rst +++ b/doc/en/how-to/fixtures.rst @@ -1423,7 +1423,7 @@ Running the above tests results in the following test IDs being used: rootdir: /home/sweet/project collected 12 items - + diff --git a/doc/en/how-to/subtests.rst b/doc/en/how-to/subtests.rst index 5a08dbc4769..956405bc7cf 100644 --- a/doc/en/how-to/subtests.rst +++ b/doc/en/how-to/subtests.rst @@ -29,37 +29,7 @@ Each assertion failure or error is caught by the context manager and reported in .. code-block:: pytest $ pytest -q test_subtest.py - uuuuuF [100%] - ================================= FAILURES ================================= - _______________________ test [custom message] (i=1) ________________________ - - subtests = <_pytest.subtests.Subtests object at 0xdeadbeef0001> - - def test(subtests): - for i in range(5): - with subtests.test(msg="custom message", i=i): - > assert i % 2 == 0 - E assert (1 % 2) == 0 - - test_subtest.py:6: AssertionError - _______________________ test [custom message] (i=3) ________________________ - - subtests = <_pytest.subtests.Subtests object at 0xdeadbeef0001> - - def test(subtests): - for i in range(5): - with subtests.test(msg="custom message", i=i): - > assert i % 2 == 0 - E assert (3 % 2) == 0 - - test_subtest.py:6: AssertionError - ___________________________________ test ___________________________________ - contains 2 failed subtests - ========================= short test summary info ========================== - SUBFAILED[custom message] (i=1) test_subtest.py::test - assert (1 % 2) == 0 - SUBFAILED[custom message] (i=3) test_subtest.py::test - assert (3 % 2) == 0 - FAILED test_subtest.py::test - contains 2 failed subtests - 3 failed, 3 subtests passed in 0.12s + In the output above: @@ -90,7 +60,7 @@ outside the ``subtests.test`` block: Verbosity --------- -By default, only **subtest failures** are shown. Higher verbosity levels (:option:`-v`) will also show progress output for **passed** subtests. +By default, only **subtest failures** are shown. Higher verbosity levels (``-v``) will also show progress output for **passed** subtests. It is possible to control the verbosity of subtests by setting :confval:`verbosity_subtests`. @@ -118,7 +88,7 @@ Parametrization * Happens at collection time. * Generates individual tests. * Parametrized tests can be referenced from the command line. -* Plays well with plugins that handle test execution, such as :option:`--last-failed`. +* Plays well with plugins that handle test execution, such as ``--last-failed``. * Ideal for decision table testing. Subtests diff --git a/doc/en/how-to/tmp_path.rst b/doc/en/how-to/tmp_path.rst index e73c55878a6..04c663bb986 100644 --- a/doc/en/how-to/tmp_path.rst +++ b/doc/en/how-to/tmp_path.rst @@ -136,9 +136,9 @@ Temporary directory location and retention The temporary directories, as returned by the :fixture:`tmp_path` and (now deprecated) :fixture:`tmpdir` fixtures, are automatically created under a base temporary directory, -in a structure that depends on the :option:`--basetemp` option: +in a structure that depends on the ``--basetemp`` option: -- By default (when the :option:`--basetemp` option is not set), +- By default (when the ``--basetemp`` option is not set), the temporary directories will follow this template: .. code-block:: text @@ -160,7 +160,7 @@ in a structure that depends on the :option:`--basetemp` option: but this behavior can be configured with :confval:`tmp_path_retention_count` and :confval:`tmp_path_retention_policy`. -- When the :option:`--basetemp` option is used (e.g. ``pytest --basetemp=mydir``), +- When the ``--basetemp`` option is used (e.g. ``pytest --basetemp=mydir``), it will be used directly as base temporary directory: .. code-block:: text @@ -172,7 +172,7 @@ in a structure that depends on the :option:`--basetemp` option: .. warning:: - The directory given to :option:`--basetemp` will be cleared blindly before each test run, + The directory given to ``--basetemp`` will be cleared blindly before each test run, so make sure to use a directory for that purpose only. When distributing tests on the local machine using ``pytest-xdist``, care is taken to diff --git a/doc/en/index.rst b/doc/en/index.rst index b98c886d981..eae2214f39b 100644 --- a/doc/en/index.rst +++ b/doc/en/index.rst @@ -2,7 +2,7 @@ .. sidebar:: **Next Open Trainings and Events** - - `Professional Testing with Python `_, via `Python Academy `_ (3 day in-depth training), **March 3th -- 5th 2026**, Leipzig (DE) / Remote + - `Professional Testing with Python `_, via `Python Academy `_ (3 day in-depth training), **March 4th -- 6th 2025**, Remote Also see :doc:`previous talks and blogposts ` diff --git a/doc/en/reference/reference.rst b/doc/en/reference/reference.rst index 62ae3564e18..6cb8fe56c4f 100644 --- a/doc/en/reference/reference.rst +++ b/doc/en/reference/reference.rst @@ -2498,17 +2498,8 @@ passed multiple times. The expected format is ``name=value``. For example:: .. tab:: toml - .. code-block:: toml - - [pytest] - tmp_path_retention_policy = "all" - - .. tab:: ini - - .. code-block:: ini - - [pytest] - tmp_path_retention_policy = all + [pytest] + tmp_path_retention_policy = all Default: ``all`` @@ -2601,17 +2592,7 @@ passed multiple times. The expected format is ``name=value``. For example:: .. code-block:: toml - [pytest] - verbosity_assertions = "2" - - .. tab:: ini - - .. code-block:: ini - - [pytest] - verbosity_assertions = 2 - - If not set, defaults to application wide verbosity level (via the :option:`-v` command-line option). A special value of + If not set, defaults to application wide verbosity level (via the ``-v`` command-line option). A special value of ``"auto"`` can be used to explicitly use the global verbosity level. @@ -2624,7 +2605,7 @@ passed multiple times. The expected format is ``name=value``. For example:: .. code-block:: toml [pytest] - verbosity_subtests = "1" + verbosity_subtests = 1 .. tab:: ini @@ -2634,9 +2615,9 @@ passed multiple times. The expected format is ``name=value``. For example:: verbosity_subtests = 1 A value of ``1`` or higher will show output for **passed** subtests (**failed** subtests are always reported). - Passed subtests output can be suppressed with the value ``0``, which overwrites the :option:`-v` command-line option. + Passed subtests output can be suppressed with the value ``0``, which overwrites the ``-v`` command-line option. - If not set, defaults to application wide verbosity level (via the :option:`-v` command-line option). A special value of + If not set, defaults to application wide verbosity level (via the ``-v`` command-line option). A special value of ``"auto"`` can be used to explicitly use the global verbosity level. See also: :ref:`subtests`. @@ -2650,8 +2631,8 @@ passed multiple times. The expected format is ``name=value``. For example:: .. code-block:: toml - [pytest] - verbosity_test_cases = "2" + If not set, defaults to application wide verbosity level (via the ``-v`` command-line option). A special value of + ``"auto"`` can be used to explicitly use the global verbosity level. .. tab:: ini @@ -3320,9 +3301,6 @@ All the command-line flags can also be obtained by running ``pytest --help``:: --no-header Disable header --no-summary Disable summary --no-fold-skipped Do not fold skipped tests in short summary. - --force-short-summary - Force condensed summary output regardless of - verbosity level. -q, --quiet Decrease verbosity --verbosity=VERBOSE Set verbosity. Default: 0. -r chars Show extra test summary info as specified by chars: @@ -3357,6 +3335,22 @@ All the command-line flags can also be obtained by running ``pytest --help``:: -W, --pythonwarnings PYTHONWARNINGS Set which warnings to report, see -W option of Python itself + --maxfail=num Exit after first num failures or errors + --strict-config Any warnings encountered while parsing the `pytest` + section of the configuration file raise errors + --strict-markers Markers not registered in the `markers` section of + the configuration file raise errors + --strict (Deprecated) alias to --strict-markers + -c, --config-file FILE + Load configuration from `FILE` instead of trying to + locate one of the implicit configuration files. + --continue-on-collection-errors + Force test execution even if collection errors occur + --rootdir=ROOTDIR Define root directory for tests. Can be relative + path: 'root_dir', './root_dir', + 'root_dir/another_dir/'; absolute path: + '/home/user/root_dir'; path with variables: + '$HOME/root_dir'. collection: --collect-only, --co Only collect tests, don't execute them @@ -3389,14 +3383,6 @@ All the command-line flags can also be obtained by running ``pytest --help``:: failure test session debugging and configuration: - -c, --config-file FILE - Load configuration from `FILE` instead of trying to - locate one of the implicit configuration files. - --rootdir=ROOTDIR Define root directory for tests. Can be relative - path: 'root_dir', './root_dir', - 'root_dir/another_dir/'; absolute path: - '/home/user/root_dir'; path with variables: - '$HOME/root_dir'. --basetemp=dir Base temporary directory for this test run. (Warning: this directory is removed if it exists.) -V, --version Display pytest version and information about @@ -3405,22 +3391,15 @@ All the command-line flags can also be obtained by running ``pytest --help``:: -h, --help Show help message and configuration info -p name Early-load given plugin module name or entry point (multi-allowed). To avoid loading of plugins, use - the `no:` prefix, e.g. `no:doctest`. See also - --disable-plugin-autoload. - --disable-plugin-autoload - Disable plugin auto-loading through entry point - packaging metadata. Only plugins explicitly - specified in -p or env var PYTEST_PLUGINS will be - loaded. + the `no:` prefix, e.g. `no:doctest`. --trace-config Trace considerations of conftest.py files --debug=[DEBUG_FILE_NAME] Store internal tracing debug information in this log file. This file is opened with 'w' and truncated as a result, care advised. Default: pytestdebug.log. -o, --override-ini OVERRIDE_INI - Override configuration option with "option=value" - style, e.g. `-o strict_xfail=True -o - cache_dir=cache`. + Override ini option with "option=value" style, e.g. + `-o xfail_strict=True -o cache_dir=cache`. --assert=MODE Control assertion debugging tools. 'plain' performs no assertion debugging. 'rewrite' (the default) rewrites assert statements @@ -3462,7 +3441,7 @@ All the command-line flags can also be obtained by running ``pytest --help``:: Disable a logger by name. Can be passed multiple times. - [pytest] configuration options in the first pytest.toml|pytest.ini|tox.ini|setup.cfg|pyproject.toml file found: + [pytest] ini-options in the first pytest.ini|tox.ini|setup.cfg|pyproject.toml file found: markers (linelist): Register new markers for test functions empty_parameter_set_mark (string): @@ -3501,9 +3480,6 @@ All the command-line flags can also be obtained by running ``pytest --help``:: disable_test_id_escaping_and_forfeit_all_rights_to_community_support (bool): Disable string escape non-ASCII characters, might cause unwanted side effects(use at your own risk) - strict_parametrization_ids (bool): - Emit an error if non-unique parameter set IDs are - detected console_output_style (string): Console output: "classic", or with additional progress information ("progress" (percentage) | @@ -3514,9 +3490,8 @@ All the command-line flags can also be obtained by running ``pytest --help``:: overriding the main level. Higher levels will provide more detailed information about each test case executed. - strict_xfail (bool): Default for the strict parameter of xfail markers - when not given explicitly (default: False) (alias: - xfail_strict) + xfail_strict (bool): Default for the strict parameter of xfail markers + when not given explicitly (default: False) tmp_path_retention_count (string): How many sessions should we keep the `tmp_path` directories, according to @@ -3581,13 +3556,6 @@ All the command-line flags can also be obtained by running ``pytest --help``:: faulthandler_timeout (string): Dump the traceback of all threads if a test takes more than TIMEOUT seconds to finish - faulthandler_exit_on_timeout (bool): - Exit the test process if a test takes more than - faulthandler_timeout seconds to finish - verbosity_subtests (string): - Specify verbosity level for subtests. Higher levels - will generate output for passed subtests. Failed - subtests are always reported. addopts (args): Extra command line options minversion (string): Minimally required pytest version pythonpath (paths): Add paths to sys.path @@ -3595,7 +3563,7 @@ All the command-line flags can also be obtained by running ``pytest --help``:: Plugins that must be present for pytest to run Environment variables: - CI When set to a non-empty value, pytest knows it is running in a CI process and does not truncate summary info + CI When set (regardless of value), pytest knows it is running in a CI process and does not truncate summary info BUILD_NUMBER Equivalent to CI PYTEST_ADDOPTS Extra command line options PYTEST_PLUGINS Comma-separated plugins to load during startup diff --git a/pyproject.toml b/pyproject.toml index 5956eff9f68..53acb5993a1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -340,8 +340,8 @@ disable = [ ] [tool.codespell] -ignore-words-list = "afile,asend,asser,assertio,feld,hove,ned,noes,notin,paramete,parth,tesults,varius,wil" -skip = "AUTHORS,*/plugin_list.rst" +ignore-words-list = "afile,asser,assertio,feld,hove,ned,noes,notin,paramete,parth,socio-economic,tesults,varius,wil" +skip = "*/plugin_list.rst" write-changes = true [tool.check-wheel-contents] @@ -561,7 +561,7 @@ files = [ mypy_path = [ "src", ] -python_version = "3.10" +python_version = "3.8" check_untyped_defs = true disallow_any_generics = true disallow_untyped_defs = true diff --git a/scripts/update-plugin-list.py b/scripts/update-plugin-list.py index be57d436966..88e602bb41f 100644 --- a/scripts/update-plugin-list.py +++ b/scripts/update-plugin-list.py @@ -67,7 +67,6 @@ "nuts", "flask_fixture", "databricks-labs-pytester", - "tursu", } diff --git a/src/_pytest/_code/code.py b/src/_pytest/_code/code.py index 4cf99a77340..a55dd6cf2fa 100644 --- a/src/_pytest/_code/code.py +++ b/src/_pytest/_code/code.py @@ -54,7 +54,7 @@ TracebackStyle = Literal["long", "short", "line", "no", "native", "value", "auto"] -EXCEPTION_OR_MORE = type[BaseException] | tuple[type[BaseException], ...] +EXCEPTION_OR_MORE = Union[Type[BaseException], Tuple[Type[BaseException], ...]] class Code: diff --git a/src/_pytest/_io/pprint.py b/src/_pytest/_io/pprint.py index 28f06909206..3fb8ab3f832 100644 --- a/src/_pytest/_io/pprint.py +++ b/src/_pytest/_io/pprint.py @@ -111,7 +111,7 @@ def _format( p(self, object, stream, indent, allowance, context, level + 1) context.remove(objid) elif ( - _dataclasses.is_dataclass(object) + _dataclasses.is_dataclass(object) # type:ignore[unreachable] and not isinstance(object, type) and object.__dataclass_params__.repr # type:ignore[attr-defined] and @@ -119,7 +119,7 @@ def _format( hasattr(object.__repr__, "__wrapped__") and "__create_fn__" in object.__repr__.__wrapped__.__qualname__ ): - context.add(objid) + context.add(objid) # type:ignore[unreachable] self._pprint_dataclass( object, stream, indent, allowance, context, level + 1 ) diff --git a/src/_pytest/capture.py b/src/_pytest/capture.py index 6d98676be5f..69abb8c24b2 100644 --- a/src/_pytest/capture.py +++ b/src/_pytest/capture.py @@ -1024,41 +1024,6 @@ def test_output(capsys): capman.unset_fixture() -@fixture -def capteesys(request: SubRequest) -> Generator[CaptureFixture[str]]: - r"""Enable simultaneous text capturing and pass-through of writes - to ``sys.stdout`` and ``sys.stderr`` as defined by ``--capture=``. - - - The captured output is made available via ``capteesys.readouterr()`` method - calls, which return a ``(out, err)`` namedtuple. - ``out`` and ``err`` will be ``text`` objects. - - The output is also passed-through, allowing it to be "live-printed", - reported, or both as defined by ``--capture=``. - - Returns an instance of :class:`CaptureFixture[str] `. - - Example: - - .. code-block:: python - - def test_output(capteesys): - print("hello") - captured = capteesys.readouterr() - assert captured.out == "hello\n" - """ - capman: CaptureManager = request.config.pluginmanager.getplugin("capturemanager") - capture_fixture = CaptureFixture( - SysCapture, request, config=dict(tee=True), _ispytest=True - ) - capman.set_fixture(capture_fixture) - capture_fixture._start() - yield capture_fixture - capture_fixture.close() - capman.unset_fixture() - - @fixture def capsysbinary(request: SubRequest) -> Generator[CaptureFixture[bytes]]: r"""Enable bytes capturing of writes to ``sys.stdout`` and ``sys.stderr``. diff --git a/src/_pytest/fixtures.py b/src/_pytest/fixtures.py index d8d19fcac6d..d8ef11fe050 100644 --- a/src/_pytest/fixtures.py +++ b/src/_pytest/fixtures.py @@ -48,7 +48,6 @@ from _pytest.compat import NotSetType from _pytest.compat import safe_getattr from _pytest.compat import safe_isclass -from _pytest.compat import signature from _pytest.config import _PluggyPlugin from _pytest.config import Config from _pytest.config import ExitCode @@ -1873,19 +1872,17 @@ def parsefactories( # The attribute can be an arbitrary descriptor, so the attribute # access below can raise. safe_getattr() ignores such exceptions. obj_ub = safe_getattr(holderobj_tp, name, None) - if type(obj_ub) is FixtureFunctionDefinition: - marker = obj_ub._fixture_function_marker - if marker.name: - fixture_name = marker.name - else: - fixture_name = name - - # OK we know it is a fixture -- now safe to look up on the _instance_. - try: - obj = getattr(holderobj, name) - # if the fixture is named in the decorator we cannot find it in the module - except AttributeError: - obj = obj_ub + marker = getfixturemarker(obj_ub) + if not isinstance(marker, FixtureFunctionMarker): + # Magic globals with __getattr__ might have got us a wrong + # fixture attribute. + continue + + # OK we know it is a fixture -- now safe to look up on the _instance_. + obj = getattr(holderobj, name) + + if marker.name: + name = marker.name func = obj._get_wrapped_function() diff --git a/src/_pytest/logging.py b/src/_pytest/logging.py index 6f34c1b93fd..eaa66757fb1 100644 --- a/src/_pytest/logging.py +++ b/src/_pytest/logging.py @@ -809,7 +809,6 @@ def pytest_runtest_logstart(self) -> None: def pytest_runtest_logreport(self) -> None: self.log_cli_handler.set_when("logreport") - @contextmanager def _runtest_for(self, item: nodes.Item, when: str) -> Generator[None]: """Implement the internals of the pytest_runtest_xxx() hooks.""" with ( diff --git a/src/_pytest/pathlib.py b/src/_pytest/pathlib.py index cd15434605d..83c845942a5 100644 --- a/src/_pytest/pathlib.py +++ b/src/_pytest/pathlib.py @@ -711,7 +711,7 @@ def _import_module_using_spec( if module_path.is_dir(): # The `spec_from_file_location` matches a loader based on the file extension by default. # For a namespace package, need to manually specify a loader. - loader = NamespaceLoader(name, module_path, PathFinder()) # type: ignore[arg-type] + loader = NamespaceLoader(name, module_path, PathFinder()) spec = importlib.util.spec_from_file_location( module_name, str(module_path), loader=loader diff --git a/src/_pytest/python_api.py b/src/_pytest/python_api.py index 1e389eb0663..e22e60db77e 100644 --- a/src/_pytest/python_api.py +++ b/src/_pytest/python_api.py @@ -406,7 +406,7 @@ def __repr__(self) -> str: # handle complex numbers, e.g. (inf + 1j). if ( isinstance(self.expected, bool) - or (not isinstance(self.expected, Complex | Decimal)) + or (not isinstance(self.expected, (Complex, Decimal))) or math.isinf(abs(self.expected) or isinstance(self.expected, bool)) ): return str(self.expected) @@ -448,8 +448,8 @@ def is_bool(val: Any) -> bool: # numpy<1.13. See #3748. return all(self.__eq__(a) for a in asarray.flat) - # Short-circuit exact equality, except for bool and np.bool_ - if is_bool(self.expected) and not is_bool(actual): + # Short-circuit exact equality, except for bool + if isinstance(self.expected, bool) and not isinstance(actual, bool): return False elif actual == self.expected: return True @@ -457,10 +457,10 @@ def is_bool(val: Any) -> bool: # If either type is non-numeric, fall back to strict equality. # NB: we need Complex, rather than just Number, to ensure that __abs__, # __sub__, and __float__ are defined. Also, consider bool to be - # non-numeric, even though it has the required arithmetic. - if is_bool(self.expected) or not ( - isinstance(self.expected, Complex | Decimal) - and isinstance(actual, Complex | Decimal) + # nonnumeric, even though it has the required arithmetic. + if isinstance(self.expected, bool) or not ( + isinstance(self.expected, (Complex, Decimal)) + and isinstance(actual, (Complex, Decimal)) ): return False diff --git a/src/_pytest/stepwise.py b/src/_pytest/stepwise.py index 8901540eb59..8992271e401 100644 --- a/src/_pytest/stepwise.py +++ b/src/_pytest/stepwise.py @@ -195,9 +195,9 @@ def pytest_runtest_logreport(self, report: TestReport) -> None: if report.nodeid == self.cached_info.last_failed: self.cached_info.last_failed = None - def pytest_report_collectionfinish(self) -> list[str] | None: + def pytest_report_collectionfinish(self) -> str | None: if self.config.get_verbosity() >= 0 and self.report_status: - return [f"stepwise: {x}" for x in self.report_status] + return f"stepwise: {self.report_status}" return None def pytest_sessionfinish(self) -> None: diff --git a/src/_pytest/subtests.py b/src/_pytest/subtests.py index 4856f72b9ff..d9455c80544 100644 --- a/src/_pytest/subtests.py +++ b/src/_pytest/subtests.py @@ -360,7 +360,7 @@ def pytest_report_from_serializable(data: dict[str, Any]) -> SubtestReport | Non def pytest_configure(config: Config) -> None: - config.stash[failed_subtests_key] = defaultdict(int) + config.stash[failed_subtests_key] = defaultdict(lambda: 0) @hookimpl(tryfirst=True) diff --git a/src/_pytest/threadexception.py b/src/_pytest/threadexception.py index eb57783be26..52340cbaf23 100644 --- a/src/_pytest/threadexception.py +++ b/src/_pytest/threadexception.py @@ -30,31 +30,23 @@ class ThreadExceptionMeta(NamedTuple): exc_value: BaseException | None -thread_exceptions: StashKey[collections.deque[ThreadExceptionMeta | BaseException]] = ( - StashKey() -) - - -def collect_thread_exception(config: Config) -> None: - pop_thread_exception = config.stash[thread_exceptions].pop - errors: list[pytest.PytestUnhandledThreadExceptionWarning | RuntimeError] = [] - meta = None - hook_error = None - try: - while True: - try: - meta = pop_thread_exception() - except IndexError: - break - - if isinstance(meta, BaseException): - hook_error = RuntimeError("Failed to process thread exception") - hook_error.__cause__ = meta - errors.append(hook_error) - continue - - msg = meta.msg - try: +def thread_exception_runtest_hook() -> Generator[None]: + with catch_threading_exception() as cm: + try: + yield + finally: + if cm.args: + thread_name = ( + "" if cm.args.thread is None else cm.args.thread.name + ) + msg = f"Exception in thread {thread_name}\n\n" + msg += "".join( + traceback.format_exception( + cm.args.exc_type, + cm.args.exc_value, + cm.args.exc_traceback, + ) + ) warnings.warn(pytest.PytestUnhandledThreadExceptionWarning(msg)) except pytest.PytestUnhandledThreadExceptionWarning as e: # This except happens when the warning is treated as an error (e.g. `-Werror`). @@ -74,79 +66,16 @@ def collect_thread_exception(config: Config) -> None: del errors, meta, hook_error -def cleanup( - *, config: Config, prev_hook: Callable[[threading.ExceptHookArgs], object] -) -> None: - try: - try: - # We don't join threads here, so exceptions raised from any - # threads still running by the time _threading_atexits joins them - # do not get captured (see #13027). - collect_thread_exception(config) - finally: - threading.excepthook = prev_hook - finally: - del config.stash[thread_exceptions] - - -def thread_exception_hook( - args: threading.ExceptHookArgs, - /, - *, - append: Callable[[ThreadExceptionMeta | BaseException], object], -) -> None: - try: - # we need to compute these strings here as they might change after - # the excepthook finishes and before the metadata object is - # collected by a pytest hook - thread_name = "" if args.thread is None else args.thread.name - summary = f"Exception in thread {thread_name}" - traceback_message = "\n\n" + "".join( - traceback.format_exception( - args.exc_type, - args.exc_value, - args.exc_traceback, - ) - ) - tracemalloc_tb = "\n" + tracemalloc_message(args.thread) - msg = summary + traceback_message + tracemalloc_tb - cause_msg = summary + tracemalloc_tb - - append( - ThreadExceptionMeta( - # Compute these strings here as they might change later - msg=msg, - cause_msg=cause_msg, - exc_value=args.exc_value, - ) - ) - except BaseException as e: - append(e) - # Raising this will cause the exception to be logged twice, once in our - # collect_thread_exception and once by sys.excepthook - # which is fine - this should never happen anyway and if it does - # it should probably be reported as a pytest bug. - raise - - -def pytest_configure(config: Config) -> None: - prev_hook = threading.excepthook - deque: collections.deque[ThreadExceptionMeta | BaseException] = collections.deque() - config.stash[thread_exceptions] = deque - config.add_cleanup(functools.partial(cleanup, config=config, prev_hook=prev_hook)) - threading.excepthook = functools.partial(thread_exception_hook, append=deque.append) - - -@pytest.hookimpl(trylast=True) -def pytest_runtest_setup(item: Item) -> None: - collect_thread_exception(item.config) - - -@pytest.hookimpl(trylast=True) -def pytest_runtest_call(item: Item) -> None: - collect_thread_exception(item.config) - - -@pytest.hookimpl(trylast=True) -def pytest_runtest_teardown(item: Item) -> None: - collect_thread_exception(item.config) +@pytest.hookimpl(wrapper=True, trylast=True) +def pytest_runtest_setup() -> Generator[None]: + yield from thread_exception_runtest_hook() + + +@pytest.hookimpl(wrapper=True, tryfirst=True) +def pytest_runtest_call() -> Generator[None]: + yield from thread_exception_runtest_hook() + + +@pytest.hookimpl(wrapper=True, tryfirst=True) +def pytest_runtest_teardown() -> Generator[None]: + yield from thread_exception_runtest_hook() diff --git a/src/_pytest/unittest.py b/src/_pytest/unittest.py index 23b92724f5d..a6c779a045f 100644 --- a/src/_pytest/unittest.py +++ b/src/_pytest/unittest.py @@ -317,7 +317,11 @@ def add_skip() -> None: # We also need to check if `self.instance._outcome` is `None` (this happens if the test # class/method is decorated with `unittest.skip`, see pytest-dev/pytest-subtests#173). if sys.version_info < (3, 11) and self.instance._outcome is not None: - subtest_errors, _ = self._obtain_errors_and_skips() + subtest_errors = [ + x + for x, y in self.instance._outcome.errors + if isinstance(x, _SubTest) and y is not None + ] if len(subtest_errors) == 0: add_skip() else: @@ -443,8 +447,18 @@ def addSubTest( # For python < 3.11: add non-subtest skips once all subtest failures are processed by # `_addSubTest`. if sys.version_info < (3, 11): - subtest_errors, non_subtest_skip = self._obtain_errors_and_skips() - + from unittest.case import _SubTest # type: ignore[attr-defined] + + non_subtest_skip = [ + (x, y) + for x, y in self.instance._outcome.skipped + if not isinstance(x, _SubTest) + ] + subtest_errors = [ + (x, y) + for x, y in self.instance._outcome.errors + if isinstance(x, _SubTest) and y is not None + ] # Check if we have non-subtest skips: if there are also sub failures, non-subtest skips are not treated in # `_addSubTest` and have to be added using `add_skip` after all subtest failures are processed. if len(non_subtest_skip) > 0 and len(subtest_errors) > 0: @@ -455,30 +469,6 @@ def addSubTest( for testcase, reason in non_subtest_skip: self.addSkip(testcase, reason, handle_subtests=False) - def _obtain_errors_and_skips(self) -> tuple[list[Any], list[Any]]: - """Compute or obtain the cached values for subtest errors and non-subtest skips.""" - from unittest.case import _SubTest # type: ignore[attr-defined] - - assert sys.version_info < (3, 11), ( - "This workaround only should be used in Python 3.10" - ) - if self._cached_errors_and_skips is not None: - return self._cached_errors_and_skips - - subtest_errors = [ - (x, y) - for x, y in self.instance._outcome.errors - if isinstance(x, _SubTest) and y is not None - ] - - non_subtest_skips = [ - (x, y) - for x, y in self.instance._outcome.skipped - if not isinstance(x, _SubTest) - ] - self._cached_errors_and_skips = (subtest_errors, non_subtest_skips) - return subtest_errors, non_subtest_skips - @hookimpl(tryfirst=True) def pytest_runtest_makereport(item: Item, call: CallInfo[None]) -> None: diff --git a/src/_pytest/unraisableexception.py b/src/_pytest/unraisableexception.py index 0faca36aa00..8ed5794dd1a 100644 --- a/src/_pytest/unraisableexception.py +++ b/src/_pytest/unraisableexception.py @@ -28,42 +28,24 @@ gc_collect_iterations_key = StashKey[int]() -def gc_collect_harder(iterations: int) -> None: - for _ in range(iterations): - gc.collect() - - -class UnraisableMeta(NamedTuple): - msg: str - cause_msg: str - exc_value: BaseException | None - - -unraisable_exceptions: StashKey[collections.deque[UnraisableMeta | BaseException]] = ( - StashKey() -) - - -def collect_unraisable(config: Config) -> None: - pop_unraisable = config.stash[unraisable_exceptions].pop - errors: list[pytest.PytestUnraisableExceptionWarning | RuntimeError] = [] - meta = None - hook_error = None - try: - while True: - try: - meta = pop_unraisable() - except IndexError: - break - - if isinstance(meta, BaseException): - hook_error = RuntimeError("Failed to process unraisable exception") - hook_error.__cause__ = meta - errors.append(hook_error) - continue - - msg = meta.msg - try: +def unraisable_exception_runtest_hook() -> Generator[None]: + with catch_unraisable_exception() as cm: + try: + yield + finally: + if cm.unraisable: + if cm.unraisable.err_msg is not None: + err_msg = cm.unraisable.err_msg + else: + err_msg = "Exception ignored in" + msg = f"{err_msg}: {cm.unraisable.object!r}\n\n" + msg += "".join( + traceback.format_exception( + cm.unraisable.exc_type, + cm.unraisable.exc_value, + cm.unraisable.exc_traceback, + ) + ) warnings.warn(pytest.PytestUnraisableExceptionWarning(msg)) except pytest.PytestUnraisableExceptionWarning as e: # This except happens when the warning is treated as an error (e.g. `-Werror`). @@ -83,81 +65,16 @@ def collect_unraisable(config: Config) -> None: del errors, meta, hook_error -def cleanup( - *, config: Config, prev_hook: Callable[[sys.UnraisableHookArgs], object] -) -> None: - # A single collection doesn't necessarily collect everything. - # Constant determined experimentally by the Trio project. - gc_collect_iterations = config.stash.get(gc_collect_iterations_key, 5) - try: - try: - gc_collect_harder(gc_collect_iterations) - collect_unraisable(config) - finally: - sys.unraisablehook = prev_hook - finally: - del config.stash[unraisable_exceptions] - - -def unraisable_hook( - unraisable: sys.UnraisableHookArgs, - /, - *, - append: Callable[[UnraisableMeta | BaseException], object], -) -> None: - try: - # we need to compute these strings here as they might change after - # the unraisablehook finishes and before the metadata object is - # collected by a pytest hook - err_msg = ( - "Exception ignored in" if unraisable.err_msg is None else unraisable.err_msg - ) - summary = f"{err_msg}: {unraisable.object!r}" - traceback_message = "\n\n" + "".join( - traceback.format_exception( - unraisable.exc_type, - unraisable.exc_value, - unraisable.exc_traceback, - ) - ) - tracemalloc_tb = "\n" + tracemalloc_message(unraisable.object) - msg = summary + traceback_message + tracemalloc_tb - cause_msg = summary + tracemalloc_tb - - append( - UnraisableMeta( - msg=msg, - cause_msg=cause_msg, - exc_value=unraisable.exc_value, - ) - ) - except BaseException as e: - append(e) - # Raising this will cause the exception to be logged twice, once in our - # collect_unraisable and once by the unraisablehook calling machinery - # which is fine - this should never happen anyway and if it does - # it should probably be reported as a pytest bug. - raise - - -def pytest_configure(config: Config) -> None: - prev_hook = sys.unraisablehook - deque: collections.deque[UnraisableMeta | BaseException] = collections.deque() - config.stash[unraisable_exceptions] = deque - config.add_cleanup(functools.partial(cleanup, config=config, prev_hook=prev_hook)) - sys.unraisablehook = functools.partial(unraisable_hook, append=deque.append) - - -@pytest.hookimpl(trylast=True) -def pytest_runtest_setup(item: Item) -> None: - collect_unraisable(item.config) - - -@pytest.hookimpl(trylast=True) -def pytest_runtest_call(item: Item) -> None: - collect_unraisable(item.config) - - -@pytest.hookimpl(trylast=True) -def pytest_runtest_teardown(item: Item) -> None: - collect_unraisable(item.config) +@pytest.hookimpl(wrapper=True, tryfirst=True) +def pytest_runtest_setup() -> Generator[None]: + yield from unraisable_exception_runtest_hook() + + +@pytest.hookimpl(wrapper=True, tryfirst=True) +def pytest_runtest_call() -> Generator[None]: + yield from unraisable_exception_runtest_hook() + + +@pytest.hookimpl(wrapper=True, tryfirst=True) +def pytest_runtest_teardown() -> Generator[None]: + yield from unraisable_exception_runtest_hook() diff --git a/src/_pytest/warnings.py b/src/_pytest/warnings.py index 1dbf0025a31..dc3efd781f3 100644 --- a/src/_pytest/warnings.py +++ b/src/_pytest/warnings.py @@ -24,8 +24,6 @@ def catch_warnings_for_item( ihook, when: Literal["config", "collect", "runtest"], item: Item | None, - *, - record: bool = True, ) -> Generator[None]: """Context manager that catches warnings generated in the contained execution block. diff --git a/testing/acceptance_test.py b/testing/acceptance_test.py index f941cbe1921..b169dd28566 100644 --- a/testing/acceptance_test.py +++ b/testing/acceptance_test.py @@ -1587,49 +1587,3 @@ def test_no_terminal_plugin(pytester: Pytester) -> None: pytester.makepyfile("def test(): assert 1 == 2") result = pytester.runpytest("-pno:terminal", "-s") assert result.ret == ExitCode.TESTS_FAILED - - -def test_stop_iteration_from_collect(pytester: Pytester) -> None: - pytester.makepyfile(test_it="raise StopIteration('hello')") - result = pytester.runpytest() - assert result.ret == ExitCode.INTERRUPTED - result.assert_outcomes(failed=0, passed=0, errors=1) - result.stdout.fnmatch_lines( - [ - "=* short test summary info =*", - "ERROR test_it.py - StopIteration: hello", - "!* Interrupted: 1 error during collection !*", - "=* 1 error in * =*", - ] - ) - - -def test_stop_iteration_runtest_protocol(pytester: Pytester) -> None: - pytester.makepyfile( - test_it=""" - import pytest - @pytest.fixture - def fail_setup(): - raise StopIteration(1) - def test_fail_setup(fail_setup): - pass - def test_fail_teardown(request): - def stop_iteration(): - raise StopIteration(2) - request.addfinalizer(stop_iteration) - def test_fail_call(): - raise StopIteration(3) - """ - ) - result = pytester.runpytest() - assert result.ret == ExitCode.TESTS_FAILED - result.assert_outcomes(failed=1, passed=1, errors=2) - result.stdout.fnmatch_lines( - [ - "=* short test summary info =*", - "FAILED test_it.py::test_fail_call - StopIteration: 3", - "ERROR test_it.py::test_fail_setup - StopIteration: 1", - "ERROR test_it.py::test_fail_teardown - StopIteration: 2", - "=* 1 failed, 1 passed, 2 errors in * =*", - ] - ) diff --git a/testing/conftest.py b/testing/conftest.py index 663c9d80b3e..a2fc6077425 100644 --- a/testing/conftest.py +++ b/testing/conftest.py @@ -1,13 +1,15 @@ # mypy: allow-untyped-defs from __future__ import annotations -from collections.abc import Generator +import dataclasses import importlib.metadata import re import sys from packaging.version import Version +from packaging.version import Version + from _pytest.monkeypatch import MonkeyPatch from _pytest.pytester import Pytester import pytest diff --git a/testing/io/test_terminalwriter.py b/testing/io/test_terminalwriter.py index 9aa89da0e41..89e72924d02 100644 --- a/testing/io/test_terminalwriter.py +++ b/testing/io/test_terminalwriter.py @@ -69,7 +69,6 @@ def test_terminalwriter_not_unicode() -> None: class TestTerminalWriter: @pytest.fixture(params=["path", "stringio"]) def tw(self, request, tmp_path: Path) -> Generator[terminalwriter.TerminalWriter]: - f: io.TextIOWrapper | StringIO if request.param == "path": p = tmp_path.joinpath("tmpfile") f = open(str(p), "w+", encoding="utf8") diff --git a/testing/python/approx.py b/testing/python/approx.py index f870b9bd4d8..59dfcff0d4c 100644 --- a/testing/python/approx.py +++ b/testing/python/approx.py @@ -648,15 +648,6 @@ def test_expecting_bool(self) -> None: assert True != approx(False, abs=2) # noqa: E712 assert 1 != approx(True) - def test_expecting_bool_numpy(self) -> None: - """Check approx comparing with numpy.bool (#13047).""" - np = pytest.importorskip("numpy") - assert np.False_ != approx(True) - assert np.True_ != approx(False) - assert np.True_ == approx(True) - assert np.False_ == approx(False) - assert np.True_ != approx(False, abs=2) - def test_list(self): actual = [1 + 1e-7, 2 + 1e-8] expected = [1, 2] diff --git a/testing/test_assertrewrite.py b/testing/test_assertrewrite.py index 92664354470..784fca9e5ea 100644 --- a/testing/test_assertrewrite.py +++ b/testing/test_assertrewrite.py @@ -2,8 +2,6 @@ from __future__ import annotations import ast -from collections.abc import Generator -from collections.abc import Mapping import dis import errno from functools import partial @@ -132,7 +130,7 @@ def test_location_is_set(self) -> None: if isinstance(node, ast.Import): continue for n in [node, *ast.iter_child_nodes(node)]: - assert isinstance(n, ast.stmt | ast.expr) + assert isinstance(n, (ast.stmt, ast.expr)) for location in [ (n.lineno, n.col_offset), (n.end_lineno, n.end_col_offset), diff --git a/testing/test_monkeypatch.py b/testing/test_monkeypatch.py index c321439e398..4f054f5837c 100644 --- a/testing/test_monkeypatch.py +++ b/testing/test_monkeypatch.py @@ -416,7 +416,7 @@ def test_context() -> None: with monkeypatch.context() as m: m.setattr(functools, "partial", 3) assert not inspect.isclass(functools.partial) - assert inspect.isclass(functools.partial) + assert inspect.isclass(functools.partial) # type:ignore[unreachable] def test_context_classmethod() -> None: diff --git a/testing/test_pathlib.py b/testing/test_pathlib.py index 1dec3c6ec78..0880c355557 100644 --- a/testing/test_pathlib.py +++ b/testing/test_pathlib.py @@ -964,7 +964,7 @@ def test_demo(): ) # unit test - importlib.import_module(name) # import standard library + __import__(name) # import standard library import_path( # import user files file_path, diff --git a/testing/test_terminal.py b/testing/test_terminal.py index 3053f5ef9a1..0b928fd5025 100644 --- a/testing/test_terminal.py +++ b/testing/test_terminal.py @@ -2693,28 +2693,11 @@ def test_len_dict(): [ "*short test summary info*", f"*{list(range(10))}*", - f"*{dict(zip(range(10), range(10), strict=True))}*", + f"*{dict(zip(range(10), range(10)))}*", ] ) -def test_force_short_summary(monkeypatch: MonkeyPatch, pytester: Pytester) -> None: - monkeypatch.setattr(_pytest.terminal, "running_on_ci", lambda: False) - - pytester.makepyfile( - """ - def test(): - assert "a\\n" * 10 == "" - """ - ) - - result = pytester.runpytest("-vv", "--force-short-summary") - assert result.ret == 1 - result.stdout.fnmatch_lines( - ["*short test summary info*", "*AssertionError: assert 'a\\na\\na\\na..."] - ) - - @pytest.mark.parametrize( "seconds, expected", [ @@ -3407,144 +3390,3 @@ def test_x(a): r".*test_foo.py::test_x\[a::b/\] .*FAILED.*", ] ) - - -class TestTerminalProgressPlugin: - """Tests for the TerminalProgressPlugin.""" - - @pytest.fixture - def mock_file(self) -> StringIO: - return StringIO() - - @pytest.fixture - def mock_tr(self, mock_file: StringIO) -> pytest.TerminalReporter: - tr: pytest.TerminalReporter = mock.create_autospec(pytest.TerminalReporter) - - def write_raw(content: str, *, flush: bool = False) -> None: - mock_file.write(content) - - tr.write_raw = write_raw # type: ignore[method-assign] - tr._progress_nodeids_reported = set() - return tr - - @pytest.mark.skipif(sys.platform != "win32", reason="#13896") - def test_plugin_registration_enabled_by_default( - self, pytester: pytest.Pytester, monkeypatch: MonkeyPatch - ) -> None: - """Test that the plugin registration is enabled by default. - - Currently only on Windows (#13896). - """ - monkeypatch.setattr(sys.stdout, "isatty", lambda: True) - # The plugin module should be registered as a default plugin. - config = pytester.parseconfigure() - plugin = config.pluginmanager.get_plugin("terminalprogress") - assert plugin is not None - - def test_plugin_registred_on_all_platforms_when_explicitly_requested( - self, pytester: pytest.Pytester, monkeypatch: MonkeyPatch - ) -> None: - """Test that the plugin is registered on any platform if explicitly requested.""" - monkeypatch.setattr(sys.stdout, "isatty", lambda: True) - # The plugin module should be registered as a default plugin. - config = pytester.parseconfigure("-p", "terminalprogress") - plugin = config.pluginmanager.get_plugin("terminalprogress") - assert plugin is not None - - def test_disabled_for_non_tty( - self, pytester: pytest.Pytester, monkeypatch: MonkeyPatch - ) -> None: - """Test that plugin is disabled for non-TTY output.""" - monkeypatch.setattr(sys.stdout, "isatty", lambda: False) - config = pytester.parseconfigure("-p", "terminalprogress") - plugin = config.pluginmanager.get_plugin("terminalprogress-plugin") - assert plugin is None - - def test_disabled_for_dumb_terminal( - self, pytester: pytest.Pytester, monkeypatch: MonkeyPatch - ) -> None: - """Test that plugin is disabled when TERM=dumb.""" - monkeypatch.setenv("TERM", "dumb") - monkeypatch.setattr(sys.stdout, "isatty", lambda: True) - config = pytester.parseconfigure("-p", "terminalprogress") - plugin = config.pluginmanager.get_plugin("terminalprogress-plugin") - assert plugin is None - - @pytest.mark.parametrize( - ["state", "progress", "expected"], - [ - ("indeterminate", None, "\x1b]9;4;3;\x1b\\"), - ("normal", 50, "\x1b]9;4;1;50\x1b\\"), - ("error", 75, "\x1b]9;4;2;75\x1b\\"), - ("paused", None, "\x1b]9;4;4;\x1b\\"), - ("paused", 80, "\x1b]9;4;4;80\x1b\\"), - ("remove", None, "\x1b]9;4;0;\x1b\\"), - ], - ) - def test_emit_progress_sequences( - self, - mock_file: StringIO, - mock_tr: pytest.TerminalReporter, - state: Literal["remove", "normal", "error", "indeterminate", "paused"], - progress: int | None, - expected: str, - ) -> None: - """Test that progress sequences are emitted correctly.""" - plugin = TerminalProgressPlugin(mock_tr) - plugin._emit_progress(state, progress) - assert expected in mock_file.getvalue() - - def test_session_lifecycle( - self, mock_file: StringIO, mock_tr: pytest.TerminalReporter - ) -> None: - """Test progress updates during session lifecycle.""" - plugin = TerminalProgressPlugin(mock_tr) - - session = mock.create_autospec(pytest.Session) - session.testscollected = 3 - - # Session start - should emit indeterminate progress. - plugin.pytest_sessionstart(session) - assert "\x1b]9;4;3;\x1b\\" in mock_file.getvalue() - mock_file.truncate(0) - mock_file.seek(0) - - # Collection finish - should emit 0% progress. - plugin.pytest_collection_finish() - assert "\x1b]9;4;1;0\x1b\\" in mock_file.getvalue() - mock_file.truncate(0) - mock_file.seek(0) - - # First test - 33% progress. - report1 = pytest.TestReport( - nodeid="test_1", - location=("test.py", 0, "test_1"), - when="call", - outcome="passed", - keywords={}, - longrepr=None, - ) - mock_tr.reported_progress = 1 # type: ignore[misc] - plugin.pytest_runtest_logreport(report1) - assert "\x1b]9;4;1;33\x1b\\" in mock_file.getvalue() - mock_file.truncate(0) - mock_file.seek(0) - - # Second test with failure - 66% in error state. - report2 = pytest.TestReport( - nodeid="test_2", - location=("test.py", 1, "test_2"), - when="call", - outcome="failed", - keywords={}, - longrepr=None, - ) - mock_tr.reported_progress = 2 # type: ignore[misc] - plugin.pytest_runtest_logreport(report2) - assert "\x1b]9;4;2;66\x1b\\" in mock_file.getvalue() - mock_file.truncate(0) - mock_file.seek(0) - - # Session finish - should remove progress. - plugin.pytest_sessionfinish() - assert "\x1b]9;4;0;\x1b\\" in mock_file.getvalue()