| 1 |
:orphan: |
| 2 |
|
| 3 |
.. _`pytest helpers`: |
| 4 |
|
| 5 |
Pytest API and builtin fixtures |
| 6 |
================================================ |
| 7 |
|
| 8 |
|
| 9 |
Most of the information of this page has been moved over to :ref:`api-reference`. |
| 10 |
|
| 11 |
For information on plugin hooks and objects, see :ref:`plugins`. |
| 12 |
|
| 13 |
For information on the ``pytest.mark`` mechanism, see :ref:`mark`. |
| 14 |
|
| 15 |
For information about fixtures, see :ref:`fixtures`. To see a complete list of available fixtures (add :option:`-v` to also see fixtures with leading ``_``), type : |
| 16 |
|
| 17 |
.. code-block:: pytest |
| 18 |
|
| 19 |
$ pytest --fixtures -v |
| 20 |
=========================== test session starts ============================ |
| 21 |
platform linux -- Python 3.x.y, pytest-9.x.y, pluggy-1.x.y -- $PYTHON_PREFIX/bin/python |
| 22 |
cachedir: .pytest_cache |
| 23 |
rootdir: /home/sweet/project |
| 24 |
collected 0 items |
| 25 |
cache -- .../_pytest/cacheprovider.py:560 |
| 26 |
Return a cache object that can persist state between testing sessions. |
| 27 |
|
| 28 |
cache.get(key, default) |
| 29 |
cache.set(key, value) |
| 30 |
|
| 31 |
Keys must be ``/`` separated strings, where the first part is usually the |
| 32 |
name of your plugin or application to avoid clashes with other cache users. |
| 33 |
|
| 34 |
Values can be any object handled by the json stdlib module. |
| 35 |
|
| 36 |
capsys -- .../_pytest/capture.py:1007 |
| 37 |
Enable text capturing of writes to ``sys.stdout`` and ``sys.stderr``. |
| 38 |
|
| 39 |
The captured output is made available via ``capsys.readouterr()`` method |
| 40 |
calls, which return a ``(out, err)`` namedtuple. |
| 41 |
``out`` and ``err`` will be ``text`` objects. |
| 42 |
|
| 43 |
Returns an instance of :class:`CaptureFixture[str] <pytest.CaptureFixture>`. |
| 44 |
|
| 45 |
Example: |
| 46 |
|
| 47 |
.. code-block:: python |
| 48 |
|
| 49 |
def test_output(capsys): |
| 50 |
print("hello") |
| 51 |
captured = capsys.readouterr() |
| 52 |
assert captured.out == "hello\n" |
| 53 |
|
| 54 |
capteesys -- .../_pytest/capture.py:1035 |
| 55 |
Enable simultaneous text capturing and pass-through of writes |
| 56 |
to ``sys.stdout`` and ``sys.stderr`` as defined by ``--capture=``. |
| 57 |
|
| 58 |
|
| 59 |
The captured output is made available via ``capteesys.readouterr()`` method |
| 60 |
calls, which return a ``(out, err)`` namedtuple. |
| 61 |
``out`` and ``err`` will be ``text`` objects. |
| 62 |
|
| 63 |
The output is also passed-through, allowing it to be "live-printed", |
| 64 |
reported, or both as defined by ``--capture=``. |
| 65 |
|
| 66 |
Returns an instance of :class:`CaptureFixture[str] <pytest.CaptureFixture>`. |
| 67 |
|
| 68 |
Example: |
| 69 |
|
| 70 |
.. code-block:: python |
| 71 |
|
| 72 |
def test_output(capteesys): |
| 73 |
print("hello") |
| 74 |
captured = capteesys.readouterr() |
| 75 |
assert captured.out == "hello\n" |
| 76 |
|
| 77 |
capsysbinary -- .../_pytest/capture.py:1070 |
| 78 |
Enable bytes capturing of writes to ``sys.stdout`` and ``sys.stderr``. |
| 79 |
|
| 80 |
The captured output is made available via ``capsysbinary.readouterr()`` |
| 81 |
method calls, which return a ``(out, err)`` namedtuple. |
| 82 |
``out`` and ``err`` will be ``bytes`` objects. |
| 83 |
|
| 84 |
Returns an instance of :class:`CaptureFixture[bytes] <pytest.CaptureFixture>`. |
| 85 |
|
| 86 |
Example: |
| 87 |
|
| 88 |
.. code-block:: python |
| 89 |
|
| 90 |
def test_output(capsysbinary): |
| 91 |
print("hello") |
| 92 |
captured = capsysbinary.readouterr() |
| 93 |
assert captured.out == b"hello\n" |
| 94 |
|
| 95 |
capfd -- .../_pytest/capture.py:1098 |
| 96 |
Enable text capturing of writes to file descriptors ``1`` and ``2``. |
| 97 |
|
| 98 |
The captured output is made available via ``capfd.readouterr()`` method |
| 99 |
calls, which return a ``(out, err)`` namedtuple. |
| 100 |
``out`` and ``err`` will be ``text`` objects. |
| 101 |
|
| 102 |
Returns an instance of :class:`CaptureFixture[str] <pytest.CaptureFixture>`. |
| 103 |
|
| 104 |
Example: |
| 105 |
|
| 106 |
.. code-block:: python |
| 107 |
|
| 108 |
def test_system_echo(capfd): |
| 109 |
os.system('echo "hello"') |
| 110 |
captured = capfd.readouterr() |
| 111 |
assert captured.out == "hello\n" |
| 112 |
|
| 113 |
capfdbinary -- .../_pytest/capture.py:1126 |
| 114 |
Enable bytes capturing of writes to file descriptors ``1`` and ``2``. |
| 115 |
|
| 116 |
The captured output is made available via ``capfd.readouterr()`` method |
| 117 |
calls, which return a ``(out, err)`` namedtuple. |
| 118 |
``out`` and ``err`` will be ``byte`` objects. |
| 119 |
|
| 120 |
Returns an instance of :class:`CaptureFixture[bytes] <pytest.CaptureFixture>`. |
| 121 |
|
| 122 |
Example: |
| 123 |
|
| 124 |
.. code-block:: python |
| 125 |
|
| 126 |
def test_system_echo(capfdbinary): |
| 127 |
os.system('echo "hello"') |
| 128 |
captured = capfdbinary.readouterr() |
| 129 |
assert captured.out == b"hello\n" |
| 130 |
|
| 131 |
doctest_namespace [session scope] -- .../_pytest/doctest.py:721 |
| 132 |
Fixture that returns a :py:class:`dict` that will be injected into the |
| 133 |
namespace of doctests. |
| 134 |
|
| 135 |
Usually this fixture is used in conjunction with another ``autouse`` fixture: |
| 136 |
|
| 137 |
.. code-block:: python |
| 138 |
|
| 139 |
@pytest.fixture(autouse=True) |
| 140 |
def add_np(doctest_namespace): |
| 141 |
doctest_namespace["np"] = numpy |
| 142 |
|
| 143 |
For more details: :ref:`doctest_namespace`. |
| 144 |
|
| 145 |
pytestconfig [session scope] -- .../_pytest/fixtures.py:1564 |
| 146 |
Session-scoped fixture that returns the session's :class:`pytest.Config` |
| 147 |
object. |
| 148 |
|
| 149 |
Example:: |
| 150 |
|
| 151 |
def test_foo(pytestconfig): |
| 152 |
if pytestconfig.get_verbosity() > 0: |
| 153 |
... |
| 154 |
|
| 155 |
record_property -- .../_pytest/junitxml.py:275 |
| 156 |
Add extra properties to the calling test. |
| 157 |
|
| 158 |
User properties become part of the test report and are available to the |
| 159 |
configured reporters, like JUnit XML. |
| 160 |
|
| 161 |
The fixture is callable with ``name, value``. The value is automatically |
| 162 |
XML-encoded. |
| 163 |
|
| 164 |
Example:: |
| 165 |
|
| 166 |
def test_function(record_property): |
| 167 |
record_property("example_key", 1) |
| 168 |
|
| 169 |
record_xml_attribute -- .../_pytest/junitxml.py:298 |
| 170 |
Add extra xml attributes to the tag for the calling test. |
| 171 |
|
| 172 |
The fixture is callable with ``name, value``. The value is |
| 173 |
automatically XML-encoded. |
| 174 |
|
| 175 |
record_testsuite_property [session scope] -- .../_pytest/junitxml.py:336 |
| 176 |
Record a new ``<property>`` tag as child of the root ``<testsuite>``. |
| 177 |
|
| 178 |
This is suitable to writing global information regarding the entire test |
| 179 |
suite, and is compatible with ``xunit2`` JUnit family. |
| 180 |
|
| 181 |
This is a ``session``-scoped fixture which is called with ``(name, value)``. Example: |
| 182 |
|
| 183 |
.. code-block:: python |
| 184 |
|
| 185 |
def test_foo(record_testsuite_property): |
| 186 |
record_testsuite_property("ARCH", "PPC") |
| 187 |
record_testsuite_property("STORAGE_TYPE", "CEPH") |
| 188 |
|
| 189 |
:param name: |
| 190 |
The property name. |
| 191 |
:param value: |
| 192 |
The property value. Will be converted to a string. |
| 193 |
|
| 194 |
.. warning:: |
| 195 |
|
| 196 |
Currently this fixture **does not work** with the |
| 197 |
`pytest-xdist <https://github.com/pytest-dev/pytest-xdist>`__ plugin. See |
| 198 |
:issue:`7767` for details. |
| 199 |
|
| 200 |
tmpdir_factory [session scope] -- .../_pytest/legacypath.py:298 |
| 201 |
Return a :class:`pytest.TempdirFactory` instance for the test session. |
| 202 |
|
| 203 |
tmpdir -- .../_pytest/legacypath.py:305 |
| 204 |
Return a temporary directory (as `legacy_path`_ object) |
| 205 |
which is unique to each test function invocation. |
| 206 |
The temporary directory is created as a subdirectory |
| 207 |
of the base temporary directory, with configurable retention, |
| 208 |
as discussed in :ref:`temporary directory location and retention`. |
| 209 |
|
| 210 |
.. note:: |
| 211 |
These days, it is preferred to use ``tmp_path``. |
| 212 |
|
| 213 |
:ref:`About the tmpdir and tmpdir_factory fixtures<tmpdir and tmpdir_factory>`. |
| 214 |
|
| 215 |
.. _legacy_path: https://py.readthedocs.io/en/latest/path.html |
| 216 |
|
| 217 |
caplog -- .../_pytest/logging.py:611 |
| 218 |
Access and control log capturing. |
| 219 |
|
| 220 |
Captured logs are available through the following properties/methods:: |
| 221 |
|
| 222 |
* caplog.messages -> list of format-interpolated log messages |
| 223 |
* caplog.text -> string containing formatted log output |
| 224 |
* caplog.records -> list of logging.LogRecord instances |
| 225 |
* caplog.record_tuples -> list of (logger_name, level, message) tuples |
| 226 |
* caplog.clear() -> clear captured records and formatted log output string |
| 227 |
|
| 228 |
monkeypatch -- .../_pytest/monkeypatch.py:36 |
| 229 |
A convenient fixture for monkey-patching. |
| 230 |
|
| 231 |
The fixture provides these methods to modify objects, dictionaries, or |
| 232 |
:data:`os.environ`: |
| 233 |
|
| 234 |
* :meth:`monkeypatch.setattr(obj, name, value, raising=True) <pytest.MonkeyPatch.setattr>` |
| 235 |
* :meth:`monkeypatch.delattr(obj, name, raising=True) <pytest.MonkeyPatch.delattr>` |
| 236 |
* :meth:`monkeypatch.setitem(mapping, name, value) <pytest.MonkeyPatch.setitem>` |
| 237 |
* :meth:`monkeypatch.delitem(obj, name, raising=True) <pytest.MonkeyPatch.delitem>` |
| 238 |
* :meth:`monkeypatch.setenv(name, value, prepend=None) <pytest.MonkeyPatch.setenv>` |
| 239 |
* :meth:`monkeypatch.delenv(name, raising=True) <pytest.MonkeyPatch.delenv>` |
| 240 |
* :meth:`monkeypatch.syspath_prepend(path) <pytest.MonkeyPatch.syspath_prepend>` |
| 241 |
* :meth:`monkeypatch.chdir(path) <pytest.MonkeyPatch.chdir>` |
| 242 |
* :meth:`monkeypatch.context() <pytest.MonkeyPatch.context>` |
| 243 |
|
| 244 |
All modifications will be undone after the requesting test function or |
| 245 |
fixture has finished. The ``raising`` parameter determines if a :class:`KeyError` |
| 246 |
or :class:`AttributeError` will be raised if the set/deletion operation does not have the |
| 247 |
specified target. |
| 248 |
|
| 249 |
To undo modifications done by the fixture in a contained scope, |
| 250 |
use :meth:`context() <pytest.MonkeyPatch.context>`. |
| 251 |
|
| 252 |
recwarn -- .../_pytest/recwarn.py:37 |
| 253 |
Return a :class:`WarningsRecorder` instance that records all warnings emitted by test functions. |
| 254 |
|
| 255 |
See :ref:`warnings` for information on warning categories. |
| 256 |
|
| 257 |
subtests -- .../_pytest/subtests.py:136 |
| 258 |
Provides subtests functionality. |
| 259 |
|
| 260 |
tmp_path_factory [session scope] -- .../_pytest/tmpdir.py:276 |
| 261 |
Return a :class:`pytest.TempPathFactory` instance for the test session. |
| 262 |
|
| 263 |
tmp_path -- .../_pytest/tmpdir.py:291 |
| 264 |
Return a temporary directory (as :class:`pathlib.Path` object) |
| 265 |
which is unique to each test function invocation. |
| 266 |
The temporary directory is created as a subdirectory |
| 267 |
of the base temporary directory, with configurable retention, |
| 268 |
as discussed in :ref:`temporary directory location and retention`. |
| 269 |
|
| 270 |
|
| 271 |
========================== no tests ran in 0.12s =========================== |
| 272 |
|
| 273 |
You can also interactively ask for help, e.g. by typing on the Python interactive prompt something like: |
| 274 |
|
| 275 |
.. code-block:: python |
| 276 |
|
| 277 |
import pytest |
| 278 |
|
| 279 |
help(pytest) |