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

Provide helper and mocks to ease test cases writing.

Service testing

You can have access to several REST API assertion functions

If you are using pytest you can import the following fixtures:

  • test_module_name (providing test module name, default to "test")
  • service_module_name (providing service module name, default to the folder containing server.py)
  • service_module (providing server, retrieved thanks to the service module name)
  • async_service_module (providing asynchronous_server)
  • app (providing flask app, default to application within service_module)

Sending a GET request

from pytest_layab import *


def test_get(client):
    response = client.get('/my_endpoint')

Posting JSON

from pytest_layab import *


def test_json_post(client):
    response = post_json(client, '/my_endpoint', {
        'my_key': 'my_value',
    })

Posting file

from pytest_layab import *


def test_file_post(client):
    response = post_file(client, '/my_endpoint', 'file_name', 'file/path')

Posting non JSON

from pytest_layab import *


def test_post(client):
    response = client.post('/my_endpoint', 'data to be sent')

Putting JSON

from pytest_layab import *


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

Putting non JSON

from pytest_layab import *


def test_put(client):
    response = client.put('/my_endpoint', 'data to be sent')

Sending a DELETE request

from pytest_layab import *


def test_delete(client):
    response = client.delete('/my_endpoint')

Checking response code

from pytest_layab import *


def test_200_ok(client):
    response = None
    assert response.status_code == 200

def test_201_created(client):
    response = None
    assert_201(response, '/my_new_location')

def test_202_accepted(client):
    response = None
    assert_202_regex(response, '/my_new_location/.*')

def test_204_no_content(client):
    response = None
    assert_204(response)

def test_303_see_other(client):
    response = None
    assert_303_regex(response, '/my_new_location/.*')

Checking response JSON

from pytest_layab import *


def test_json_exact_content(client):
    response = None
    assert response.json == {'expected_key': 'Expected 13 value'}

Checking response Text

import re

from pytest_layab import *


def test_text_exact_content(client):
    response = None
    assert response.get_data(as_text=True) == 'Expected 13 value'

def test_text_with_regular_expression(client):
    response = None
    assert re.match('Expected \d\d value', response.get_data(as_text=True))

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

Checking response bytes

from pytest_layab import *


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

Basic Assertions

from pytest_layab import *


def test_without_list_order():
    assert_items_equal({'expected_key': ['First value', 'Second value']}, {'expected_key': ['Second value', 'First value']})

Mocks

Date-Time

You can mock current date-time.

import module_where_datetime_is_used


class DateTimeMock:
    @staticmethod
    def utcnow():
        class UTCDateTimeMock:
            @staticmethod
            def isoformat():
                return "2018-10-11T15:05:05.663979"
        return UTCDateTimeMock


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

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-1.3.0.tar.gz (5.3 kB view hashes)

Uploaded Source

Built Distribution

pytest_layab-1.3.0-py3-none-any.whl (6.8 kB view hashes)

Uploaded Python 3

Supported by

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