Skip to main content

Pytest `client` fixture for the Falcon Framework

Project description

Pytest Falcon Client

Pytest client fixture for the Falcon Framework.

Build Status Coverage Status Code style: black Python versions PyPi

Installation

pip install pytest-falcon-client

Setup

Plugin provides make_client fixture that you can use as is by passing falcon.API instance as argument:

import pytest

from yout_application import create_api

@pytest.fixture
def api():
    return create_api()


def test_something(make_client, api):
    client = make_client(api)

    got = client.get('/your_url/42/')

    assert got == {'awesome': 'response'}

For simpler usage you can define your own client fixture:

import pytest

from yout_application import create_api

@pytest.fixture
def client(make_client):
    api = create_api()
    return make_client(api)


def test_something(client):
    got = client.get('/your_url/42/')

    assert got == {'awesome': 'response'}

Assertion examples

Get response body as json and do automatic assertions for status code

def test_something(client):
    got = client.get('/your_url/42/')

    assert got == {'some': 'staff'}

Get response body as json and do automatic assertions for your own status codes

def test_something(client):
    got = client.get('/your_url/42/', expected_statuses=[400, 401])

    assert got == {'some': 'stuff'}

Get response object as is and skip any automatic assertions

def test_something(client):
    response = client.get('/your_url/42/', as_response=True)

    assert response.status_code == 400
    assert response.json == {'some': 'stuff'}

Custom automatic assertions on every request

For example, you want to assert that every response has Access-Control-Allow-Origin header with value falconframework.org. You can do it with custom ApiTestClient like this:

import pytest

from pytest_falcon_client import ApiTestClient

from yout_application import create_api


class CustomApiTestClient(ApiTestClient):
    def response_assertions(self, response):
        # You can do any automatic assertions here for every request
        assert response.headers[
            'Access-Control-Allow-Origin'] == 'falconframework.org'


@pytest.fixture
def client():
    api = create_api()
    return CustomApiTestClient(api)


def test_something(client):
    response = client.get('/your_url/42/', as_response=True)

    assert response.status_code == 400
    assert response.json == {'some': 'stuff'}

Simulate some default client behaviour

What if you service depends on some default client behaviour, like headers, cookies or something else? You can setup this behaviour for every request with custom ApiTestClient:

import pytest

from pytest_falcon_client import ApiTestClient

from yout_application import create_api


class CustomApiTestClient(ApiTestClient):
    def prepare_request(self, method, expected_statuses, *args, **kwargs):
        kwargs['headers'] = {'Origin': 'falconframework.org'}  # add `ORIGIN` header to every request
        return args, kwargs, expected_statuses  # should returns all of this


@pytest.fixture
def client():
    api = create_api()
    return CustomApiTestClient(api)


def test_something(client):
    got = client.get('/your_url/42/', as_response=True)

    assert got == {'some': 'stuff'}

Look at more examples in tests.

Project details


Download files

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

Files for pytest-falcon-client, version 2.0.1
Filename, size File type Python version Upload date Hashes
Filename, size pytest-falcon-client-2.0.1.tar.gz (3.3 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page