![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
.. image:: https://img.shields.io/pypi/v/pytest-mock-server.svg :target: https://pypi.org/project/pytest-mock-server :alt: PyPI version
.. image:: https://img.shields.io/pypi/pyversions/pytest-mock-server.svg :target: https://pypi.org/project/pytest-mock-server :alt: Python versions
.. image:: https://travis-ci.org/AndreyErmilov/pytest-mock-server.svg?branch=master :target: https://travis-ci.org/AndreyErmilov/pytest-mock-server :alt: See Build Status on Travis CI
Mock server plugin for pytest
You can install "pytest-mock-server" via pip
_ from PyPI
_::
$ pip install pytest-mock-server
One handler
.. code-block:: python
import pytest
import requests
@pytest.mark.server(url='/v1/books/', response=[{'id': 1}], method='GET')
def test_handler_responses():
response = requests.get('http://localhost:5000/v1/books/')
assert response.status_code == 200
assert response.json() == [{'id': 1}]
More than one handlers
.. code-block:: python
import pytest import requests
@pytest.mark.server(url='/v1/books/', response=[{'id': 1}], method='GET') @pytest.mark.server(url='/v1/books/<book_id>/', response={'id': 1}, method='GET') def test_handler_responses(): response = requests.get('http://localhost:5000/v1/books/') assert response.status_code == 200 assert response.json() == [{'id': 1}] response = requests.get('http://localhost:5000/v1/books/1/') assert response.status_code == 200 assert response.json() == {'id': 1}
Callback executes before response returns
.. code-block:: python
import pytest
import requests
import time
def sleep_two(*args, **kwargs):
time.sleep(2)
@pytest.mark.server(url='/v1/books/', response={}, callback=sleep_two)
def test_handler_responses():
"""Ensures Timeouts works"""
with pytest.raises(requests.exceptions.Timeout):
response = requests.get('http://localhost:5000/v1/books/', timeout=1)
Custom settings for server
.. code-block:: python
import pytest import requests
@pytest.mark.server(url='/v1/books/', response={}) @pytest.mark.server_settings(port=8000) def test_handler_responses(): response = requests.get('http://localhost:8000/v1/books/') assert response.status_code == 200 assert response.json() == {}
Contributions are very welcome. Tests can be run with tox
_, please ensure
the coverage at least stays the same before you submit a pull request.
Distributed under the terms of the MIT
_ license, "pytest-mock-server" is free and open source software
If you encounter any problems, please file an issue
_ along with a detailed description.
.. _MIT
: http://opensource.org/licenses/MIT
.. _file an issue
: https://github.com/AndreyErmilov/pytest-mock-server/issues
.. _tox
: https://tox.readthedocs.io/en/latest/
.. _pip
: https://pypi.org/project/pip/
.. _PyPI
: https://pypi.org/project
FAQs
Mock server plugin for pytest
We found that pytest-mock-server demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.