{"id":15067210,"url":"https://github.com/colin-b/pytest_layab","last_synced_at":"2025-10-05T04:31:42.364Z","repository":{"id":62584100,"uuid":"225420380","full_name":"Colin-b/pytest_layab","owner":"Colin-b","description":"pytest fixtures and assertion functions for layab","archived":true,"fork":false,"pushed_at":"2020-10-05T14:44:43.000Z","size":188,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"develop","last_synced_at":"2025-01-17T05:36:23.660Z","etag":null,"topics":["layab","pytest"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Colin-b.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-12-02T16:30:44.000Z","updated_at":"2024-02-18T19:21:22.000Z","dependencies_parsed_at":"2022-11-03T22:17:26.029Z","dependency_job_id":null,"html_url":"https://github.com/Colin-b/pytest_layab","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Colin-b%2Fpytest_layab","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Colin-b%2Fpytest_layab/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Colin-b%2Fpytest_layab/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Colin-b%2Fpytest_layab/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Colin-b","download_url":"https://codeload.github.com/Colin-b/pytest_layab/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":235364870,"owners_count":18978255,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["layab","pytest"],"created_at":"2024-09-25T01:18:13.209Z","updated_at":"2025-10-05T04:31:42.037Z","avatar_url":"https://github.com/Colin-b.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003ch2 align=\"center\"\u003epytest fixtures and assertions functions for layab\u003c/h2\u003e\n\n\u003cp align=\"center\"\u003e\n\u003ca href=\"https://pypi.org/project/pytest-layab/\"\u003e\u003cimg alt=\"pypi version\" src=\"https://img.shields.io/pypi/v/pytest-layab\"\u003e\u003c/a\u003e\n\u003ca href=\"https://travis-ci.com/Colin-b/pytest_layab\"\u003e\u003cimg alt=\"Build status\" src=\"https://api.travis-ci.com/Colin-b/pytest_layab.svg?branch=master\"\u003e\u003c/a\u003e\n\u003ca href=\"https://travis-ci.com/Colin-b/pytest_layab\"\u003e\u003cimg alt=\"Coverage\" src=\"https://img.shields.io/badge/coverage-100%25-brightgreen\"\u003e\u003c/a\u003e\n\u003ca href=\"https://github.com/psf/black\"\u003e\u003cimg alt=\"Code style: black\" src=\"https://img.shields.io/badge/code%20style-black-000000.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://travis-ci.com/Colin-b/pytest_layab\"\u003e\u003cimg alt=\"Number of tests\" src=\"https://img.shields.io/badge/tests-18 passed-blue\"\u003e\u003c/a\u003e\n\u003ca href=\"https://pypi.org/project/pytest-layab/\"\u003e\u003cimg alt=\"Number of downloads\" src=\"https://img.shields.io/pypi/dm/pytest-layab\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n`pytest` fixtures and utility functions that can be used to test [`layab`](https://github.com/Colin-b/layab) based REST API.\n\n- [Flask based API](#flask)\n  - [Test client](#test-client)\n  - [Sending JSON (as POST)](#posting-json)\n  - [Sending file (as POST)](#posting-file)\n  - [Sending JSON (as PUT)](#putting-json)\n  - [Assert CREATED response](#checking-http-201-created-response)\n  - [Compare response content to a file](#checking-response-content)\n \n### Flask\n\n#### Test client\n\nYou can have access to the `pytest-flask` `client` fixture for your [`layab`](https://github.com/Colin-b/layab) based REST API.\n\nProviding a `service_module_name` `pytest` fixture will give you access to a [Flask test client](https://pytest-flask.readthedocs.io/en/latest/index.html) and ensure `SERVER_ENVIRONMENT` environment variable will be set to `test` in order to load test specific configuration.\n\n`pytest-flask` must be installed for the following sample to work:\n\n```python\nimport pytest\nfrom pytest_layab.flask import app\n\n\n@pytest.fixture\ndef service_module_name():\n    # Considering main.py exists within a folder named my_module.\n    # And main.py contains a variable named application containing the Flask app.\n    return \"my_module.main\"\n\n\ndef test_get(client):\n    # Perform a GET request on your application on /my_endpoint endpoint.\n    response = client.get('/my_endpoint')\n```\n\n#### Helper functions\n\nThe following examples consider that you already have a [test client](#test-client).\n\n##### Posting JSON\n\n```python\nfrom pytest_layab.flask import post_json\n\n\ndef test_json_post(client):\n    response = post_json(client, '/my_endpoint', {\n        'my_key': 'my_value',\n    })\n```\n\n##### Posting file\n\n```python\nfrom pytest_layab.flask import post_file\n\n\ndef test_file_post(client):\n    response = post_file(client, '/my_endpoint', 'file_name', 'file/path')\n```\n\n##### Putting JSON\n\n```python\nfrom pytest_layab.flask import put_json\n\n\ndef test_json_put(client):\n    response = put_json(client, '/my_endpoint', {\n        'my_key': 'my_value',\n    })\n```\n\n##### Checking HTTP 201 (CREATED) response\n\n`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.\n\n```python\nfrom pytest_layab.flask import assert_201\n\n\ndef test_created_response(client):\n    response = None\n    assert_201(response, '/my_new_location')\n```\n\n##### Checking response content\n\n`pytest_layab.flask.assert_file` function will ensure that the response body will have the same content as in the provided file.\n\n```python\nfrom pytest_layab.flask import assert_file\n\n\ndef test_with_content_in_a_file(client):\n    response = None\n    assert_file(response, 'path/to/file/with/expected/content')\n```\n\n## Mocks\n\n### Date-Time\n\nYou can mock current date-time.\n\n```python\nimport datetime\nimport module_where_datetime_is_used\n\n\n_date_time_for_tests = datetime.datetime(2018, 10, 11, 15, 5, 5, 663979)\n\n\nclass DateTimeModuleMock:\n    class DateTimeMock(datetime.datetime):\n        @classmethod\n        def now(cls, tz=None):\n            return _date_time_for_tests.replace(tzinfo=tz)\n    \n    class DateMock(datetime.date):\n        @classmethod\n        def today(cls):\n            return _date_time_for_tests.date()\n\n    timedelta = datetime.timedelta\n    timezone = datetime.timezone\n    datetime = DateTimeMock\n    date = DateMock\n\n\ndef test_date_mock(monkeypatch):\n    monkeypatch.setattr(module_where_datetime_is_used, \"datetime\", DateTimeModuleMock)\n```\n\n## How to install\n1. [python 3.6+](https://www.python.org/downloads/) must be installed\n2. Use pip to install module:\n```sh\npython -m pip install pytest_layab\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcolin-b%2Fpytest_layab","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcolin-b%2Fpytest_layab","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcolin-b%2Fpytest_layab/lists"}