Skip to main content

Pytest fixtures for layab.

Project description

pytest fixtures and assertions functions for layab

pypi version Build status Coverage Code style: black Number of tests Number of downloads

pytest fixtures and utility functions that can be used to test layab based REST API.

Flask

Test client

You can have access to the pytest-flask client fixture for your layab based REST API.

Providing a service_module_name pytest fixture will give you access to a Flask test client and ensure SERVER_ENVIRONMENT environment variable will be set to test in order to load test specific configuration.

pytest-flask must be installed for the following sample to work:

import pytest
from pytest_layab.flask import app


@pytest.fixture
def service_module_name():
    # Considering main.py exists within a folder named my_module.
    # And main.py contains a variable named application containing the Flask app.
    return "my_module.main"


def test_get(client):
    # Perform a GET request on your application on /my_endpoint endpoint.
    response = client.get('/my_endpoint')

Helper functions

The following examples consider that you already have a test client.

Posting JSON
from pytest_layab.flask import post_json


def test_json_post(client):
    response = post_json(client, '/my_endpoint', {
        'my_key': 'my_value',
    })
Posting file
from pytest_layab.flask import post_file


def test_file_post(client):
    response = post_file(client, '/my_endpoint', 'file_name', 'file/path')
Putting JSON
from pytest_layab.flask import put_json


def test_json_put(client):
    response = put_json(client, '/my_endpoint', {
        'my_key': 'my_value',
    })
Checking HTTP 201 (CREATED) response

pytest_layab.flask.assert_201 function will ensure that the status code of the response is 201 and that the location header contains the expected relative route.

from pytest_layab.flask import assert_201


def test_created_response(client):
    response = None
    assert_201(response, '/my_new_location')
Checking response content

pytest_layab.flask.assert_file function will ensure that the response body will have the same content as in the provided file.

from pytest_layab.flask import assert_file


def test_with_content_in_a_file(client):
    response = None
    assert_file(response, 'path/to/file/with/expected/content')

Mocks

Date-Time

You can mock current date-time.

import datetime
import module_where_datetime_is_used


_date_time_for_tests = datetime.datetime(2018, 10, 11, 15, 5, 5, 663979)


class DateTimeModuleMock:
    class DateTimeMock(datetime.datetime):
        @classmethod
        def now(cls, tz=None):
            return _date_time_for_tests.replace(tzinfo=tz)

    class DateMock(datetime.date):
        @classmethod
        def today(cls):
            return _date_time_for_tests.date()

    timedelta = datetime.timedelta
    timezone = datetime.timezone
    datetime = DateTimeMock
    date = DateMock


def test_date_mock(monkeypatch):
    monkeypatch.setattr(module_where_datetime_is_used, "datetime", DateTimeModuleMock)

How to install

  1. python 3.6+ must be installed
  2. Use pip to install module:
python -m pip install pytest_layab

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_layab-2.0.0.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

pytest_layab-2.0.0-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file pytest_layab-2.0.0.tar.gz.

File metadata

  • Download URL: pytest_layab-2.0.0.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.7.1

File hashes

Hashes for pytest_layab-2.0.0.tar.gz
Algorithm Hash digest
SHA256 3ff93173b7dd6372fa080260d2f716449311b5241f47a159c57f25ebc2326d32
MD5 9426835fe719d3a3343f8604f3f63abe
BLAKE2b-256 44d2083c7a76bdb72f5d806cbb78fb23333308c3c80323cf3f0c6147fe2a4e79

See more details on using hashes here.

File details

Details for the file pytest_layab-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: pytest_layab-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.7.1

File hashes

Hashes for pytest_layab-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9490299d0bf7f7463cae4b673bdb017442e614517704ca9dcebd1df4ad1028d0
MD5 142f8c484a1b87368d8a017d0ff5f804
BLAKE2b-256 f6e04368c964a3d8504f571da70fe90ce3cf3ed2575040fec238ea447f440bfb

See more details on using hashes here.

Supported by

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