Pytest Falcon Client
Pytest client
fixture for the Falcon Framework.
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
.