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 hashes)

Uploaded source

Built Distributions

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

Uploaded py2 py3

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page