Package: asgi-testclient
asgi-testClientTesting ASGI applications made easy!The why?Whywould 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 aTestClientbut then a asked my selfwhy does anybody building web frameworks have to build their own TestClient when there's a standard?. Ok, then just installstarlettea use it test client; would you install a library just to use a tiny part of it?This client does not have any dependencies.RequirementsPython 3.6+It should run on Python 3.5 but I haven' tested it.Installationpip install asgi-testclientUsageThe client replicates the requests API, so if you have used request you should feel comfortable.Note:the client method are coroutinesget, post, delete, put, patch, etc...importpytestfromasgi_testclientimportTestClientfrommyappimportAPI@pytest.fixturedefclient():returnTestClient(API)@pytest.mark.asyncioasyncdeftest_get(client):response=awaitclient.get("/")assertresponse.json()=={"hello":"world"}assertresponse.status_code==200I have usedpytestin this example but you can use whichever runner you prefer.If you still prefer simple functions to coroutines, you can use the sync interface:importpytestfromasgi_testclient.syncimportTestClient@pytest.fixturedefclient():returnTestClient(API)deftest_get(client):response=client.get("/")assertresponse.json()=={"hello":"world"}assertresponse.status_code==200Take in account that if you're running inside an async app you should use the async client, yet you can run the sync one inside threads is still desired.WebsocketsIf you're using ASGI you may be doing some web-sockets stuff. We have added support for it also, so you can test it easy.fromasgi_testclientimportTestClientfrommyappimportAPIasyncdeftest_send():echo_server=TestClient(API)websocket=awaitecho_server.ws_connect("/")formsgin["Hey","Echo","Back"]:awaitwebsocket.send_text(msg)data=awaitwebsocket.receive_text()assertdata==msgawaitwebsocket.close()asyncdeftest_ws_context():client=TestClient(API)asyncwithclient.ws_session("/")aswebsocket:data=awaitwebsocket.receive_text()assertdata=="Hello, world!"Few things to take in count here:When usingws_connectyou must callwebsocket.close()to finish up your APP task.For using websockets in context manager you must usews_sessioninstead ofws_connect.When waiting on server responsewebsocker.receive_*it may raise aWsDisconnect.And one more time for those who don't want to this async we got the sync version:pfromasgi_testclient.syncimportTestClientfrommyappimportAPIclient=TestClient(API)deftest_send_receive_json():websocket=client.ws_connect("/json")json_msg={"hello":"test"}websocket.send_json(json_msg)assertwebsocket.receive_json()==json_msgwebsocket.close()deftest_ws_context():withclient.ws_session("/")aswebsocket:data=websocket.receive_text()assertdata=="Hello, world!"Important:In the sync version you cannot usesendorreceivesince they're coroutines, instead use their childrensend_*orreceive_*text|bytes|json.Also sync version is done throwmonkey patchingso you can't use both versionasync & syncat the same time.TODO:Support Websockets client.Cookies support.Redirects.Support files encodingStream request & responseCreditsTom Christie: I brought inspiration from thestarlettetest client.Kenneth ☤ Reitz: This package tries to replicaterequestsAPI.