Skip to content

Commit

Permalink
Fix pytest-asyncio compatibility with decorators
Browse files Browse the repository at this point in the history
  • Loading branch information
sarayourfriend committed Nov 8, 2023
1 parent a946fa8 commit c201559
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 18 deletions.
5 changes: 5 additions & 0 deletions tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ def pook_on():
@pook.on does not mix with pytest marks. This works around
that limitation.
This also works around https://github.com/pytest-dev/pytest-asyncio/issues/403
which causes decorated async tests to skip unless explicitly marked,
which defeats the purpose of the auto discovery mode for `pytest-asyncio`
in our use case, as nearly all of our tests are decorated with `pook.on`.
"""
pook.on()
yield
Expand Down
11 changes: 6 additions & 5 deletions tests/unit/interceptors/aiohttp_test.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import sys
if sys.version_info >= (3, 7):
# Prevents import error when testing Python < 3.7
import aiohttp

import pook
import pytest

from pathlib import Path
if sys.version_info >= (3, 7):
# Prevents import error when testing Python < 3.7
import aiohttp
else:
pytestmark = pytest.mark.skip

from pathlib import Path

pytestmark = pytest.mark.asyncio

URL = "https://httpbin.org/status/404"

Expand Down
18 changes: 7 additions & 11 deletions tests/unit/interceptors/httpx_test.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
import sys
if sys.version_info >= (3, 7):
# Prevents import error when testing Python < 3.7
import httpx

import pook
import pytest

if sys.version_info >= (3, 7):
# Prevents import error when testing Python < 3.7
import httpx
else:
pytestmark = pytest.mark.skip

from itertools import zip_longest


URL = "https://httpbin.org/status/404"


@pytest.fixture
def pook_on():
pook.on()
yield
pook.off()


@pook.on
def test_sync():
mock = pook.get(URL).reply(200).body("123").mock
Expand All @@ -30,7 +26,6 @@ def test_sync():
assert len(mock.matches) == 1


@pytest.mark.asyncio
async def test_async(pook_on):
mock = pook.get(URL).reply(200).body(b"async_body", binary=True).mock

Expand Down Expand Up @@ -75,6 +70,7 @@ def test_streaming():
assert len(mock.matches) == 1


@pook.on
def test_redirect_following():
urls = [URL, f"{URL}/redirected", f"{URL}/redirected_again"]
mocks = []
Expand Down
5 changes: 3 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
[tox]
envlist = {py36,py37,py38,py39,py310,py311,pypy310}

[pytest]
asyncio_mode = auto

[testenv]
setenv =
PYTHONPATH = {toxinidir}
Expand All @@ -12,5 +15,3 @@ commands =

[testenv:py36]
requires = virtualenv<20.22.0
commands =
pytest -k 'not aiohttp and not httpx'

0 comments on commit c201559

Please sign in to comment.