Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add testing for some async frameworks #63

Merged
merged 1 commit into from
Mar 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
56 changes: 56 additions & 0 deletions tests/test_async.py
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)