{"id":13705506,"url":"https://github.com/yunstanford/pytest-sanic","last_synced_at":"2025-10-08T15:15:49.029Z","repository":{"id":41413205,"uuid":"94911313","full_name":"yunstanford/pytest-sanic","owner":"yunstanford","description":"a Pytest Plugin for Sanic.","archived":false,"fork":false,"pushed_at":"2022-07-04T09:49:54.000Z","size":188,"stargazers_count":135,"open_issues_count":11,"forks_count":21,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-10-01T13:49:38.281Z","etag":null,"topics":["asyncio","pytest","pytest-plugin","python3","sanic"],"latest_commit_sha":null,"homepage":"http://pytest-sanic.readthedocs.io/en/latest","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yunstanford.png","metadata":{"files":{"readme":"README.rst","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-06-20T16:15:55.000Z","updated_at":"2024-10-02T04:41:14.000Z","dependencies_parsed_at":"2022-09-21T08:12:58.372Z","dependency_job_id":null,"html_url":"https://github.com/yunstanford/pytest-sanic","commit_stats":null,"previous_names":[],"tags_count":23,"template":false,"template_full_name":null,"purl":"pkg:github/yunstanford/pytest-sanic","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yunstanford%2Fpytest-sanic","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yunstanford%2Fpytest-sanic/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yunstanford%2Fpytest-sanic/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yunstanford%2Fpytest-sanic/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yunstanford","download_url":"https://codeload.github.com/yunstanford/pytest-sanic/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yunstanford%2Fpytest-sanic/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278965538,"owners_count":26076922,"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","status":"online","status_checked_at":"2025-10-08T02:00:06.501Z","response_time":56,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["asyncio","pytest","pytest-plugin","python3","sanic"],"created_at":"2024-08-02T22:00:42.677Z","updated_at":"2025-10-08T15:15:48.991Z","avatar_url":"https://github.com/yunstanford.png","language":"Python","funding_links":[],"categories":["Extensions","Python"],"sub_categories":["Development"],"readme":"pytest-sanic\n============\n\n.. start-badges\n\n.. list-table::\n    :stub-columns: 1\n\n    * - Build\n      - | |travis|\n    * - Docs\n      - |docs|\n    * - Package\n      - | |version| |wheel| |supported-versions| |supported-implementations|\n\n.. |travis| image:: https://travis-ci.org/yunstanford/pytest-sanic.svg?branch=master\n    :alt: Travis-CI Build Status\n    :target: https://travis-ci.org/yunstanford/pytest-sanic\n\n.. |docs| image:: https://readthedocs.org/projects/pytest-sanic/badge/?style=flat\n    :target: https://readthedocs.org/projects/pytest-sanic\n    :alt: Documentation Status\n\n.. |version| image:: https://img.shields.io/pypi/v/pytest-sanic.svg\n    :alt: PyPI Package latest release\n    :target: https://pypi.python.org/pypi/pytest-sanic\n\n.. |wheel| image:: https://img.shields.io/pypi/wheel/pytest-sanic.svg\n    :alt: PyPI Wheel\n    :target: https://pypi.python.org/pypi/pytest-sanic\n\n.. |supported-versions| image:: https://img.shields.io/pypi/pyversions/pytest-sanic.svg\n    :alt: Supported versions\n    :target: https://pypi.python.org/pypi/pytest-sanic\n\n.. |supported-implementations| image:: https://img.shields.io/pypi/implementation/pytest-sanic.svg\n    :alt: Supported implementations\n    :target: https://pypi.python.org/pypi/pytest-sanic\n\n.. end-badges\n\nA pytest plugin for `Sanic \u003chttp://sanic.readthedocs.io/en/latest/\u003e`_. It helps you to test your code asynchronously.\n\nThis plugin provides:\n\n* very easy testing with async coroutines\n* common and useful fixtures\n* asynchronous fixture support\n* test_client/sanic_client for Sanic application\n* test_server for Sanic application\n\n\nYou can find out more here:\n\nhttp://pytest-sanic.readthedocs.io/en/latest/\n\n\nReleases and change logs can be found here:\n\nhttps://github.com/yunstanford/pytest-sanic/releases\n\n\n-------\nInstall\n-------\n\n.. code::\n\n    pip install pytest-sanic\n\n\n-----------\nQuick Start\n-----------\n\nYou don't have to load ``pytest-sanic`` explicitly. ``pytest`` will do it for you.\n\nYou can set up a fixture for your ``app`` like this:\n\n.. code-block:: python\n\n    import pytest\n    from .app import create_app\n\n    @pytest.yield_fixture\n    def app():\n        app = create_app(test_config, **params)\n        yield app\n\nThis ``app`` fixture can then be used from tests:\n\n.. code-block:: python\n\n    async def test_sanic_db_find_by_id(app):\n        \"\"\"\n        Let's assume that, in db we have,\n            {\n                \"id\": \"123\",\n                \"name\": \"Kobe Bryant\",\n                \"team\": \"Lakers\",\n            }\n        \"\"\"\n        doc = await app.db[\"players\"].find_by_id(\"123\")\n        assert doc.name == \"Kobe Bryant\"\n        assert doc.team == \"Lakers\"\n\nTo send requests to your ``app``, you set up a client fixture using the loop_ and sanic_client_ fixtures:\n\n.. code-block:: python\n\n    @pytest.fixture\n    def test_cli(loop, app, sanic_client):\n        return loop.run_until_complete(sanic_client(app))\n\nThis ``test_cli`` fixture can then be used to send requests to your ``app``:\n\n.. code-block:: python\n\n    async def test_index(test_cli):\n        resp = await test_cli.get('/')\n        assert resp.status_code == 200\n\n    async def test_player(test_cli):\n        resp = await test_cli.get('/player')\n        assert resp.status_code == 200\n\n--------------------\nasynchronous fixture\n--------------------\n\n``pytest-sanic`` also supports asynchronous fixtures, just writes them like common pytest fixtures.\n\n.. code-block:: python\n\n    @pytest.fixture\n    async def async_fixture_sleep():\n        await asyncio.sleep(0.1)\n        return \"sleep...\"\n\n\n--------\nFixtures\n--------\n\nSome fixtures for easy testing.\n\n``loop``\n~~~~~~~~\n\n``pytest-sanic`` creates an event loop and injects it as a fixture. ``pytest`` will use this event loop to run your ``async tests``.\nBy default, fixture ``loop`` is an instance of `asyncio.new_event_loop`. But `uvloop` is also an option for you, by simpy passing\n``--loop uvloop``. Keep mind to just use one single event loop.\n\n\n``unused_port``\n~~~~~~~~~~~~~~~\n\nan unused TCP port on the localhost.\n\n\n``test_server``\n~~~~~~~~~~~~~~~\n\nCreates a TestServer instance by giving a ``Sanic`` application. It's very easy to utilize ``test_server`` to create your `Sanic`\napplication server for testing.\n\n.. code-block:: python\n\n    @pytest.yield_fixture\n    def app():\n        app = Sanic(\"test_sanic_app\")\n\n        @app.route(\"/test_get\", methods=['GET'])\n        async def test_get(request):\n            return response.json({\"GET\": True})\n\n        yield app\n\n    @pytest.fixture\n    def sanic_server(loop, app, test_server):\n        return loop.run_until_complete(test_server(app))\n\nYou can also very easily override this ``loop`` fixture by creating your own, simply like,\n\n.. code-block:: python\n\n    @pytest.yield_fixture\n    def loop():\n        loop = MyEventLoop()\n        yield loop\n        loop.close()\n\n``test_client``\n~~~~~~~~~~~~~~~\n\n``test_client`` has been deprecated, please use `sanic_client` instead, check out `issue \u003chttps://github.com/yunstanford/pytest-sanic/issues/22\u003e`_ for more context.\n\n\n``sanic_client``\n~~~~~~~~~~~~~~~~\n\nCreates a TestClient instance by giving a ``Sanic`` application. You can simply have a client by using ``sanic_client``, like\n\n.. code-block:: python\n\n    @pytest.yield_fixture\n    def app():\n        app = Sanic(\"test_sanic_app\")\n\n        @app.route(\"/test_get\", methods=['GET'])\n        async def test_get(request):\n            return response.json({\"GET\": True})\n\n        @app.route(\"/test_post\", methods=['POST'])\n        async def test_post(request):\n            return response.json({\"POST\": True})\n\n        @app.route(\"/test_put\", methods=['PUT'])\n        async def test_put(request):\n            return response.json({\"PUT\": True})\n\n        @app.route(\"/test_delete\", methods=['DELETE'])\n        async def test_delete(request):\n            return response.json({\"DELETE\": True})\n\n        @app.route(\"/test_patch\", methods=['PATCH'])\n        async def test_patch(request):\n            return response.json({\"PATCH\": True})\n\n        @app.route(\"/test_options\", methods=['OPTIONS'])\n        async def test_options(request):\n            return response.json({\"OPTIONS\": True})\n\n        @app.route(\"/test_head\", methods=['HEAD'])\n        async def test_head(request):\n            return response.json({\"HEAD\": True})\n\n        @app.websocket(\"/test_ws\")\n        async def test_ws(request, ws):\n            data = await ws.recv()\n            await ws.send(data)\n\n        yield app\n\n    @pytest.fixture\n    def test_cli(loop, app, sanic_client):\n        return loop.run_until_complete(sanic_client(app, protocol=WebSocketProtocol))\n\n    #########\n    # Tests #\n    #########\n\n    async def test_fixture_test_client_get(test_cli):\n        \"\"\"\n        GET request\n        \"\"\"\n        resp = await test_cli.get('/test_get')\n        assert resp.status_code == 200\n        resp_json = resp.json()\n        assert resp_json == {\"GET\": True}\n\n    async def test_fixture_test_client_post(test_cli):\n        \"\"\"\n        POST request\n        \"\"\"\n        resp = await test_cli.post('/test_post')\n        assert resp.status_code == 200\n        resp_json = resp.json()\n        assert resp_json == {\"POST\": True}\n\n    async def test_fixture_test_client_put(test_cli):\n        \"\"\"\n        PUT request\n        \"\"\"\n        resp = await test_cli.put('/test_put')\n        assert resp.status_code == 200\n        resp_json = resp.json()\n        assert resp_json == {\"PUT\": True}\n\n    async def test_fixture_test_client_delete(test_cli):\n        \"\"\"\n        DELETE request\n        \"\"\"\n        resp = await test_cli.delete('/test_delete')\n        assert resp.status_code == 200\n        resp_json = resp.json()\n        assert resp_json == {\"DELETE\": True}\n\n    async def test_fixture_test_client_patch(test_cli):\n        \"\"\"\n        PATCH request\n        \"\"\"\n        resp = await test_cli.patch('/test_patch')\n        assert resp.status_code == 200\n        resp_json = resp.json()\n        assert resp_json == {\"PATCH\": True}\n\n    async def test_fixture_test_client_options(test_cli):\n        \"\"\"\n        OPTIONS request\n        \"\"\"\n        resp = await test_cli.options('/test_options')\n        assert resp.status_code == 200\n        resp_json = resp.json()\n        assert resp_json == {\"OPTIONS\": True}\n\n    async def test_fixture_test_client_head(test_cli):\n        \"\"\"\n        HEAD request\n        \"\"\"\n        resp = await test_cli.head('/test_head')\n        assert resp.status_code == 200\n        resp_json = resp.json()\n        # HEAD should not have body\n        assert resp_json is None\n\n    async def test_fixture_test_client_ws(test_cli):\n        \"\"\"\n        Websockets\n        \"\"\"\n        ws_conn = await test_cli.ws_connect('/test_ws')\n        data = 'hello world!'\n        await ws_conn.send(data)\n        msg = await ws_conn.recv()\n        assert msg == data\n        await ws_conn.close()\n\n\nsmall notes:\n\n``test_cli.ws_connect`` does not work in ``sanic.__version__ \u003c= '0.5.4'``, because of a Sanic bug, but it\nhas been fixed in master branch. And ``websockets.__version__ \u003e= '4.0'`` has broken websockets in ``sanic.__version__ \u003c= '0.6.0'``, but it has been fixed in `master \u003chttps://github.com/channelcat/sanic/commit/bca1e084116335fd939c2ee226070f0428cd5de8\u003e`_.\n\n\n----\nTips\n----\n\n* `Blueprints Testing \u003chttps://github.com/yunstanford/pytest-sanic/issues/3\u003e`_\n* ``test_cli.ws_connect`` does not work in ``sanic.__version__ \u003c= '0.5.4'``, because of a Sanic bug, but it has been fixed in master branch.\n* `Importing app has loop already running \u003chttps://github.com/yunstanford/pytest-sanic/issues/1\u003e`_ when you have `db_init` listeners.\n* `Incorrect coverage report \u003chttps://github.com/pytest-dev/pytest-cov/issues/117\u003e`_ with ``pytest-cov``, but we can have workarounds for this issue, it's a pytest loading plugin problem essentially.\n* Websockets \u003e 4.0 has broken websockets in ``sanic.__version__ \u003c= '0.6.0'``, but it has been fixed in `this commit \u003chttps://github.com/channelcat/sanic/commit/bca1e084116335fd939c2ee226070f0428cd5de8\u003e`_\n\n\nFeel free to create issue if you have any question. You can also check out `closed issues \u003chttps://github.com/yunstanford/pytest-sanic/issues?q=is%3Aclosed\u003e`_\n\n\n-----------\nDevelopment\n-----------\n\n``pytest-sanic`` accepts contributions on GitHub, in the form of issues or pull requests.\n\n\nBuild.\n\n.. code::\n\n    poetry install\n\n\nRun unit tests.\n\n.. code::\n\n    poetry run pytest ./tests --cov pytest_sanic\n\n\n---------\nReference\n---------\n\nSome useful pytest plugins:\n\n* `pytest-tornado \u003chttps://github.com/eugeniy/pytest-tornado\u003e`_\n* `pytest-asyncio \u003chttps://github.com/pytest-dev/pytest-asyncio\u003e`_\n* `pytest-aiohttp \u003chttps://github.com/aio-libs/pytest-aiohttp\u003e`_\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyunstanford%2Fpytest-sanic","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyunstanford%2Fpytest-sanic","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyunstanford%2Fpytest-sanic/lists"}