-
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]>
- Loading branch information
1 parent
8815be2
commit 15a3714
Showing
2 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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('<html><body>Hello, world!</body></html>') | ||
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) |