sanic-testing


Namesanic-testing JSON
Version 23.12.0 PyPI version JSON
download
home_pagehttps://github.com/sanic-org/sanic-testing/
SummaryCore testing clients for Sanic
upload_time2023-12-31 10:14:41
maintainer
docs_urlNone
authorAdam Hopkins
requires_python
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Sanic Core Test

This package is meant to be the core testing utility and clients for testing Sanic applications. It is mainly derived from `sanic.testing` which has (or will be) removed from the main Sanic repository in the future.

[Documentation](https://sanicframework.org/en/plugins/sanic-testing/getting-started.html)

## Getting Started

    pip install sanic-testing

The package is meant to create an almost seemless transition. Therefore, after loading the package, it will attach itself to your Sanic instance and insert test clients.

```python
from sanic import Sanic
from sanic_testing import TestManager

sanic_app = Sanic(__name__)
TestManager(sanic_app)
```

This will provide access to both the sync (`sanic.test_client`) and async (`sanic.asgi_client`) clients. Both of these clients are also available directly on the `TestManager` instance.

## Writing a sync test

Testing should be pretty much the same as when the test client was inside Sanic core. The difference is just that you need to run `TestManager`.

```python
import pytest

@pytest.fixture
def app():
    sanic_app = Sanic(__name__)
    TestManager(sanic_app)

    @sanic_app.get("/")
    def basic(request):
        return response.text("foo")

    return sanic_app

def test_basic_test_client(app):
    request, response = app.test_client.get("/")

    assert response.body == b"foo"
    assert response.status == 200
```

## Writing an async test

Testing of an async method is best done with `pytest-asyncio` installed. Again, the following test should look familiar to anyone that has used `asgi_client` in the Sanic core package before.

The main benefit of using the `asgi_client` is that it is able to reach inside your application, and execute your handlers without ever having to stand up a server or make a network call.

```python
import pytest

@pytest.fixture
def app():
    sanic_app = Sanic(__name__)
    TestManager(sanic_app)

    @sanic_app.get("/")
    def basic(request):
        return response.text("foo")

    return sanic_app

@pytest.mark.asyncio
async def test_basic_asgi_client(app):
    request, response = await app.asgi_client.get("/")

    assert response.body == b"foo"
    assert response.status == 200
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/sanic-org/sanic-testing/",
    "name": "sanic-testing",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "",
    "author": "Adam Hopkins",
    "author_email": "admhpkns@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/db/a0/379a5083a1289795697101f722703a26e9a119c2648a128b85d2eb6cf19a/sanic-testing-23.12.0.tar.gz",
    "platform": "any",
    "description": "# Sanic Core Test\n\nThis package is meant to be the core testing utility and clients for testing Sanic applications. It is mainly derived from `sanic.testing` which has (or will be) removed from the main Sanic repository in the future.\n\n[Documentation](https://sanicframework.org/en/plugins/sanic-testing/getting-started.html)\n\n## Getting Started\n\n    pip install sanic-testing\n\nThe package is meant to create an almost seemless transition. Therefore, after loading the package, it will attach itself to your Sanic instance and insert test clients.\n\n```python\nfrom sanic import Sanic\nfrom sanic_testing import TestManager\n\nsanic_app = Sanic(__name__)\nTestManager(sanic_app)\n```\n\nThis will provide access to both the sync (`sanic.test_client`) and async (`sanic.asgi_client`) clients. Both of these clients are also available directly on the `TestManager` instance.\n\n## Writing a sync test\n\nTesting should be pretty much the same as when the test client was inside Sanic core. The difference is just that you need to run `TestManager`.\n\n```python\nimport pytest\n\n@pytest.fixture\ndef app():\n    sanic_app = Sanic(__name__)\n    TestManager(sanic_app)\n\n    @sanic_app.get(\"/\")\n    def basic(request):\n        return response.text(\"foo\")\n\n    return sanic_app\n\ndef test_basic_test_client(app):\n    request, response = app.test_client.get(\"/\")\n\n    assert response.body == b\"foo\"\n    assert response.status == 200\n```\n\n## Writing an async test\n\nTesting of an async method is best done with `pytest-asyncio` installed. Again, the following test should look familiar to anyone that has used `asgi_client` in the Sanic core package before.\n\nThe main benefit of using the `asgi_client` is that it is able to reach inside your application, and execute your handlers without ever having to stand up a server or make a network call.\n\n```python\nimport pytest\n\n@pytest.fixture\ndef app():\n    sanic_app = Sanic(__name__)\n    TestManager(sanic_app)\n\n    @sanic_app.get(\"/\")\n    def basic(request):\n        return response.text(\"foo\")\n\n    return sanic_app\n\n@pytest.mark.asyncio\nasync def test_basic_asgi_client(app):\n    request, response = await app.asgi_client.get(\"/\")\n\n    assert response.body == b\"foo\"\n    assert response.status == 200\n```\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Core testing clients for Sanic",
    "version": "23.12.0",
    "project_urls": {
        "Homepage": "https://github.com/sanic-org/sanic-testing/"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "6a347914665b25f02e16f70d6f6120c90e262a61f6150e1ce8f226cc9625ab38",
                "md5": "393cb0a04a326f0d47880a6a1b033476",
                "sha256": "d809911fca49cba93e1df9de5c6ab8d95d91bdc03b18ba8a25b4e0b66c4e4c73"
            },
            "downloads": -1,
            "filename": "sanic_testing-23.12.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "393cb0a04a326f0d47880a6a1b033476",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 10342,
            "upload_time": "2023-12-31T10:14:40",
            "upload_time_iso_8601": "2023-12-31T10:14:40.483118Z",
            "url": "https://files.pythonhosted.org/packages/6a/34/7914665b25f02e16f70d6f6120c90e262a61f6150e1ce8f226cc9625ab38/sanic_testing-23.12.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "dba0379a5083a1289795697101f722703a26e9a119c2648a128b85d2eb6cf19a",
                "md5": "e63c281edd1953d79429fef77d678b5c",
                "sha256": "2b9c52b7314b7e1807958f41581e18b8254c5161c953e70fcf492e0dd2fe133f"
            },
            "downloads": -1,
            "filename": "sanic-testing-23.12.0.tar.gz",
            "has_sig": false,
            "md5_digest": "e63c281edd1953d79429fef77d678b5c",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 10675,
            "upload_time": "2023-12-31T10:14:41",
            "upload_time_iso_8601": "2023-12-31T10:14:41.571910Z",
            "url": "https://files.pythonhosted.org/packages/db/a0/379a5083a1289795697101f722703a26e9a119c2648a128b85d2eb6cf19a/sanic-testing-23.12.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-12-31 10:14:41",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "sanic-org",
    "github_project": "sanic-testing",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "tox": true,
    "lcname": "sanic-testing"
}
        
Elapsed time: 0.16157s