| 1 |
.. _deprecations: |
| 2 |
|
| 3 |
Deprecations and Removals |
| 4 |
========================= |
| 5 |
|
| 6 |
This page lists all pytest features that are currently deprecated or have been removed in past major releases. |
| 7 |
The objective is to give users a clear rationale why a certain feature has been removed, and what alternatives |
| 8 |
should be used instead. |
| 9 |
|
| 10 |
|
| 11 |
Deprecated Features |
| 12 |
------------------- |
| 13 |
|
| 14 |
Below is a complete list of all pytest features which are considered deprecated. Using those features will issue |
| 15 |
:class:`~pytest.PytestWarning` or subclasses, which can be filtered using :ref:`standard warning filters <warnings>`. |
| 16 |
|
| 17 |
|
| 18 |
.. _fixture-nodeid-deprecated: |
| 19 |
|
| 20 |
Passing ``baseid``/``nodeid`` strings to fixture registration APIs |
| 21 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 22 |
|
| 23 |
.. deprecated:: 9.1 |
| 24 |
|
| 25 |
Passing ``baseid`` to :class:`~pytest.FixtureDef` or ``nodeid`` strings to |
| 26 |
``FixtureManager._register_fixture`` and ``FixtureManager.parsefactories`` |
| 27 |
is deprecated. These are internal pytest APIs that are used by some plugins. |
| 28 |
|
| 29 |
Use the ``node`` parameter instead for fixture scoping. This enables more robust |
| 30 |
node-based matching instead of fragile string prefix matching. |
| 31 |
|
| 32 |
.. code-block:: python |
| 33 |
|
| 34 |
# Deprecated |
| 35 |
fixture_manager.parsefactories(plugin_obj, nodeid="tests/sub") |
| 36 |
fixture_manager._register_fixture(name="fix", func=func, nodeid="tests/sub") |
| 37 |
|
| 38 |
# Use instead |
| 39 |
fixture_manager.parsefactories(holder=plugin_obj, node=directory_node) |
| 40 |
pytest.register_fixture(name="fix", func=func, node=directory_node) |
| 41 |
|
| 42 |
The equivalent of passing ``nodeid=None`` (global visibility) is ``node=session``. |
| 43 |
|
| 44 |
In pytest 10, the ``baseid`` and ``nodeid`` string parameters will be removed. |
| 45 |
|
| 46 |
|
| 47 |
.. _fixturedef-has-location-deprecated: |
| 48 |
|
| 49 |
``FixtureDef.has_location`` |
| 50 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 51 |
|
| 52 |
.. deprecated:: 9.1 |
| 53 |
|
| 54 |
The private ``FixtureDef.has_location`` attribute is deprecated and will be removed in pytest 10. |
| 55 |
|
| 56 |
It indicated whether a fixture was found from a node or a conftest in the collection tree (as opposed to a non-conftest plugin). |
| 57 |
It was used to determine the override order of fixtures, pushing fixtures with "no location" to the front of the override chain (such that they are chosen last). |
| 58 |
The override order is now determined by the visibility of the fixtures in the collection tree, making this distinction obsolete. |
| 59 |
|
| 60 |
|
| 61 |
.. _console-main: |
| 62 |
|
| 63 |
``pytest.console_main()`` |
| 64 |
~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 65 |
|
| 66 |
.. deprecated:: 9.1 |
| 67 |
|
| 68 |
:func:`pytest.console_main` is deprecated and will be removed in pytest 10. |
| 69 |
|
| 70 |
This function is the CLI entry point used internally by the ``pytest`` console script |
| 71 |
and ``python -m pytest``. It was never intended for programmatic use, and exposing it |
| 72 |
in the public API led to confusion with :func:`pytest.main`, which is the correct way |
| 73 |
to invoke pytest from Python code. |
| 74 |
|
| 75 |
If you are calling ``pytest.console_main()`` in your code, replace it with :func:`pytest.main`: |
| 76 |
|
| 77 |
.. code-block:: python |
| 78 |
|
| 79 |
# Deprecated |
| 80 |
pytest.console_main() |
| 81 |
|
| 82 |
# Use this instead |
| 83 |
exit_code = pytest.main() |
| 84 |
|
| 85 |
|
| 86 |
.. _pastebin-deprecated: |
| 87 |
|
| 88 |
The ``--pastebin`` option |
| 89 |
~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 90 |
|
| 91 |
.. deprecated:: 9.1 |
| 92 |
|
| 93 |
The :option:`--pastebin` option has been deprecated due to being very niche, being the only feature in core pytest relying on an external service and having low usage. |
| 94 |
|
| 95 |
The plugin which implements ``--pastebin`` has been extracted to a separate package, :pypi:`pytest-pastebin`. |
| 96 |
Please install ``pytest-pastebin`` if you want to keep using ``--pastebin``. |
| 97 |
|
| 98 |
|
| 99 |
.. _dynamic-fixture-request-during-teardown: |
| 100 |
|
| 101 |
``request.getfixturevalue()`` during fixture teardown |
| 102 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 103 |
|
| 104 |
.. deprecated:: 9.1 |
| 105 |
|
| 106 |
Calling :meth:`request.getfixturevalue() <pytest.FixtureRequest.getfixturevalue>` |
| 107 |
during teardown to request a fixture that was not already requested is deprecated. |
| 108 |
|
| 109 |
This pattern is brittle because teardown runs after pytest has started unwinding active scopes. |
| 110 |
Depending on the requested fixture's scope and the current teardown order, the lookup may appear |
| 111 |
to work, or it may fail. |
| 112 |
|
| 113 |
In pytest 10, first-time fixture requests made during teardown will become an error. |
| 114 |
If teardown logic needs another fixture, request it before teardown begins, either by |
| 115 |
declaring it in the fixture signature or by calling ``request.getfixturevalue()`` before |
| 116 |
the fixture yields. |
| 117 |
|
| 118 |
Fixtures that were already requested before teardown started are unaffected and may still |
| 119 |
be retrieved while they remain active, though this is discouraged. |
| 120 |
|
| 121 |
|
| 122 |
.. _config-inicfg: |
| 123 |
|
| 124 |
``config.inicfg`` |
| 125 |
~~~~~~~~~~~~~~~~~ |
| 126 |
|
| 127 |
.. deprecated:: 9.0 |
| 128 |
|
| 129 |
The private ``config.inicfg`` attribute is deprecated. |
| 130 |
Use :meth:`config.getini() <pytest.Config.getini>` to access configuration values instead. |
| 131 |
|
| 132 |
``config.inicfg`` was never documented and it should have had a ``_`` prefix from the start. |
| 133 |
Pytest performs caching, transformation and aliasing on configuration options which make direct access to the raw ``config.inicfg`` untenable. |
| 134 |
|
| 135 |
**Reading configuration values:** |
| 136 |
|
| 137 |
Instead of accessing ``config.inicfg`` directly, use :meth:`config.getini() <pytest.Config.getini>`: |
| 138 |
|
| 139 |
.. code-block:: python |
| 140 |
|
| 141 |
# Deprecated |
| 142 |
value = config.inicfg["some_option"] |
| 143 |
|
| 144 |
# Use this instead |
| 145 |
value = config.getini("some_option") |
| 146 |
|
| 147 |
**Setting configuration values:** |
| 148 |
|
| 149 |
Setting or deleting configuration values after initialization is not supported. |
| 150 |
If you need to override configuration values, use the ``-o`` command line option: |
| 151 |
|
| 152 |
.. code-block:: bash |
| 153 |
|
| 154 |
pytest -o some_option=value |
| 155 |
|
| 156 |
or set them in your configuration file instead. |
| 157 |
|
| 158 |
|
| 159 |
.. _parametrize-iterators: |
| 160 |
|
| 161 |
Non-Collection iterables in ``@pytest.mark.parametrize`` |
| 162 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 163 |
|
| 164 |
.. deprecated:: 9.1 |
| 165 |
|
| 166 |
Using non-:class:`~collections.abc.Collection` iterables (such as generators, iterators, or custom iterable objects) |
| 167 |
for the ``argvalues`` parameter in :ref:`@pytest.mark.parametrize <pytest.mark.parametrize ref>` |
| 168 |
and :meth:`metafunc.parametrize <pytest.Metafunc.parametrize>` is deprecated. |
| 169 |
|
| 170 |
These iterables get exhausted after the first iteration, leading to tests getting unexpectedly skipped in cases such as: |
| 171 |
|
| 172 |
* Running :func:`pytest.main()` multiple times in the same process |
| 173 |
* Using class-level parametrize decorators where the same mark is applied to multiple test methods |
| 174 |
* Collecting tests multiple times |
| 175 |
|
| 176 |
Example of problematic code: |
| 177 |
|
| 178 |
.. code-block:: python |
| 179 |
|
| 180 |
import pytest |
| 181 |
|
| 182 |
|
| 183 |
def data_generator(): |
| 184 |
yield 1 |
| 185 |
yield 2 |
| 186 |
|
| 187 |
|
| 188 |
@pytest.mark.parametrize("n", data_generator()) |
| 189 |
class Test: |
| 190 |
def test_1(self, n): |
| 191 |
pass |
| 192 |
|
| 193 |
# test_2 will be skipped because data_generator() is exhausted. |
| 194 |
def test_2(self, n): |
| 195 |
pass |
| 196 |
|
| 197 |
You can fix it by converting generators and iterators to lists or tuples: |
| 198 |
|
| 199 |
.. code-block:: python |
| 200 |
|
| 201 |
import pytest |
| 202 |
|
| 203 |
|
| 204 |
def data_generator(): |
| 205 |
yield 1 |
| 206 |
yield 2 |
| 207 |
|
| 208 |
|
| 209 |
@pytest.mark.parametrize("n", list(data_generator())) |
| 210 |
class Test: |
| 211 |
def test_1(self, n): |
| 212 |
pass |
| 213 |
|
| 214 |
def test_2(self, n): |
| 215 |
pass |
| 216 |
|
| 217 |
Note that :class:`range` objects are ``Collection`` and are not affected by this deprecation. |
| 218 |
|
| 219 |
|
| 220 |
.. _class-scoped-fixture-as-instance-method: |
| 221 |
|
| 222 |
Class-scoped fixture as instance method |
| 223 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 224 |
|
| 225 |
.. deprecated:: 9.1 |
| 226 |
|
| 227 |
Defining a class-scoped fixture as an instance method (without ``@classmethod``) is deprecated |
| 228 |
and will be removed in pytest 10.0. |
| 229 |
|
| 230 |
When a class-scoped fixture is defined as an instance method, any attributes set on ``self`` |
| 231 |
will not be visible to test methods. This happens because pytest creates a new instance of the |
| 232 |
test class for each test method, while the fixture runs only once per class on a different instance. |
| 233 |
|
| 234 |
**Before** (deprecated): |
| 235 |
|
| 236 |
.. code-block:: python |
| 237 |
|
| 238 |
class TestExample: |
| 239 |
@pytest.fixture(scope="class") |
| 240 |
def setup_data(self): |
| 241 |
self.data = [1, 2, 3] # This won't be visible to tests! |
| 242 |
|
| 243 |
def test_something(self, setup_data): |
| 244 |
assert self.data == [ |
| 245 |
1, |
| 246 |
2, |
| 247 |
3, |
| 248 |
] # AttributeError: 'TestExample' object has no attribute 'data' |
| 249 |
|
| 250 |
**After** (recommended): |
| 251 |
|
| 252 |
.. code-block:: python |
| 253 |
|
| 254 |
class TestExample: |
| 255 |
@pytest.fixture(scope="class") |
| 256 |
@classmethod |
| 257 |
def setup_data(cls): |
| 258 |
cls.data = [1, 2, 3] |
| 259 |
|
| 260 |
def test_something(self, setup_data): |
| 261 |
assert self.data == [1, 2, 3] # Works correctly |
| 262 |
|
| 263 |
Using ``@classmethod`` ensures attributes are set on the class itself, making them accessible |
| 264 |
to all test methods. |
| 265 |
|
| 266 |
|
| 267 |
.. _monkeypatch-fixup-namespace-packages: |
| 268 |
|
| 269 |
``monkeypatch.syspath_prepend`` with legacy namespace packages |
| 270 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 271 |
|
| 272 |
.. deprecated:: 9.0 |
| 273 |
|
| 274 |
When using :meth:`monkeypatch.syspath_prepend() <pytest.MonkeyPatch.syspath_prepend>`, |
| 275 |
pytest automatically calls ``pkg_resources.fixup_namespace_packages()`` if ``pkg_resources`` is imported. |
| 276 |
This is only needed for legacy namespace packages that use ``pkg_resources.declare_namespace()``. |
| 277 |
|
| 278 |
Legacy namespace packages are deprecated in favor of native namespace packages (:pep:`420`). |
| 279 |
If you are using ``pkg_resources.declare_namespace()`` in your ``__init__.py`` files, |
| 280 |
you should migrate to native namespace packages by removing the ``__init__.py`` files from your namespace packages. |
| 281 |
|
| 282 |
This deprecation warning will only be issued when: |
| 283 |
|
| 284 |
1. ``pkg_resources`` is imported, and |
| 285 |
2. The specific path being prepended contains a declared namespace package (via ``pkg_resources.declare_namespace()``) |
| 286 |
|
| 287 |
To fix this warning, convert your legacy namespace packages to native namespace packages: |
| 288 |
|
| 289 |
**Legacy namespace package** (deprecated): |
| 290 |
|
| 291 |
.. code-block:: python |
| 292 |
|
| 293 |
# mypkg/__init__.py |
| 294 |
__import__("pkg_resources").declare_namespace(__name__) |
| 295 |
|
| 296 |
**Native namespace package** (recommended): |
| 297 |
|
| 298 |
Simply remove the ``__init__.py`` file entirely. |
| 299 |
Python 3.3+ natively supports namespace packages without ``__init__.py``. |
| 300 |
|
| 301 |
|
| 302 |
.. _hook-markers: |
| 303 |
|
| 304 |
Configuring hook specs/impls using markers |
| 305 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 306 |
|
| 307 |
.. deprecated:: 7.2 |
| 308 |
|
| 309 |
Before pluggy, pytest's plugin library, was its own package and had a clear API, |
| 310 |
pytest just used ``pytest.mark`` to configure hooks. |
| 311 |
|
| 312 |
The :py:func:`pytest.hookimpl` and :py:func:`pytest.hookspec` decorators |
| 313 |
have been available since years and should be used instead. |
| 314 |
|
| 315 |
.. code-block:: python |
| 316 |
|
| 317 |
@pytest.mark.tryfirst |
| 318 |
def pytest_runtest_call(): ... |
| 319 |
|
| 320 |
|
| 321 |
# or |
| 322 |
def pytest_runtest_call(): ... |
| 323 |
|
| 324 |
|
| 325 |
pytest_runtest_call.tryfirst = True |
| 326 |
|
| 327 |
should be changed to: |
| 328 |
|
| 329 |
.. code-block:: python |
| 330 |
|
| 331 |
@pytest.hookimpl(tryfirst=True) |
| 332 |
def pytest_runtest_call(): ... |
| 333 |
|
| 334 |
Changed ``hookimpl`` attributes: |
| 335 |
|
| 336 |
* ``tryfirst`` |
| 337 |
* ``trylast`` |
| 338 |
* ``optionalhook`` |
| 339 |
* ``hookwrapper`` |
| 340 |
|
| 341 |
Changed ``hookwrapper`` attributes: |
| 342 |
|
| 343 |
* ``firstresult`` |
| 344 |
* ``historic`` |
| 345 |
|
| 346 |
|
| 347 |
Directly constructing internal classes |
| 348 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 349 |
|
| 350 |
.. deprecated:: 7.0 |
| 351 |
|
| 352 |
Directly constructing the following classes is now deprecated: |
| 353 |
|
| 354 |
- ``_pytest.mark.structures.Mark`` |
| 355 |
- ``_pytest.mark.structures.MarkDecorator`` |
| 356 |
- ``_pytest.mark.structures.MarkGenerator`` |
| 357 |
- ``_pytest.python.Metafunc`` |
| 358 |
- ``_pytest.runner.CallInfo`` |
| 359 |
- ``_pytest._code.ExceptionInfo`` |
| 360 |
- ``_pytest.config.argparsing.Parser`` |
| 361 |
- ``_pytest.config.argparsing.OptionGroup`` |
| 362 |
- ``_pytest.pytester.HookRecorder`` |
| 363 |
|
| 364 |
These constructors have always been considered private, but now issue a deprecation warning, which may become a hard error in pytest 8. |
| 365 |
|
| 366 |
.. _diamond-inheritance-deprecated: |
| 367 |
|
| 368 |
Diamond inheritance between :class:`pytest.Collector` and :class:`pytest.Item` |
| 369 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 370 |
|
| 371 |
.. deprecated:: 7.0 |
| 372 |
|
| 373 |
Defining a custom pytest node type which is both an :class:`~pytest.Item` and a :class:`~pytest.Collector` (e.g. :class:`~pytest.File`) now issues a warning. |
| 374 |
It was never sanely supported and triggers hard to debug errors. |
| 375 |
|
| 376 |
Some plugins providing linting/code analysis have been using this as a hack. |
| 377 |
Instead, a separate collector node should be used, which collects the item. See |
| 378 |
:ref:`non-python tests` for an example, as well as an `example pr fixing inheritance`_. |
| 379 |
|
| 380 |
.. _example pr fixing inheritance: https://github.com/asmeurer/pytest-flakes/pull/40/files |
| 381 |
|
| 382 |
|
| 383 |
.. _uncooperative-constructors-deprecated: |
| 384 |
|
| 385 |
Constructors of custom :class:`~_pytest.nodes.Node` subclasses should take ``**kwargs`` |
| 386 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 387 |
|
| 388 |
.. deprecated:: 7.0 |
| 389 |
|
| 390 |
If custom subclasses of nodes like :class:`pytest.Item` override the |
| 391 |
``__init__`` method, they should take ``**kwargs``. Thus, |
| 392 |
|
| 393 |
.. code-block:: python |
| 394 |
|
| 395 |
class CustomItem(pytest.Item): |
| 396 |
def __init__(self, name, parent, additional_arg): |
| 397 |
super().__init__(name, parent) |
| 398 |
self.additional_arg = additional_arg |
| 399 |
|
| 400 |
should be turned into: |
| 401 |
|
| 402 |
.. code-block:: python |
| 403 |
|
| 404 |
class CustomItem(pytest.Item): |
| 405 |
def __init__(self, *, additional_arg, **kwargs): |
| 406 |
super().__init__(**kwargs) |
| 407 |
self.additional_arg = additional_arg |
| 408 |
|
| 409 |
to avoid hard-coding the arguments pytest can pass to the superclass. |
| 410 |
See :ref:`non-python tests` for a full example. |
| 411 |
|
| 412 |
For cases without conflicts, no deprecation warning is emitted. For cases with |
| 413 |
conflicts (such as :class:`pytest.File` now taking ``path`` instead of |
| 414 |
``fspath``, as :ref:`outlined above <node-ctor-fspath-deprecation>`), a |
| 415 |
deprecation warning is now raised. |
| 416 |
|
| 417 |
|
| 418 |
.. _yield-fixture-deprecated: |
| 419 |
|
| 420 |
The ``yield_fixture`` function/decorator |
| 421 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 422 |
|
| 423 |
.. deprecated:: 6.2 |
| 424 |
|
| 425 |
``pytest.yield_fixture`` is a deprecated alias for :func:`pytest.fixture`. |
| 426 |
|
| 427 |
It has been so for a very long time, so it can be searched/replaced safely. |
| 428 |
|
| 429 |
|
| 430 |
Removed Features and Breaking Changes |
| 431 |
------------------------------------- |
| 432 |
|
| 433 |
As stated in our :ref:`backwards-compatibility` policy, deprecated features are removed only in major releases after |
| 434 |
an appropriate period of deprecation has passed. |
| 435 |
|
| 436 |
Some breaking changes which could not be deprecated are also listed. |
| 437 |
|
| 438 |
|
| 439 |
.. _import-or-skip-import-error: |
| 440 |
|
| 441 |
``pytest.importorskip`` default behavior regarding :class:`ImportError` |
| 442 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 443 |
|
| 444 |
.. deprecated:: 8.2 |
| 445 |
.. versionremoved:: 9.1 |
| 446 |
|
| 447 |
Traditionally :func:`pytest.importorskip` captured :class:`ImportError`, with the original intent being to skip |
| 448 |
tests where a dependent module is not installed, for example testing with different dependencies. |
| 449 |
|
| 450 |
However, some packages might be installed in the system but not importable due to some other issue, for example |
| 451 |
a compilation error or a broken installation. In those cases, :func:`pytest.importorskip` would still silently skip |
| 452 |
the test, but more often than not users would rather see the unexpected error so the underlying issue can be fixed. |
| 453 |
|
| 454 |
In ``8.2``, the ``exc_type`` parameter was added, giving users the ability to pass |
| 455 |
:class:`ModuleNotFoundError` to skip tests only if the module cannot really be found, and not because of some other |
| 456 |
error. |
| 457 |
|
| 458 |
As of ``9.1``, :func:`pytest.importorskip` only captures :class:`ModuleNotFoundError` by default. |
| 459 |
If you want to preserve the previous behavior and skip on other :class:`ImportError` exceptions during import, |
| 460 |
pass ``exc_type=ImportError`` explicitly. |
| 461 |
|
| 462 |
|
| 463 |
.. _node-ctor-fspath-deprecation: |
| 464 |
|
| 465 |
``fspath`` argument for Node constructors replaced with ``pathlib.Path`` |
| 466 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 467 |
|
| 468 |
.. deprecated:: 7.0 |
| 469 |
.. versionremoved:: 9.1 |
| 470 |
|
| 471 |
In order to support the transition from ``py.path.local`` to :mod:`pathlib`, |
| 472 |
the ``fspath`` argument to :class:`~_pytest.nodes.Node` constructors like |
| 473 |
:func:`pytest.Function.from_parent()` and :func:`pytest.Class.from_parent()` |
| 474 |
is now deprecated. |
| 475 |
|
| 476 |
Plugins which construct nodes should pass the ``path`` argument, of type |
| 477 |
:class:`pathlib.Path`, instead of the ``fspath`` argument. |
| 478 |
|
| 479 |
Plugins which implement custom items and collectors are encouraged to replace |
| 480 |
``fspath`` parameters (``py.path.local``) with ``path`` parameters |
| 481 |
(``pathlib.Path``), and drop any other usage of the ``py`` library if possible. |
| 482 |
|
| 483 |
If possible, plugins with custom items should use :ref:`cooperative |
| 484 |
constructors <uncooperative-constructors-deprecated>` to avoid hardcoding |
| 485 |
arguments they only pass on to the superclass. |
| 486 |
|
| 487 |
.. note:: |
| 488 |
The name of the :class:`~_pytest.nodes.Node` arguments and attributes (the |
| 489 |
new attribute being ``path``) is **the opposite** of the situation for |
| 490 |
hooks, :ref:`outlined below <legacy-path-hooks-deprecated>` (the old |
| 491 |
argument being ``path``). |
| 492 |
|
| 493 |
This is an unfortunate artifact due to historical reasons, which should be |
| 494 |
resolved in future versions as we slowly get rid of the :pypi:`py` |
| 495 |
dependency (see :issue:`9283` for a longer discussion). |
| 496 |
|
| 497 |
Due to the ongoing migration of methods like :meth:`~pytest.Item.reportinfo` |
| 498 |
which still is expected to return a ``py.path.local`` object, nodes still have |
| 499 |
both ``fspath`` (``py.path.local``) and ``path`` (``pathlib.Path``) attributes, |
| 500 |
no matter what argument was used in the constructor. We expect to deprecate the |
| 501 |
``fspath`` attribute in a future release. |
| 502 |
|
| 503 |
|
| 504 |
.. _sync-test-async-fixture: |
| 505 |
|
| 506 |
sync test depending on async fixture |
| 507 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 508 |
|
| 509 |
.. deprecated:: 8.4 |
| 510 |
.. versionremoved:: 9.0 |
| 511 |
|
| 512 |
Pytest has for a long time given an error when encountering an asynchronous test function, prompting the user to install |
| 513 |
a plugin that can handle it. It has not given any errors if you have an asynchronous fixture that's depended on by a |
| 514 |
synchronous test. If the fixture was an async function you did get an "unawaited coroutine" warning, but for async yield fixtures you didn't even get that. |
| 515 |
This is a problem even if you do have a plugin installed for handling async tests, as they may require |
| 516 |
special decorators for async fixtures to be handled, and some may not robustly handle if a user accidentally requests an |
| 517 |
async fixture from their sync tests. Fixture values being cached can make this even more unintuitive, where everything will |
| 518 |
"work" if the fixture is first requested by an async test, and then requested by a synchronous test. |
| 519 |
|
| 520 |
Unfortunately there is no 100% reliable method of identifying when a user has made a mistake, versus when they expect an |
| 521 |
unawaited object from their fixture that they will handle on their own. To suppress this warning |
| 522 |
when you in fact did intend to handle this you can wrap your async fixture in a synchronous fixture: |
| 523 |
|
| 524 |
.. code-block:: python |
| 525 |
|
| 526 |
import asyncio |
| 527 |
import pytest |
| 528 |
|
| 529 |
|
| 530 |
@pytest.fixture |
| 531 |
async def unawaited_fixture(): |
| 532 |
return 1 |
| 533 |
|
| 534 |
|
| 535 |
def test_foo(unawaited_fixture): |
| 536 |
assert 1 == asyncio.run(unawaited_fixture) |
| 537 |
|
| 538 |
should be changed to |
| 539 |
|
| 540 |
|
| 541 |
.. code-block:: python |
| 542 |
|
| 543 |
import asyncio |
| 544 |
import pytest |
| 545 |
|
| 546 |
|
| 547 |
@pytest.fixture |
| 548 |
def unawaited_fixture(): |
| 549 |
async def inner_fixture(): |
| 550 |
return 1 |
| 551 |
|
| 552 |
return inner_fixture() |
| 553 |
|
| 554 |
|
| 555 |
def test_foo(unawaited_fixture): |
| 556 |
assert 1 == asyncio.run(unawaited_fixture) |
| 557 |
|
| 558 |
|
| 559 |
You can also make use of `pytest_fixture_setup` to handle the coroutine/asyncgen before pytest sees it - this is the way current async pytest plugins handle it. |
| 560 |
|
| 561 |
If a user has an async fixture with ``autouse=True`` in their ``conftest.py``, or in a file |
| 562 |
containing both synchronous tests and the fixture, they will receive this warning. |
| 563 |
Unless you're using a plugin that specifically handles async fixtures |
| 564 |
with synchronous tests, we strongly recommend against this practice. |
| 565 |
It can lead to unpredictable behavior (with larger scopes, it may appear to "work" if an async |
| 566 |
test is the first to request the fixture, due to value caching) and will generate |
| 567 |
unawaited-coroutine runtime warnings (but only for non-yield fixtures). |
| 568 |
Additionally, it creates ambiguity for other developers about whether the fixture is intended to perform |
| 569 |
setup for synchronous tests. |
| 570 |
|
| 571 |
The `anyio pytest plugin <https://anyio.readthedocs.io/en/stable/testing.html>`_ supports |
| 572 |
synchronous tests with async fixtures, though certain limitations apply. |
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
Applying a mark to a fixture function |
| 577 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 578 |
|
| 579 |
.. deprecated:: 7.4 |
| 580 |
.. versionremoved:: 9.0 |
| 581 |
|
| 582 |
Applying a mark to a fixture function never had any effect, but it is a common user error. |
| 583 |
|
| 584 |
.. code-block:: python |
| 585 |
|
| 586 |
@pytest.mark.usefixtures("clean_database") |
| 587 |
@pytest.fixture |
| 588 |
def user() -> User: ... |
| 589 |
|
| 590 |
Users expected in this case that the ``usefixtures`` mark would have its intended effect of using the ``clean_database`` fixture when ``user`` was invoked, when in fact it has no effect at all. |
| 591 |
|
| 592 |
Now pytest will issue a warning when it encounters this problem, and will raise an error in the future versions. |
| 593 |
|
| 594 |
.. _legacy-path-hooks-deprecated: |
| 595 |
|
| 596 |
``py.path.local`` arguments for hooks replaced with ``pathlib.Path`` |
| 597 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 598 |
|
| 599 |
.. deprecated:: 7.0 |
| 600 |
.. versionremoved:: 9.0 |
| 601 |
|
| 602 |
In order to support the transition from ``py.path.local`` to :mod:`pathlib`, the following hooks now receive additional arguments: |
| 603 |
|
| 604 |
* :hook:`pytest_ignore_collect(collection_path: pathlib.Path) <pytest_ignore_collect>` as equivalent to ``path`` |
| 605 |
* :hook:`pytest_collect_file(file_path: pathlib.Path) <pytest_collect_file>` as equivalent to ``path`` |
| 606 |
* :hook:`pytest_pycollect_makemodule(module_path: pathlib.Path) <pytest_pycollect_makemodule>` as equivalent to ``path`` |
| 607 |
* :hook:`pytest_report_header(start_path: pathlib.Path) <pytest_report_header>` as equivalent to ``startdir`` |
| 608 |
* :hook:`pytest_report_collectionfinish(start_path: pathlib.Path) <pytest_report_collectionfinish>` as equivalent to ``startdir`` |
| 609 |
|
| 610 |
The accompanying ``py.path.local`` based paths have been deprecated: plugins which manually invoke those hooks should only pass the new ``pathlib.Path`` arguments, and users should change their hook implementations to use the new ``pathlib.Path`` arguments. |
| 611 |
|
| 612 |
.. note:: |
| 613 |
The name of the :class:`~_pytest.nodes.Node` arguments and attributes, |
| 614 |
:ref:`outlined above <node-ctor-fspath-deprecation>` (the new attribute |
| 615 |
being ``path``) is **the opposite** of the situation for hooks (the old |
| 616 |
argument being ``path``). |
| 617 |
|
| 618 |
This is an unfortunate artifact due to historical reasons, which should be |
| 619 |
resolved in future versions as we slowly get rid of the :pypi:`py` |
| 620 |
dependency (see :issue:`9283` for a longer discussion). |
| 621 |
|
| 622 |
.. _yield tests deprecated: |
| 623 |
|
| 624 |
``yield`` tests |
| 625 |
~~~~~~~~~~~~~~~ |
| 626 |
|
| 627 |
.. versionremoved:: 4.0 |
| 628 |
|
| 629 |
``yield`` tests ``xfail``. |
| 630 |
|
| 631 |
.. versionremoved:: 8.4 |
| 632 |
|
| 633 |
``yield`` tests raise a collection error. |
| 634 |
|
| 635 |
pytest no longer supports ``yield``-style tests, where a test function actually ``yield`` functions and values |
| 636 |
that are then turned into proper test methods. Example: |
| 637 |
|
| 638 |
.. code-block:: python |
| 639 |
|
| 640 |
def check(x, y): |
| 641 |
assert x**x == y |
| 642 |
|
| 643 |
|
| 644 |
def test_squared(): |
| 645 |
yield check, 2, 4 |
| 646 |
yield check, 3, 9 |
| 647 |
|
| 648 |
This would result in two actual test functions being generated. |
| 649 |
|
| 650 |
This form of test function doesn't support fixtures properly, and users should switch to ``pytest.mark.parametrize``: |
| 651 |
|
| 652 |
.. code-block:: python |
| 653 |
|
| 654 |
@pytest.mark.parametrize("x, y", [(2, 4), (3, 9)]) |
| 655 |
def test_squared(x, y): |
| 656 |
assert x**x == y |
| 657 |
|
| 658 |
.. _nose-deprecation: |
| 659 |
|
| 660 |
Support for tests written for nose |
| 661 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 662 |
|
| 663 |
.. deprecated:: 7.2 |
| 664 |
.. versionremoved:: 8.0 |
| 665 |
|
| 666 |
Support for running tests written for `nose <https://nose.readthedocs.io/en/latest/>`__ is now deprecated. |
| 667 |
|
| 668 |
``nose`` has been in maintenance mode-only for years, and maintaining the plugin is not trivial as it spills |
| 669 |
over the code base (see :issue:`9886` for more details). |
| 670 |
|
| 671 |
setup/teardown |
| 672 |
^^^^^^^^^^^^^^ |
| 673 |
|
| 674 |
One thing that might catch users by surprise is that plain ``setup`` and ``teardown`` methods are not pytest native, |
| 675 |
they are in fact part of the ``nose`` support. |
| 676 |
|
| 677 |
|
| 678 |
.. code-block:: python |
| 679 |
|
| 680 |
class Test: |
| 681 |
def setup(self): |
| 682 |
self.resource = make_resource() |
| 683 |
|
| 684 |
def teardown(self): |
| 685 |
self.resource.close() |
| 686 |
|
| 687 |
def test_foo(self): ... |
| 688 |
|
| 689 |
def test_bar(self): ... |
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
Native pytest support uses ``setup_method`` and ``teardown_method`` (see :ref:`xunit-method-setup`), so the above should be changed to: |
| 694 |
|
| 695 |
.. code-block:: python |
| 696 |
|
| 697 |
class Test: |
| 698 |
def setup_method(self): |
| 699 |
self.resource = make_resource() |
| 700 |
|
| 701 |
def teardown_method(self): |
| 702 |
self.resource.close() |
| 703 |
|
| 704 |
def test_foo(self): ... |
| 705 |
|
| 706 |
def test_bar(self): ... |
| 707 |
|
| 708 |
|
| 709 |
This is easy to do in an entire code base by doing a simple find/replace. |
| 710 |
|
| 711 |
@with_setup |
| 712 |
^^^^^^^^^^^ |
| 713 |
|
| 714 |
Code using `@with_setup <with-setup-nose>`_ such as this: |
| 715 |
|
| 716 |
.. code-block:: python |
| 717 |
|
| 718 |
from nose.tools import with_setup |
| 719 |
|
| 720 |
|
| 721 |
def setup_some_resource(): ... |
| 722 |
|
| 723 |
|
| 724 |
def teardown_some_resource(): ... |
| 725 |
|
| 726 |
|
| 727 |
@with_setup(setup_some_resource, teardown_some_resource) |
| 728 |
def test_foo(): ... |
| 729 |
|
| 730 |
Will also need to be ported to a supported pytest style. One way to do it is using a fixture: |
| 731 |
|
| 732 |
.. code-block:: python |
| 733 |
|
| 734 |
import pytest |
| 735 |
|
| 736 |
|
| 737 |
def setup_some_resource(): ... |
| 738 |
|
| 739 |
|
| 740 |
def teardown_some_resource(): ... |
| 741 |
|
| 742 |
|
| 743 |
@pytest.fixture |
| 744 |
def some_resource(): |
| 745 |
setup_some_resource() |
| 746 |
yield |
| 747 |
teardown_some_resource() |
| 748 |
|
| 749 |
|
| 750 |
def test_foo(some_resource): ... |
| 751 |
|
| 752 |
|
| 753 |
.. _`with-setup-nose`: https://nose.readthedocs.io/en/latest/testing_tools.html?highlight=with_setup#nose.tools.with_setup |
| 754 |
|
| 755 |
|
| 756 |
The ``compat_co_firstlineno`` attribute |
| 757 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 758 |
|
| 759 |
Nose inspects this attribute on function objects to allow overriding the function's inferred line number. |
| 760 |
Pytest no longer respects this attribute. |
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
Passing ``msg=`` to ``pytest.skip``, ``pytest.fail`` or ``pytest.exit`` |
| 765 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 766 |
|
| 767 |
.. deprecated:: 7.0 |
| 768 |
.. versionremoved:: 8.0 |
| 769 |
|
| 770 |
Passing the keyword argument ``msg`` to :func:`pytest.skip`, :func:`pytest.fail` or :func:`pytest.exit` |
| 771 |
is now deprecated and ``reason`` should be used instead. This change is to bring consistency between these |
| 772 |
functions and the ``@pytest.mark.skip`` and ``@pytest.mark.xfail`` markers which already accept a ``reason`` argument. |
| 773 |
|
| 774 |
.. code-block:: python |
| 775 |
|
| 776 |
def test_fail_example(): |
| 777 |
# old |
| 778 |
pytest.fail(msg="foo") |
| 779 |
# new |
| 780 |
pytest.fail(reason="bar") |
| 781 |
|
| 782 |
|
| 783 |
def test_skip_example(): |
| 784 |
# old |
| 785 |
pytest.skip(msg="foo") |
| 786 |
# new |
| 787 |
pytest.skip(reason="bar") |
| 788 |
|
| 789 |
|
| 790 |
def test_exit_example(): |
| 791 |
# old |
| 792 |
pytest.exit(msg="foo") |
| 793 |
# new |
| 794 |
pytest.exit(reason="bar") |
| 795 |
|
| 796 |
|
| 797 |
.. _instance-collector-deprecation: |
| 798 |
|
| 799 |
The ``pytest.Instance`` collector |
| 800 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 801 |
|
| 802 |
.. versionremoved:: 7.0 |
| 803 |
|
| 804 |
The ``pytest.Instance`` collector type has been removed. |
| 805 |
|
| 806 |
Previously, Python test methods were collected as :class:`~pytest.Class` -> ``Instance`` -> :class:`~pytest.Function`. |
| 807 |
Now :class:`~pytest.Class` collects the test methods directly. |
| 808 |
|
| 809 |
Most plugins which reference ``Instance`` do so in order to ignore or skip it, |
| 810 |
using a check such as ``if isinstance(node, Instance): return``. |
| 811 |
Such plugins should simply remove consideration of ``Instance`` on pytest>=7. |
| 812 |
However, to keep such uses working, a dummy type has been instanced in ``pytest.Instance`` and ``_pytest.python.Instance``, |
| 813 |
and importing it emits a deprecation warning. This was removed in pytest 8. |
| 814 |
|
| 815 |
|
| 816 |
Using ``pytest.warns(None)`` |
| 817 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 818 |
|
| 819 |
.. deprecated:: 7.0 |
| 820 |
.. versionremoved:: 8.0 |
| 821 |
|
| 822 |
:func:`pytest.warns(None) <pytest.warns>` is now deprecated because it was frequently misused. |
| 823 |
Its correct usage was checking that the code emits at least one warning of any type - like ``pytest.warns()`` |
| 824 |
or ``pytest.warns(Warning)``. |
| 825 |
|
| 826 |
See :ref:`warns use cases` for examples. |
| 827 |
|
| 828 |
|
| 829 |
Backward compatibilities in ``Parser.addoption`` |
| 830 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 831 |
|
| 832 |
.. deprecated:: 2.4 |
| 833 |
.. versionremoved:: 8.0 |
| 834 |
|
| 835 |
Several behaviors of :meth:`Parser.addoption <pytest.Parser.addoption>` are now |
| 836 |
removed in pytest 8 (deprecated since pytest 2.4.0): |
| 837 |
|
| 838 |
- ``parser.addoption(..., help=".. %default ..")`` - use ``%(default)s`` instead. |
| 839 |
- ``parser.addoption(..., type="int/string/float/complex")`` - use ``type=int`` etc. instead. |
| 840 |
|
| 841 |
|
| 842 |
The ``--strict`` command-line option (reintroduced) |
| 843 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 844 |
|
| 845 |
.. deprecated:: 6.2 |
| 846 |
.. versionchanged:: 9.0 |
| 847 |
|
| 848 |
The ``--strict`` command-line option had been deprecated in favor of ``--strict-markers``, which |
| 849 |
better conveys what the option does. |
| 850 |
|
| 851 |
In version 8.1, we accidentally un-deprecated ``--strict``. |
| 852 |
|
| 853 |
In version 9.0, we changed ``--strict`` to make it set the new :confval:`strict` |
| 854 |
configuration option. It now enables all strictness related options (including |
| 855 |
:confval:`strict_markers`). |
| 856 |
|
| 857 |
|
| 858 |
.. _cmdline-preparse-deprecated: |
| 859 |
|
| 860 |
Implementing the ``pytest_cmdline_preparse`` hook |
| 861 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 862 |
|
| 863 |
.. deprecated:: 7.0 |
| 864 |
.. versionremoved:: 8.0 |
| 865 |
|
| 866 |
Implementing the ``pytest_cmdline_preparse`` hook has been officially deprecated. |
| 867 |
Implement the :hook:`pytest_load_initial_conftests` hook instead. |
| 868 |
|
| 869 |
.. code-block:: python |
| 870 |
|
| 871 |
def pytest_cmdline_preparse(config: Config, args: List[str]) -> None: ... |
| 872 |
|
| 873 |
|
| 874 |
# becomes: |
| 875 |
|
| 876 |
|
| 877 |
def pytest_load_initial_conftests( |
| 878 |
early_config: Config, parser: Parser, args: List[str] |
| 879 |
) -> None: ... |
| 880 |
|
| 881 |
|
| 882 |
Collection changes in pytest 8 |
| 883 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 884 |
|
| 885 |
Added a new :class:`pytest.Directory` base collection node, which all collector nodes for filesystem directories are expected to subclass. |
| 886 |
This is analogous to the existing :class:`pytest.File` for file nodes. |
| 887 |
|
| 888 |
Changed :class:`pytest.Package` to be a subclass of :class:`pytest.Directory`. |
| 889 |
A ``Package`` represents a filesystem directory which is a Python package, |
| 890 |
i.e. contains an ``__init__.py`` file. |
| 891 |
|
| 892 |
:class:`pytest.Package` now only collects files in its own directory; previously it collected recursively. |
| 893 |
Sub-directories are collected as sub-collector nodes, thus creating a collection tree which mirrors the filesystem hierarchy. |
| 894 |
|
| 895 |
:attr:`session.name <pytest.Session.name>` is now ``""``; previously it was the rootdir directory name. |
| 896 |
This matches :attr:`session.nodeid <_pytest.nodes.Node.nodeid>` which has always been `""`. |
| 897 |
|
| 898 |
Added a new :class:`pytest.Dir` concrete collection node, a subclass of :class:`pytest.Directory`. |
| 899 |
This node represents a filesystem directory, which is not a :class:`pytest.Package`, |
| 900 |
i.e. does not contain an ``__init__.py`` file. |
| 901 |
Similarly to ``Package``, it only collects the files in its own directory, |
| 902 |
while collecting sub-directories as sub-collector nodes. |
| 903 |
|
| 904 |
Files and directories are now collected in alphabetical order jointly, unless changed by a plugin. |
| 905 |
Previously, files were collected before directories. |
| 906 |
|
| 907 |
The collection tree now contains directories/packages up to the :ref:`rootdir <rootdir>`, |
| 908 |
for initial arguments that are found within the rootdir. |
| 909 |
For files outside the rootdir, only the immediate directory/package is collected -- |
| 910 |
note however that collecting from outside the rootdir is discouraged. |
| 911 |
|
| 912 |
As an example, given the following filesystem tree:: |
| 913 |
|
| 914 |
myroot/ |
| 915 |
pytest.ini |
| 916 |
top/ |
| 917 |
├── aaa |
| 918 |
│ └── test_aaa.py |
| 919 |
├── test_a.py |
| 920 |
├── test_b |
| 921 |
│ ├── __init__.py |
| 922 |
│ └── test_b.py |
| 923 |
├── test_c.py |
| 924 |
└── zzz |
| 925 |
├── __init__.py |
| 926 |
└── test_zzz.py |
| 927 |
|
| 928 |
the collection tree, as shown by `pytest --collect-only top/` but with the otherwise-hidden :class:`~pytest.Session` node added for clarity, |
| 929 |
is now the following:: |
| 930 |
|
| 931 |
<Session> |
| 932 |
<Dir myroot> |
| 933 |
<Dir top> |
| 934 |
<Dir aaa> |
| 935 |
<Module test_aaa.py> |
| 936 |
<Function test_it> |
| 937 |
<Module test_a.py> |
| 938 |
<Function test_it> |
| 939 |
<Package test_b> |
| 940 |
<Module test_b.py> |
| 941 |
<Function test_it> |
| 942 |
<Module test_c.py> |
| 943 |
<Function test_it> |
| 944 |
<Package zzz> |
| 945 |
<Module test_zzz.py> |
| 946 |
<Function test_it> |
| 947 |
|
| 948 |
Previously, it was:: |
| 949 |
|
| 950 |
<Session> |
| 951 |
<Module top/test_a.py> |
| 952 |
<Function test_it> |
| 953 |
<Module top/test_c.py> |
| 954 |
<Function test_it> |
| 955 |
<Module top/aaa/test_aaa.py> |
| 956 |
<Function test_it> |
| 957 |
<Package test_b> |
| 958 |
<Module test_b.py> |
| 959 |
<Function test_it> |
| 960 |
<Package zzz> |
| 961 |
<Module test_zzz.py> |
| 962 |
<Function test_it> |
| 963 |
|
| 964 |
Code/plugins which rely on a specific shape of the collection tree might need to update. |
| 965 |
|
| 966 |
|
| 967 |
:class:`pytest.Package` is no longer a :class:`pytest.Module` or :class:`pytest.File` |
| 968 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 969 |
|
| 970 |
.. versionchanged:: 8.0 |
| 971 |
|
| 972 |
The ``Package`` collector node designates a Python package, that is, a directory with an `__init__.py` file. |
| 973 |
Previously ``Package`` was a subtype of ``pytest.Module`` (which represents a single Python module), |
| 974 |
the module being the `__init__.py` file. |
| 975 |
This has been deemed a design mistake (see :issue:`11137` and :issue:`7777` for details). |
| 976 |
|
| 977 |
The ``path`` property of ``Package`` nodes now points to the package directory instead of the ``__init__.py`` file. |
| 978 |
|
| 979 |
Note that a ``Module`` node for ``__init__.py`` (which is not a ``Package``) may still exist, |
| 980 |
if it is picked up during collection (e.g. if you configured :confval:`python_files` to include ``__init__.py`` files). |
| 981 |
|
| 982 |
|
| 983 |
Collecting ``__init__.py`` files no longer collects package |
| 984 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 985 |
|
| 986 |
.. versionremoved:: 8.0 |
| 987 |
|
| 988 |
Running `pytest pkg/__init__.py` now collects the `pkg/__init__.py` file (module) only. |
| 989 |
Previously, it collected the entire `pkg` package, including other test files in the directory, but excluding tests in the `__init__.py` file itself |
| 990 |
(unless :confval:`python_files` was changed to allow `__init__.py` file). |
| 991 |
|
| 992 |
To collect the entire package, specify just the directory: `pytest pkg`. |
| 993 |
|
| 994 |
|
| 995 |
The ``pytest.collect`` module |
| 996 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 997 |
|
| 998 |
.. deprecated:: 6.0 |
| 999 |
.. versionremoved:: 7.0 |
| 1000 |
|
| 1001 |
The ``pytest.collect`` module is no longer part of the public API, all its names |
| 1002 |
should now be imported from ``pytest`` directly instead. |
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
The ``pytest_warning_captured`` hook |
| 1007 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 1008 |
|
| 1009 |
.. deprecated:: 6.0 |
| 1010 |
.. versionremoved:: 7.0 |
| 1011 |
|
| 1012 |
This hook has an `item` parameter which cannot be serialized by ``pytest-xdist``. |
| 1013 |
|
| 1014 |
Use the ``pytest_warning_recorded`` hook instead, which replaces the ``item`` parameter |
| 1015 |
by a ``nodeid`` parameter. |
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
The ``pytest._fillfuncargs`` function |
| 1020 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 1021 |
|
| 1022 |
.. deprecated:: 6.0 |
| 1023 |
.. versionremoved:: 7.0 |
| 1024 |
|
| 1025 |
This function was kept for backward compatibility with an older plugin. |
| 1026 |
|
| 1027 |
Its functionality is not meant to be used directly, but if you must replace |
| 1028 |
it, use `function._request._fillfixtures()` instead, though note this is not |
| 1029 |
a public API and may break in the future. |
| 1030 |
|
| 1031 |
|
| 1032 |
``--no-print-logs`` command-line option |
| 1033 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 1034 |
|
| 1035 |
.. deprecated:: 5.4 |
| 1036 |
.. versionremoved:: 6.0 |
| 1037 |
|
| 1038 |
|
| 1039 |
The ``--no-print-logs`` option and ``log_print`` ini setting are removed. If |
| 1040 |
you used them, please use ``--show-capture`` instead. |
| 1041 |
|
| 1042 |
A ``--show-capture`` command-line option was added in ``pytest 3.5.0`` which allows to specify how to |
| 1043 |
display captured output when tests fail: ``no``, ``stdout``, ``stderr``, ``log`` or ``all`` (the default). |
| 1044 |
|
| 1045 |
|
| 1046 |
.. _resultlog deprecated: |
| 1047 |
|
| 1048 |
Result log (``--result-log``) |
| 1049 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 1050 |
|
| 1051 |
.. deprecated:: 4.0 |
| 1052 |
.. versionremoved:: 6.0 |
| 1053 |
|
| 1054 |
The ``--result-log`` option produces a stream of test reports which can be |
| 1055 |
analysed at runtime, but it uses a custom format which requires users to implement their own |
| 1056 |
parser. |
| 1057 |
|
| 1058 |
The :pypi:`pytest-reportlog` plugin provides a ``--report-log`` option, a more standard and extensible alternative, producing |
| 1059 |
one JSON object per-line, and should cover the same use cases. Please try it out and provide feedback. |
| 1060 |
|
| 1061 |
The ``pytest-reportlog`` plugin might even be merged into the core |
| 1062 |
at some point, depending on the plans for the plugins and number of users using it. |
| 1063 |
|
| 1064 |
``pytest_collect_directory`` hook |
| 1065 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 1066 |
|
| 1067 |
.. versionremoved:: 6.0 |
| 1068 |
|
| 1069 |
The ``pytest_collect_directory`` hook has not worked properly for years (it was called |
| 1070 |
but the results were ignored). Users may consider using :hook:`pytest_collection_modifyitems` instead. |
| 1071 |
|
| 1072 |
TerminalReporter.writer |
| 1073 |
~~~~~~~~~~~~~~~~~~~~~~~ |
| 1074 |
|
| 1075 |
.. versionremoved:: 6.0 |
| 1076 |
|
| 1077 |
The ``TerminalReporter.writer`` attribute has been deprecated and should no longer be used. This |
| 1078 |
was inadvertently exposed as part of the public API of that plugin and ties it too much |
| 1079 |
with ``py.io.TerminalWriter``. |
| 1080 |
|
| 1081 |
Plugins that used ``TerminalReporter.writer`` directly should instead use ``TerminalReporter`` |
| 1082 |
methods that provide the same functionality. |
| 1083 |
|
| 1084 |
.. _junit-family changed default value: |
| 1085 |
|
| 1086 |
``junit_family`` default value change to "xunit2" |
| 1087 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 1088 |
|
| 1089 |
.. versionchanged:: 6.0 |
| 1090 |
|
| 1091 |
The default value of ``junit_family`` option will change to ``xunit2`` in pytest 6.0, which |
| 1092 |
is an update of the old ``xunit1`` format and is supported by default in modern tools |
| 1093 |
that manipulate this type of file (for example, Jenkins, Azure Pipelines, etc.). |
| 1094 |
|
| 1095 |
Users are recommended to try the new ``xunit2`` format and see if their tooling that consumes the JUnit |
| 1096 |
XML file supports it. |
| 1097 |
|
| 1098 |
To use the new format, update your configuration file: |
| 1099 |
|
| 1100 |
.. tab:: toml |
| 1101 |
|
| 1102 |
.. code-block:: toml |
| 1103 |
|
| 1104 |
[pytest] |
| 1105 |
junit_family = "xunit2" |
| 1106 |
|
| 1107 |
.. tab:: ini |
| 1108 |
|
| 1109 |
.. code-block:: ini |
| 1110 |
|
| 1111 |
[pytest] |
| 1112 |
junit_family = xunit2 |
| 1113 |
|
| 1114 |
If you discover that your tooling does not support the new format, and want to keep using the |
| 1115 |
legacy version, set the option to ``legacy`` instead: |
| 1116 |
|
| 1117 |
.. tab:: toml |
| 1118 |
|
| 1119 |
.. code-block:: toml |
| 1120 |
|
| 1121 |
[pytest] |
| 1122 |
junit_family = "legacy" |
| 1123 |
|
| 1124 |
.. tab:: ini |
| 1125 |
|
| 1126 |
.. code-block:: ini |
| 1127 |
|
| 1128 |
[pytest] |
| 1129 |
junit_family = legacy |
| 1130 |
|
| 1131 |
By using ``legacy`` you will keep using the legacy/xunit1 format when upgrading to |
| 1132 |
pytest 6.0, where the default format will be ``xunit2``. |
| 1133 |
|
| 1134 |
In order to let users know about the transition, pytest will issue a warning in case |
| 1135 |
the ``--junit-xml`` option is given in the command line but ``junit_family`` is not explicitly |
| 1136 |
configured in ``pytest.ini``. |
| 1137 |
|
| 1138 |
Services known to support the ``xunit2`` format: |
| 1139 |
|
| 1140 |
* `Jenkins <https://www.jenkins.io/>`__ with the `JUnit <https://plugins.jenkins.io/junit>`__ plugin. |
| 1141 |
* `Azure Pipelines <https://azure.microsoft.com/en-us/services/devops/pipelines>`__. |
| 1142 |
|
| 1143 |
Node Construction changed to ``Node.from_parent`` |
| 1144 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 1145 |
|
| 1146 |
.. versionchanged:: 6.0 |
| 1147 |
|
| 1148 |
The construction of nodes now should use the named constructor ``from_parent``. |
| 1149 |
This limitation in api surface intends to enable better/simpler refactoring of the collection tree. |
| 1150 |
|
| 1151 |
This means that instead of :code:`MyItem(name="foo", parent=collector, obj=42)` |
| 1152 |
one now has to invoke :code:`MyItem.from_parent(collector, name="foo")`. |
| 1153 |
|
| 1154 |
Plugins that wish to support older versions of pytest and suppress the warning can use |
| 1155 |
`hasattr` to check if `from_parent` exists in that version: |
| 1156 |
|
| 1157 |
.. code-block:: python |
| 1158 |
|
| 1159 |
def pytest_pycollect_makeitem(collector, name, obj): |
| 1160 |
if hasattr(MyItem, "from_parent"): |
| 1161 |
item = MyItem.from_parent(collector, name="foo") |
| 1162 |
item.obj = 42 |
| 1163 |
return item |
| 1164 |
else: |
| 1165 |
return MyItem(name="foo", parent=collector, obj=42) |
| 1166 |
|
| 1167 |
Note that ``from_parent`` should only be called with keyword arguments for the parameters. |
| 1168 |
|
| 1169 |
|
| 1170 |
``pytest.fixture`` arguments are keyword only |
| 1171 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 1172 |
|
| 1173 |
.. versionremoved:: 6.0 |
| 1174 |
|
| 1175 |
Passing arguments to pytest.fixture() as positional arguments has been removed - pass them by keyword instead. |
| 1176 |
|
| 1177 |
``funcargnames`` alias for ``fixturenames`` |
| 1178 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 1179 |
|
| 1180 |
.. versionremoved:: 6.0 |
| 1181 |
|
| 1182 |
The ``FixtureRequest``, ``Metafunc``, and ``Function`` classes track the names of |
| 1183 |
their associated fixtures, with the aptly-named ``fixturenames`` attribute. |
| 1184 |
|
| 1185 |
Prior to pytest 2.3, this attribute was named ``funcargnames``, and we have kept |
| 1186 |
that as an alias since. It is finally due for removal, as it is often confusing |
| 1187 |
in places where we or plugin authors must distinguish between fixture names and |
| 1188 |
names supplied by non-fixture things such as ``pytest.mark.parametrize``. |
| 1189 |
|
| 1190 |
|
| 1191 |
.. _pytest.config global deprecated: |
| 1192 |
|
| 1193 |
``pytest.config`` global |
| 1194 |
~~~~~~~~~~~~~~~~~~~~~~~~ |
| 1195 |
|
| 1196 |
.. versionremoved:: 5.0 |
| 1197 |
|
| 1198 |
The ``pytest.config`` global object is deprecated. Instead use |
| 1199 |
``request.config`` (via the ``request`` fixture) or if you are a plugin author |
| 1200 |
use the ``pytest_configure(config)`` hook. Note that many hooks can also access |
| 1201 |
the ``config`` object indirectly, through ``session.config`` or ``item.config`` for example. |
| 1202 |
|
| 1203 |
|
| 1204 |
.. _`raises message deprecated`: |
| 1205 |
|
| 1206 |
``"message"`` parameter of ``pytest.raises`` |
| 1207 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 1208 |
|
| 1209 |
.. versionremoved:: 5.0 |
| 1210 |
|
| 1211 |
It is a common mistake to think this parameter will match the exception message, while in fact |
| 1212 |
it only serves to provide a custom message in case the ``pytest.raises`` check fails. To prevent |
| 1213 |
users from making this mistake, and because it is believed to be little used, pytest is |
| 1214 |
deprecating it without providing an alternative for the moment. |
| 1215 |
|
| 1216 |
If you have a valid use case for this parameter, consider that to obtain the same results |
| 1217 |
you can just call ``pytest.fail`` manually at the end of the ``with`` statement. |
| 1218 |
|
| 1219 |
For example: |
| 1220 |
|
| 1221 |
.. code-block:: python |
| 1222 |
|
| 1223 |
with pytest.raises(TimeoutError, message="Client got unexpected message"): |
| 1224 |
wait_for(websocket.recv(), 0.5) |
| 1225 |
|
| 1226 |
|
| 1227 |
Becomes: |
| 1228 |
|
| 1229 |
.. code-block:: python |
| 1230 |
|
| 1231 |
with pytest.raises(TimeoutError): |
| 1232 |
wait_for(websocket.recv(), 0.5) |
| 1233 |
pytest.fail("Client got unexpected message") |
| 1234 |
|
| 1235 |
|
| 1236 |
If you still have concerns about this deprecation and future removal, please comment on |
| 1237 |
:issue:`3974`. |
| 1238 |
|
| 1239 |
|
| 1240 |
.. _raises-warns-exec: |
| 1241 |
|
| 1242 |
``raises`` / ``warns`` with a string as the second argument |
| 1243 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 1244 |
|
| 1245 |
.. versionremoved:: 5.0 |
| 1246 |
|
| 1247 |
Use the context manager form of these instead. When necessary, invoke ``exec`` |
| 1248 |
directly. |
| 1249 |
|
| 1250 |
Example: |
| 1251 |
|
| 1252 |
.. code-block:: python |
| 1253 |
|
| 1254 |
pytest.raises(ZeroDivisionError, "1 / 0") |
| 1255 |
pytest.raises(SyntaxError, "a $ b") |
| 1256 |
|
| 1257 |
pytest.warns(DeprecationWarning, "my_function()") |
| 1258 |
pytest.warns(SyntaxWarning, "assert(1, 2)") |
| 1259 |
|
| 1260 |
Becomes: |
| 1261 |
|
| 1262 |
.. code-block:: python |
| 1263 |
|
| 1264 |
with pytest.raises(ZeroDivisionError): |
| 1265 |
1 / 0 |
| 1266 |
with pytest.raises(SyntaxError): |
| 1267 |
exec("a $ b") # exec is required for invalid syntax |
| 1268 |
|
| 1269 |
with pytest.warns(DeprecationWarning): |
| 1270 |
my_function() |
| 1271 |
with pytest.warns(SyntaxWarning): |
| 1272 |
exec("assert(1, 2)") # exec is used to avoid a top-level warning |
| 1273 |
|
| 1274 |
|
| 1275 |
|
| 1276 |
|
| 1277 |
Using ``Class`` in custom Collectors |
| 1278 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 1279 |
|
| 1280 |
.. versionremoved:: 4.0 |
| 1281 |
|
| 1282 |
Using objects named ``"Class"`` as a way to customize the type of nodes that are collected in ``Collector`` |
| 1283 |
subclasses has been deprecated. Users instead should use ``pytest_pycollect_makeitem`` to customize node types during |
| 1284 |
collection. |
| 1285 |
|
| 1286 |
This issue should affect only advanced plugins who create new collection types, so if you see this warning |
| 1287 |
message please contact the authors so they can change the code. |
| 1288 |
|
| 1289 |
|
| 1290 |
.. _marks in pytest.parametrize deprecated: |
| 1291 |
|
| 1292 |
marks in ``pytest.mark.parametrize`` |
| 1293 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 1294 |
|
| 1295 |
.. versionremoved:: 4.0 |
| 1296 |
|
| 1297 |
Applying marks to values of a ``pytest.mark.parametrize`` call is now deprecated. For example: |
| 1298 |
|
| 1299 |
.. code-block:: python |
| 1300 |
|
| 1301 |
@pytest.mark.parametrize( |
| 1302 |
"a, b", |
| 1303 |
[ |
| 1304 |
(3, 9), |
| 1305 |
pytest.mark.xfail(reason="flaky")(6, 36), |
| 1306 |
(10, 100), |
| 1307 |
(20, 200), |
| 1308 |
(40, 400), |
| 1309 |
(50, 500), |
| 1310 |
], |
| 1311 |
) |
| 1312 |
def test_foo(a, b): ... |
| 1313 |
|
| 1314 |
This code applies the ``pytest.mark.xfail(reason="flaky")`` mark to the ``(6, 36)`` value of the above parametrization |
| 1315 |
call. |
| 1316 |
|
| 1317 |
This was considered hard to read and understand, and also its implementation presented problems to the code preventing |
| 1318 |
further internal improvements in the marks architecture. |
| 1319 |
|
| 1320 |
To update the code, use ``pytest.param``: |
| 1321 |
|
| 1322 |
.. code-block:: python |
| 1323 |
|
| 1324 |
@pytest.mark.parametrize( |
| 1325 |
"a, b", |
| 1326 |
[ |
| 1327 |
(3, 9), |
| 1328 |
pytest.param(6, 36, marks=pytest.mark.xfail(reason="flaky")), |
| 1329 |
(10, 100), |
| 1330 |
(20, 200), |
| 1331 |
(40, 400), |
| 1332 |
(50, 500), |
| 1333 |
], |
| 1334 |
) |
| 1335 |
def test_foo(a, b): ... |
| 1336 |
|
| 1337 |
|
| 1338 |
.. _pytest_funcarg__ prefix deprecated: |
| 1339 |
|
| 1340 |
``pytest_funcarg__`` prefix |
| 1341 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 1342 |
|
| 1343 |
.. versionremoved:: 4.0 |
| 1344 |
|
| 1345 |
In very early pytest versions fixtures could be defined using the ``pytest_funcarg__`` prefix: |
| 1346 |
|
| 1347 |
.. code-block:: python |
| 1348 |
|
| 1349 |
def pytest_funcarg__data(): |
| 1350 |
return SomeData() |
| 1351 |
|
| 1352 |
Switch over to the ``@pytest.fixture`` decorator: |
| 1353 |
|
| 1354 |
.. code-block:: python |
| 1355 |
|
| 1356 |
@pytest.fixture |
| 1357 |
def data(): |
| 1358 |
return SomeData() |
| 1359 |
|
| 1360 |
|
| 1361 |
|
| 1362 |
[pytest] section in setup.cfg files |
| 1363 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 1364 |
|
| 1365 |
.. versionremoved:: 4.0 |
| 1366 |
|
| 1367 |
``[pytest]`` sections in ``setup.cfg`` files should now be named ``[tool:pytest]`` |
| 1368 |
to avoid conflicts with other distutils commands. |
| 1369 |
|
| 1370 |
|
| 1371 |
.. _metafunc.addcall deprecated: |
| 1372 |
|
| 1373 |
Metafunc.addcall |
| 1374 |
~~~~~~~~~~~~~~~~ |
| 1375 |
|
| 1376 |
.. versionremoved:: 4.0 |
| 1377 |
|
| 1378 |
``Metafunc.addcall`` was a precursor to the current parametrized mechanism. Users should use |
| 1379 |
:meth:`pytest.Metafunc.parametrize` instead. |
| 1380 |
|
| 1381 |
Example: |
| 1382 |
|
| 1383 |
.. code-block:: python |
| 1384 |
|
| 1385 |
def pytest_generate_tests(metafunc): |
| 1386 |
metafunc.addcall({"i": 1}, id="1") |
| 1387 |
metafunc.addcall({"i": 2}, id="2") |
| 1388 |
|
| 1389 |
Becomes: |
| 1390 |
|
| 1391 |
.. code-block:: python |
| 1392 |
|
| 1393 |
def pytest_generate_tests(metafunc): |
| 1394 |
metafunc.parametrize("i", [1, 2], ids=["1", "2"]) |
| 1395 |
|
| 1396 |
|
| 1397 |
.. _cached_setup deprecated: |
| 1398 |
|
| 1399 |
``cached_setup`` |
| 1400 |
~~~~~~~~~~~~~~~~ |
| 1401 |
|
| 1402 |
.. versionremoved:: 4.0 |
| 1403 |
|
| 1404 |
``request.cached_setup`` was the precursor of the setup/teardown mechanism available to fixtures. |
| 1405 |
|
| 1406 |
Example: |
| 1407 |
|
| 1408 |
.. code-block:: python |
| 1409 |
|
| 1410 |
@pytest.fixture |
| 1411 |
def db_session(): |
| 1412 |
return request.cached_setup( |
| 1413 |
setup=Session.create, teardown=lambda session: session.close(), scope="module" |
| 1414 |
) |
| 1415 |
|
| 1416 |
This should be updated to make use of standard fixture mechanisms: |
| 1417 |
|
| 1418 |
.. code-block:: python |
| 1419 |
|
| 1420 |
@pytest.fixture(scope="module") |
| 1421 |
def db_session(): |
| 1422 |
session = Session.create() |
| 1423 |
yield session |
| 1424 |
session.close() |
| 1425 |
|
| 1426 |
|
| 1427 |
You can consult :std:doc:`funcarg comparison section in the docs <funcarg_compare>` for |
| 1428 |
more information. |
| 1429 |
|
| 1430 |
|
| 1431 |
.. _pytest_plugins in non-top-level conftest files deprecated: |
| 1432 |
|
| 1433 |
pytest_plugins in non-top-level conftest files |
| 1434 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 1435 |
|
| 1436 |
.. versionremoved:: 4.0 |
| 1437 |
|
| 1438 |
Defining :globalvar:`pytest_plugins` is now deprecated in non-top-level conftest.py |
| 1439 |
files because they will activate referenced plugins *globally*, which is surprising because for all other pytest |
| 1440 |
features ``conftest.py`` files are only *active* for tests at or below it. |
| 1441 |
|
| 1442 |
|
| 1443 |
.. _config.warn and node.warn deprecated: |
| 1444 |
|
| 1445 |
``Config.warn`` and ``Node.warn`` |
| 1446 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 1447 |
|
| 1448 |
.. versionremoved:: 4.0 |
| 1449 |
|
| 1450 |
Those methods were part of the internal pytest warnings system, but since ``3.8`` pytest is using the builtin warning |
| 1451 |
system for its own warnings, so those two functions are now deprecated. |
| 1452 |
|
| 1453 |
``Config.warn`` should be replaced by calls to the standard ``warnings.warn``, example: |
| 1454 |
|
| 1455 |
.. code-block:: python |
| 1456 |
|
| 1457 |
config.warn("C1", "some warning") |
| 1458 |
|
| 1459 |
Becomes: |
| 1460 |
|
| 1461 |
.. code-block:: python |
| 1462 |
|
| 1463 |
warnings.warn(pytest.PytestWarning("some warning")) |
| 1464 |
|
| 1465 |
``Node.warn`` now supports two signatures: |
| 1466 |
|
| 1467 |
* ``node.warn(PytestWarning("some message"))``: is now the **recommended** way to call this function. |
| 1468 |
The warning instance must be a PytestWarning or subclass. |
| 1469 |
|
| 1470 |
* ``node.warn("CI", "some message")``: this code/message form has been **removed** and should be converted to the warning instance form above. |
| 1471 |
|
| 1472 |
.. _record_xml_property deprecated: |
| 1473 |
|
| 1474 |
record_xml_property |
| 1475 |
~~~~~~~~~~~~~~~~~~~ |
| 1476 |
|
| 1477 |
.. versionremoved:: 4.0 |
| 1478 |
|
| 1479 |
The ``record_xml_property`` fixture is now deprecated in favor of the more generic ``record_property``, which |
| 1480 |
can be used by other consumers (for example ``pytest-html``) to obtain custom information about the test run. |
| 1481 |
|
| 1482 |
This is just a matter of renaming the fixture as the API is the same: |
| 1483 |
|
| 1484 |
.. code-block:: python |
| 1485 |
|
| 1486 |
def test_foo(record_xml_property): ... |
| 1487 |
|
| 1488 |
Change to: |
| 1489 |
|
| 1490 |
.. code-block:: python |
| 1491 |
|
| 1492 |
def test_foo(record_property): ... |
| 1493 |
|
| 1494 |
|
| 1495 |
.. _passing command-line string to pytest.main deprecated: |
| 1496 |
|
| 1497 |
Passing command-line string to ``pytest.main()`` |
| 1498 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 1499 |
|
| 1500 |
.. versionremoved:: 4.0 |
| 1501 |
|
| 1502 |
Passing a command-line string to ``pytest.main()`` is deprecated: |
| 1503 |
|
| 1504 |
.. code-block:: python |
| 1505 |
|
| 1506 |
pytest.main("-v -s") |
| 1507 |
|
| 1508 |
Pass a list instead: |
| 1509 |
|
| 1510 |
.. code-block:: python |
| 1511 |
|
| 1512 |
pytest.main(["-v", "-s"]) |
| 1513 |
|
| 1514 |
|
| 1515 |
By passing a string, users expect that pytest will interpret that command-line using the shell rules they are working |
| 1516 |
on (for example ``bash`` or ``Powershell``), but this is very hard/impossible to do in a portable way. |
| 1517 |
|
| 1518 |
|
| 1519 |
.. _calling fixtures directly deprecated: |
| 1520 |
|
| 1521 |
Calling fixtures directly |
| 1522 |
~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 1523 |
|
| 1524 |
.. versionremoved:: 4.0 |
| 1525 |
|
| 1526 |
Calling a fixture function directly, as opposed to request them in a test function, is deprecated. |
| 1527 |
|
| 1528 |
For example: |
| 1529 |
|
| 1530 |
.. code-block:: python |
| 1531 |
|
| 1532 |
@pytest.fixture |
| 1533 |
def cell(): |
| 1534 |
return ... |
| 1535 |
|
| 1536 |
|
| 1537 |
@pytest.fixture |
| 1538 |
def full_cell(): |
| 1539 |
cell = cell() |
| 1540 |
cell.make_full() |
| 1541 |
return cell |
| 1542 |
|
| 1543 |
This is a great source of confusion to new users, which will often call the fixture functions and request them from test functions interchangeably, which breaks the fixture resolution model. |
| 1544 |
|
| 1545 |
In those cases just request the function directly in the dependent fixture: |
| 1546 |
|
| 1547 |
.. code-block:: python |
| 1548 |
|
| 1549 |
@pytest.fixture |
| 1550 |
def cell(): |
| 1551 |
return ... |
| 1552 |
|
| 1553 |
|
| 1554 |
@pytest.fixture |
| 1555 |
def full_cell(cell): |
| 1556 |
cell.make_full() |
| 1557 |
return cell |
| 1558 |
|
| 1559 |
Alternatively if the fixture function is called multiple times inside a test (making it hard to apply the above pattern) or |
| 1560 |
if you would like to make minimal changes to the code, you can create a fixture which calls the original function together |
| 1561 |
with the ``name`` parameter: |
| 1562 |
|
| 1563 |
.. code-block:: python |
| 1564 |
|
| 1565 |
def cell(): |
| 1566 |
return ... |
| 1567 |
|
| 1568 |
|
| 1569 |
@pytest.fixture(name="cell") |
| 1570 |
def cell_fixture(): |
| 1571 |
return cell() |
| 1572 |
|
| 1573 |
|
| 1574 |
.. _internal classes accessed through node deprecated: |
| 1575 |
|
| 1576 |
Internal classes accessed through ``Node`` |
| 1577 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 1578 |
|
| 1579 |
.. versionremoved:: 4.0 |
| 1580 |
|
| 1581 |
Access of ``Module``, ``Function``, ``Class``, ``Instance``, ``File`` and ``Item`` through ``Node`` instances now issue |
| 1582 |
this warning: |
| 1583 |
|
| 1584 |
.. code-block:: text |
| 1585 |
|
| 1586 |
usage of Function.Module is deprecated, please use pytest.Module instead |
| 1587 |
|
| 1588 |
Users should just ``import pytest`` and access those objects using the ``pytest`` module. |
| 1589 |
|
| 1590 |
This has been documented as deprecated for years, but only now we are actually emitting deprecation warnings. |
| 1591 |
|
| 1592 |
``Node.get_marker`` |
| 1593 |
~~~~~~~~~~~~~~~~~~~ |
| 1594 |
|
| 1595 |
.. versionremoved:: 4.0 |
| 1596 |
|
| 1597 |
As part of a large :ref:`marker-revamp`, ``_pytest.nodes.Node.get_marker`` is removed. See |
| 1598 |
:ref:`the documentation <update marker code>` on tips on how to update your code. |
| 1599 |
|
| 1600 |
|
| 1601 |
``somefunction.markname`` |
| 1602 |
~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 1603 |
|
| 1604 |
.. versionremoved:: 4.0 |
| 1605 |
|
| 1606 |
As part of a large :ref:`marker-revamp` we already deprecated using ``MarkInfo`` |
| 1607 |
the only correct way to get markers of an element is via ``node.iter_markers(name)``. |
| 1608 |
|
| 1609 |
|
| 1610 |
.. _pytest.namespace deprecated: |
| 1611 |
|
| 1612 |
``pytest_namespace`` |
| 1613 |
~~~~~~~~~~~~~~~~~~~~ |
| 1614 |
|
| 1615 |
.. versionremoved:: 4.0 |
| 1616 |
|
| 1617 |
This hook is deprecated because it greatly complicates the pytest internals regarding configuration and initialization, making some |
| 1618 |
bug fixes and refactorings impossible. |
| 1619 |
|
| 1620 |
Example of usage: |
| 1621 |
|
| 1622 |
.. code-block:: python |
| 1623 |
|
| 1624 |
class MySymbol: ... |
| 1625 |
|
| 1626 |
|
| 1627 |
def pytest_namespace(): |
| 1628 |
return {"my_symbol": MySymbol()} |
| 1629 |
|
| 1630 |
|
| 1631 |
Plugin authors relying on this hook should instead require that users now import the plugin modules directly (with an appropriate public API). |
| 1632 |
|
| 1633 |
As a stopgap measure, plugin authors may still inject their names into pytest's namespace, usually during ``pytest_configure``: |
| 1634 |
|
| 1635 |
.. code-block:: python |
| 1636 |
|
| 1637 |
import pytest |
| 1638 |
|
| 1639 |
|
| 1640 |
def pytest_configure(): |
| 1641 |
pytest.my_symbol = MySymbol() |