From ea63f6349ee71762b19115f4ec7f2e0fef214065 Mon Sep 17 00:00:00 2001 From: Mike Fiedler Date: Tue, 30 Mar 2021 17:18:52 -0400 Subject: [PATCH] test: add testing for some async frameworks While the basics of asyncio ought to be covered by enabling unix sockets in #54, I thought it might be nice to add some explicit asyncio tests to ensure that we don't hit any framework-specific regressions. I had written these locally when testing the changes anyhow. Refs: #6 Refs: #47 Signed-off-by: Mike Fiedler --- pyproject.toml | 2 ++ tests/test_async.py | 56 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 tests/test_async.py diff --git a/pyproject.toml b/pyproject.toml index 8f952d9..a8c94b2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -35,6 +35,8 @@ pytest = "^6.0" pytest-cov = "^2.8.1" pytest-httpbin = "^1.0" pytest-flake8 = "^1.0.4" +asynctest = "^0.13.0" +starlette = "^0.14.2" [tool.poetry.plugins.pytest11] socket = 'pytest_socket' diff --git a/tests/test_async.py b/tests/test_async.py new file mode 100644 index 0000000..963ba73 --- /dev/null +++ b/tests/test_async.py @@ -0,0 +1,56 @@ +import socket + +import pytest + + +unix_sockets_only = pytest.mark.skipif( + not hasattr(socket, "AF_UNIX"), + reason="Skip any platform that does not support AF_UNIX", +) + + +@unix_sockets_only +def test_asynctest(testdir): + testdir.makepyfile(""" + import asynctest + + + class AnExampleWithTestCaseAndCoroutines(asynctest.TestCase): + async def a_coroutine(self): + return "I worked" + + async def test_that_a_coroutine_runs(self): + self.assertIn("worked", await self.a_coroutine()) + + async def test_inet_is_blocked(self): + socket.socket(socket.AF_INET, socket.SOCK_STREAM) + """) + result = testdir.runpytest("--verbose", "--disable-socket", "--allow-unix-socket") + result.assert_outcomes(passed=1, skipped=0, failed=1) + + +@unix_sockets_only +def test_starlette(testdir): + testdir.makepyfile(""" + from pytest_socket import disable_socket + from starlette.responses import HTMLResponse + from starlette.testclient import TestClient + + + def pytest_runtest_setup(): + disable_socket() + + + async def app(scope, receive, send): + assert scope['type'] == 'http' + response = HTMLResponse('Hello, world!') + await response(scope, receive, send) + + + def test_app(): + client = TestClient(app) + response = client.get('/') + assert response.status_code == 200 + """) + result = testdir.runpytest("--verbose", "--disable-socket", "--allow-unix-socket") + result.assert_outcomes(passed=1, skipped=0, failed=0)