Add reference docs to cache and capture fixtures

This commit is contained in:
Bruno Oliveira
2018-02-08 19:57:26 -02:00
parent 6fa9768545
commit 749288dcb6
4 changed files with 153 additions and 26 deletions

View File

@@ -212,7 +212,7 @@ the cache and this will be quick::
test_caching.py:14: AssertionError
1 failed in 0.12 seconds
See the `cache-api`_ for more details.
See the :ref:`cache-api` for more details.
Inspecting Cache content
@@ -247,22 +247,3 @@ servers where isolation and correctness is more important
than speed.
.. _`cache-api`:
config.cache API
------------------
The ``config.cache`` object allows other plugins,
including ``conftest.py`` files,
to safely and flexibly store and retrieve values across
test runs because the ``config`` object is available
in many places.
Under the hood, the cache plugin uses the simple
dumps/loads API of the json stdlib module
.. currentmodule:: _pytest.cacheprovider
.. automethod:: Cache.get
.. automethod:: Cache.set
.. automethod:: Cache.makedir

View File

@@ -118,7 +118,6 @@ reporting or interaction with exceptions:
.. autofunction:: pytest_enter_pdb
Objects
-------
@@ -196,3 +195,140 @@ Full reference to objects accessible from :ref:`fixtures <fixture>` or hooks
.. autoclass:: LineMatcher()
:members:
.. autoclass:: _pytest.capture.CaptureFixture()
:members:
Fixtures
--------
Fixtures are requested by test functions or other fixtures by declaring them as argument names.
Example of a test requiring a fixture:
.. code-block:: python
def test_output(capsys):
print('hello')
out, err = capsys.readouterr()
assert out == 'hello\n'
Example of a fixture requiring another fixture:
.. code-block:: python
@pytest.fixture
def db_session(tmpdir):
fn = tmpdir / 'db.file'
return connect(str(fn))
For more details, consult the full :ref:`fixtures docs <fixture>`.
fixture decorator signature
~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. currentmodule:: _pytest.fixtures
.. autofunction:: fixture
:decorator:
.. _`cache-api`:
config.cache
~~~~~~~~~~~~
The ``config.cache`` object allows other plugins and fixtures
to store and retrieve values across test runs. To access it from fixtures
request ``pytestconfig`` into your fixture and get it with ``pytestconfig.cache``.
Under the hood, the cache plugin uses the simple
``dumps``/``loads`` API of the :py:mod:`json` stdlib module.
.. currentmodule:: _pytest.cacheprovider
.. automethod:: Cache.get
.. automethod:: Cache.set
.. automethod:: Cache.makedir
capsys
~~~~~~
.. currentmodule:: _pytest.capture
.. autofunction:: capsys()
:no-auto-options:
:decorator:
Returns an instance of :py:class:`CaptureFixture`.
Example:
.. code-block:: python
def test_output(capsys):
print("hello")
captured = capsys.readouterr()
assert captured.out == "hello\n"
capsysbinary
~~~~~~~~~~~~
.. autofunction:: capsysbinary()
:no-auto-options:
:decorator:
Returns an instance of :py:class:`CaptureFixture`.
Example:
.. code-block:: python
def test_output(capsysbinary):
print("hello")
captured = capsysbinary.readouterr()
assert captured.out == b"hello\n"
capfd
~~~~~~
.. autofunction:: capfd()
:no-auto-options:
:decorator:
Returns an instance of :py:class:`CaptureFixture`.
Example:
.. code-block:: python
def test_system_echo(capfd):
os.system('echo "hello"')
captured = capsys.readouterr()
assert captured.out == "hello\n"
capfdbinary
~~~~~~~~~~~~
.. autofunction:: capfdbinary()
:no-auto-options:
:decorator:
Returns an instance of :py:class:`CaptureFixture`.
Example:
.. code-block:: python
def test_system_echo(capfdbinary):
os.system('echo "hello"')
captured = capfdbinary.readouterr()
assert captured.out == b"hello\n"