Skip to main content

Mock server plugin for pytest

Project description

PyPI version Python versions See Build Status on Travis CI

Mock server plugin for pytest


Installation

You can install “pytest-mock-server” via pip from PyPI:

$ pip install pytest-mock-server

Usage

One handler

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

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

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

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() == {}

Contributing

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.

License

Distributed under the terms of the MIT license, “pytest-mock-server” is free and open source software

Issues

If you encounter any problems, please file an issue along with a detailed description.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest-mock-server-0.3.0.tar.gz (4.1 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

pytest_mock_server-0.3.0-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

pytest_mock_server-0.3.0-py2.py3-none-any.whl (4.4 kB view details)

Uploaded Python 2Python 3

File details

Details for the file pytest-mock-server-0.3.0.tar.gz.

File metadata

  • Download URL: pytest-mock-server-0.3.0.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for pytest-mock-server-0.3.0.tar.gz
Algorithm Hash digest
SHA256 ba62b3e1933c12408afa9525c82b9914398a8f923293b08545a12508774e03af
MD5 75490c298d3e705aa8a40a18207e1014
BLAKE2b-256 099a5dc3e93aa3ba8b01e219b52d1305b84bfa828d23a797b313fadc9a99d6e5

See more details on using hashes here.

File details

Details for the file pytest_mock_server-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: pytest_mock_server-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for pytest_mock_server-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b7a190ed614eb220f23c6962b8d5655706866f35a52f72099456282c7caa543a
MD5 ed5cca783925cc5f130967ab585acbc1
BLAKE2b-256 a639419b1caeda51d34f9db8a583b4fdcd50698cb9a8847567e9dac50d11589e

See more details on using hashes here.

File details

Details for the file pytest_mock_server-0.3.0-py2.py3-none-any.whl.

File metadata

  • Download URL: pytest_mock_server-0.3.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for pytest_mock_server-0.3.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 acddccc82c0fe3e3bb05cedc7dd946282f8d552cbe322adbeb2f4f3d9ffb7633
MD5 851a34333c772437d3e4d0f94e57742b
BLAKE2b-256 43961ddca06533c9bd770290b938236bc7762a005327951df6dc2434d1617e2d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page