Skip to main content

Test Clietn for ASGI web applications

Project description

asgi-testClient

Travis (.org) PyPI - Python Version PyPI codecov PyPI - Downloads black

Testing ASGI applications made easy!

The why?

Why would you build this when all web frameworks come with one? Well, because mostly all those web frameworks have to build their own. I was building my own web framework perhaps (research & learning purpose) and got to the point where a needed a TestClient but then a asked my self why does anybody building web frameworks have to build their own TestClient when there's a standard?. Ok, then just install starlette a use it test client; would you install a library just to use a tiny part of it? This client does not have any dependencies.

Requirements

Python 3.6+

It should run on Python 3.5 but I haven' tested it.

Installation

pip install asgi-testclient

Usage

The client replicates the requests API, so if you have used request you should feel comfortable.

import pytest
from asgi_testclient import TestClient

from myapp import API

@pytest.fixture
def client():
    return TestClient(API)


@pytest.mark.asyncio
async def test_get(client):
    response = await client.get("/")
    assert response.json() == {"hello": "world"}
    assert response.status_code == 200

I have used pytest in this example but you can use whichever runner you prefer. Note: the client method are coroutines get, post, delete, put, patch, etc...

TODO:

  • Support Websockets client.
  • Cookies support.
  • Redirects.
  • Support files encoding
  • Stream request & response

Credits

  • Tom Christie: I brought inspiration from the starlette test client.
  • Kenneth ☤ Reitz: This package tries to replicate requests API.

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

asgi-testclient-0.1.0.tar.gz (6.0 kB view hashes)

Uploaded Source

Built Distribution

asgi_testclient-0.1.0-py3-none-any.whl (13.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