Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sanic-org/sanic-testing
Test clients for Sanic
https://github.com/sanic-org/sanic-testing
hacktoberfest python sanic testing
Last synced: 30 days ago
JSON representation
Test clients for Sanic
- Host: GitHub
- URL: https://github.com/sanic-org/sanic-testing
- Owner: sanic-org
- License: mit
- Created: 2020-05-14T06:34:14.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2024-01-29T13:12:32.000Z (11 months ago)
- Last Synced: 2024-04-13T17:53:46.175Z (8 months ago)
- Topics: hacktoberfest, python, sanic, testing
- Language: Python
- Homepage: https://sanic.dev/en/plugins/sanic-testing/getting-started.html
- Size: 102 KB
- Stars: 31
- Watchers: 7
- Forks: 18
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-sanic - Sanic Testing
README
# 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 TestManagersanic_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
```