All Projects → kiwicom → Pytest Recording

kiwicom / Pytest Recording

Licence: mit
A pytest plugin that allows recording network interactions via VCR.py

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Pytest Recording

Schemathesis
A modern API testing tool for web applications built with Open API and GraphQL specifications.
Stars: ✭ 768 (+683.67%)
Mutual labels:  hacktoberfest, pytest
Pytest Qt
pytest plugin for Qt (PyQt4, PyQt5 and PySide) application testing
Stars: ✭ 210 (+114.29%)
Mutual labels:  hacktoberfest, pytest
Pytest Mock
Thin-wrapper around the mock package for easier use with pytest
Stars: ✭ 1,020 (+940.82%)
Mutual labels:  hacktoberfest, pytest
Pytest Picked
Run the tests related to the changed files (according to Git) 🤓
Stars: ✭ 262 (+167.35%)
Mutual labels:  hacktoberfest, pytest
Pytest Regressions
Pytest plugin for regression testing: https://pytest-regressions.readthedocs.io
Stars: ✭ 89 (-9.18%)
Mutual labels:  hacktoberfest, pytest
Vue Material
Material design for Vue.js
Stars: ✭ 9,528 (+9622.45%)
Mutual labels:  hacktoberfest
Lipi
A simple static blog generator.
Stars: ✭ 100 (+2.04%)
Mutual labels:  hacktoberfest
Zookeeper
Apache ZooKeeper
Stars: ✭ 10,061 (+10166.33%)
Mutual labels:  hacktoberfest
Typescript Eslint
✨ Monorepo for all the tooling which enables ESLint to support TypeScript
Stars: ✭ 10,831 (+10952.04%)
Mutual labels:  hacktoberfest
Git Stats Importer
📈 Imports your commits from a repository into git-stats history.
Stars: ✭ 100 (+2.04%)
Mutual labels:  hacktoberfest
Dandelion
a diaspora* client for Android
Stars: ✭ 100 (+2.04%)
Mutual labels:  hacktoberfest
Hls.js
HLS.js is a JavaScript library that plays HLS in browsers with support for MSE.
Stars: ✭ 10,791 (+10911.22%)
Mutual labels:  hacktoberfest
Awesome Kotlin
A curated list of awesome Kotlin related stuff Inspired by awesome-java.
Stars: ✭ 9,679 (+9776.53%)
Mutual labels:  hacktoberfest
Gentoo
[MIRROR] Official Gentoo ebuild repository
Stars: ✭ 1,364 (+1291.84%)
Mutual labels:  hacktoberfest
Milligram
A minimalist CSS framework.
Stars: ✭ 9,568 (+9663.27%)
Mutual labels:  hacktoberfest
Mud
Multipath UDP library
Stars: ✭ 100 (+2.04%)
Mutual labels:  hacktoberfest
Openrct2
An open source re-implementation of RollerCoaster Tycoon 2 🎢
Stars: ✭ 10,115 (+10221.43%)
Mutual labels:  hacktoberfest
Javascript
A repository for All algorithms implemented in Javascript (for educational purposes only)
Stars: ✭ 16,117 (+16345.92%)
Mutual labels:  hacktoberfest
Aet
AET - a system that detects visual changes on web sites and performs basic page health checks
Stars: ✭ 100 (+2.04%)
Mutual labels:  hacktoberfest
Magento2
All Submissions you make to Magento Inc. ("Magento") through GitHub are subject to the following terms and conditions: (1) You grant Magento a perpetual, worldwide, non-exclusive, no charge, royalty free, irrevocable license under your applicable copyrights and patents to reproduce, prepare derivative works of, display, publically perform, subli…
Stars: ✭ 9,816 (+9916.33%)
Mutual labels:  hacktoberfest

pytest-recording

|codecov| |Build| |Version| |Python versions| |License|

A pytest plugin that records network interactions in your tests via VCR.py.

Features

  • Straightforward pytest.mark.vcr, that reflects VCR.use_cassettes API;
  • Combining multiple VCR cassettes;
  • Network access blocking.

Usage

.. code:: python

import pytest
import requests

# cassettes/{module_name}/test_single.yaml will be used
@pytest.mark.vcr
def test_single():
    assert requests.get("http://httpbin.org/get").text == '{"get": true}'

# cassettes/{module_name}/example.yaml will be used
@pytest.mark.default_cassette("example.yaml")
@pytest.mark.vcr
def test_default():
    assert requests.get("http://httpbin.org/get").text == '{"get": true}'

# these cassettes will be used in addition to the default one
@pytest.mark.vcr("/path/to/ip.yaml", "/path/to/get.yaml")
def test_multiple():
    assert requests.get("http://httpbin.org/get").text == '{"get": true}'
    assert requests.get("http://httpbin.org/ip").text == '{"ip": true}'

Configuration


You can provide the recording configuration with the ``vcr_config`` fixture, which could be any scope - ``session``,
``package``, ``module``, or ``function``. It should return a dictionary that will be passed directly to ``VCR.use_cassettes``
under the hood.

.. code:: python

    import pytest

    @pytest.fixture(scope="module")
    def vcr_config():
        return {"filter_headers": ["authorization"]}

For more granular control you need to pass these keyword arguments to individual ``pytest.mark.vcr`` marks, and in this case
all arguments will be merged into a single dictionary with the following priority (low -> high):

- ``vcr_config`` fixture
- all marks from the most broad scope ("session") to the most narrow one ("function")

Example:

.. code:: python

    import pytest

    pytestmark = [pytest.mark.vcr(ignore_localhost=True)]

    @pytest.fixture(scope="module")
    def vcr_config():
        return {"filter_headers": ["authorization"]}

    @pytest.mark.vcr(filter_headers=[])
    def test_one():
        ...

    @pytest.mark.vcr(filter_query_parameters=["api_key"])
    def test_two():
        ...

Resulting VCR configs for each test:

- ``test_one`` - ``{"ignore_localhost": True, "filter_headers": []}``
- ``test_two`` - ``{"ignore_localhost": True, "filter_headers": ["authorization"], "filter_query_parameters": ["api_key"]}``

You can get access to the used ``VCR`` instance via ``pytest_recording_configure`` hook. It might be useful for registering
custom matchers, persisters, etc.:

.. code:: python

    # conftest.py

    def jurassic_matcher(r1, r2):
        assert r1.uri == r2.uri and "JURASSIC PARK" in r1.body, \
            "required string (JURASSIC PARK) not found in request body"

    def pytest_recording_configure(config, vcr):
        vcr.register_matcher("jurassic", jurassic_matcher)

You can disable the VCR.py integration entirely by passing the ``--disable-recording`` CLI option.

Rewrite record mode

It is possible to rewrite a cassette from scratch and not extend it with new entries as it works now with the all record mode from VCR.py.

However, it will rewrite only the default cassette and won't touch extra cassettes.

.. code:: python

import pytest

@pytest.fixture(scope="module")
def vcr_config():
    return {"record_mode": "rewrite"}

Or via command-line option:

.. code:: bash

$ pytest --record-mode=rewrite tests/

Blocking network access


To have more confidence that your tests will not go over the wire, you can block it with ``pytest.mark.block_network`` mark:

.. code:: python

    import pytest
    import requests

    @pytest.mark.block_network
    def test_multiple():
        assert requests.get("http://httpbin.org/get").text == '{"get": true}'

    ...
    # in case of access
    RuntimeError: Network is disabled

Besides marks, the network access could be blocked globally with ``--block-network`` command-line option.

However, if VCR.py recording is enabled, the network is not blocked for tests with ``pytest.mark.vcr``.

Example:

.. code:: python

    import pytest
    import requests

    @pytest.mark.vcr
    def test_multiple():
        assert requests.get("http://httpbin.org/get").text == '{"get": true}'

Run ``pytest``:

.. code:: bash

    $ pytest --record-mode=once --block-network tests/

The network blocking feature supports ``socket``-based transports and ``pycurl``.

It is possible to allow access to specified hosts during network blocking:

.. code:: python

    import pytest
    import requests

    @pytest.mark.block_network(allowed_hosts=["httpbin.*"])
    def test_access():
        assert requests.get("http://httpbin.org/get").text == '{"get": true}'
        with pytest.raises(RuntimeError, match=r"^Network is disabled$"):
            requests.get("http://example.com")

Or via command-line option:

.. code:: bash

    $ pytest --record-mode=once --block-network --allowed-hosts=httpbin.*,localhost tests/

Additional resources
--------------------

Looking for more examples? Check out `this article <https://code.kiwi.com/pytest-cassettes-forget-about-mocks-or-live-requests-a9336e1caee6>`_ about ``pytest-recording``.

Contributing
------------

To run the tests:

.. code:: bash

    $ tox -p all

For more information, take a look at `our contributing guide <https://github.com/kiwicom/pytest-recording/blob/master/CONTRIBUTING.rst>`_

Python support
--------------

Pytest-recording supports:

- CPython 3.5, 3.6, 3.7, 3.8 and 3.9.
- PyPy 7 (3.6)

License
-------

The code in this project is licensed under `MIT license`_. By contributing to ``pytest-recording``, you agree that your contributions will be licensed under its MIT license.


.. |codecov| image:: https://codecov.io/gh/kiwicom/pytest-recording/branch/master/graph/badge.svg
   :target: https://codecov.io/gh/kiwicom/pytest-recording
.. |Build| image:: https://travis-ci.org/kiwicom/pytest-recording.svg?branch=master
   :target: https://travis-ci.org/kiwicom/pytest-recording
.. |Version| image:: https://img.shields.io/pypi/v/pytest-recording.svg
   :target: https://pypi.org/project/pytest-recording/
.. |Python versions| image:: https://img.shields.io/pypi/pyversions/pytest-recording.svg
   :target: https://pypi.org/project/pytest-recording/
.. |License| image:: https://img.shields.io/pypi/l/pytest-recording.svg
   :target: https://opensource.org/licenses/MIT

.. _MIT license: https://opensource.org/licenses/MIT
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].