Skip to content

Commit 5e7c41b

Browse files
authored
Switch away from pytest-asyncio (#25)
1 parent 0e55eab commit 5e7c41b

File tree

6 files changed

+17
-21
lines changed

6 files changed

+17
-21
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,16 @@ The `pytest_jupyter.jupyter_client` plugin provides an installed
4040
that provides a factory function that starts a kernel using the `echo` kernel
4141
by default.
4242

43-
Note: The server plugin also includes the client plugin, so you can use both
43+
*Note*: The server plugin also includes the client plugin, so you can use both
4444
sets of fixtures with `"pytest_jupyter.jupyter_server"`. Both the `client`
4545
and `server` plugins also include the core fixtures.
4646

47+
*Note*: The client and server plugins use `pytest-tornasync` for async
48+
test suite running. It may not compatible with `pytest-asyncio`, meaning
49+
that all fixtures must be synchronous. You can use the `asyncio_loop` fixture
50+
and run `asyncio_loop.run_until_complete` against an async function in your
51+
fixtures if needed.
52+
4753
The server fixures use the echo kernel by default. To override this behavior,
4854
override the `jp_server_config` fixture and add the following config:
4955

pyproject.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ classifiers = [
2424
]
2525
dependencies = [
2626
"pytest",
27-
"pytest-asyncio",
2827
"jupyter_core"
2928
]
3029
requires-python = ">=3.8"
@@ -39,12 +38,12 @@ docs = [
3938
client = [
4039
"jupyter_client>=7.4.0",
4140
"ipykernel>=6.14",
41+
"pytest-tornasync>=0.6"
4242
]
4343
server = [
4444
"jupyter_server>=1.21",
4545
"nbformat>=5.3",
4646
"pytest-jupyter[client]",
47-
"pytest-tornasync>=0.6"
4847
]
4948
test = [
5049
"pytest-timeout"
@@ -89,7 +88,6 @@ addopts = "-raXs --durations 10 --color=yes --doctest-modules"
8988
testpaths = [
9089
"tests"
9190
]
92-
asyncio_mode = "strict"
9391
timeout = 10
9492
# Restore this setting to debug failures
9593
timeout_method = "thread"

pytest_jupyter/jupyter_client.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from pathlib import Path
88

99
import pytest
10-
import pytest_asyncio
1110
from jupyter_core import paths
1211

1312
try:
@@ -56,8 +55,8 @@ def zmq_context():
5655
ctx.term()
5756

5857

59-
@pytest_asyncio.fixture
60-
async def start_kernel(echo_kernel_spec):
58+
@pytest.fixture
59+
def start_kernel(echo_kernel_spec, asyncio_loop):
6160
kms = []
6261
kcs = []
6362

@@ -73,7 +72,7 @@ async def inner(kernel_name="echo", **kwargs):
7372
kc.stop_channels()
7473

7574
for km in kms:
76-
await km.shutdown_kernel(now=True)
75+
asyncio_loop.run_until_complete(km.shutdown_kernel(now=True))
7776
assert km.context.closed
7877

7978

pytest_jupyter/jupyter_server.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
import jupyter_core.paths
1616
import pytest
17-
import pytest_asyncio
1817

1918
# The try block is needed so that the documentation can
2019
# still build without needed to install all the dependencies.
@@ -52,9 +51,12 @@
5251
pytest_plugins = ["pytest_tornasync", "pytest_jupyter.jupyter_client"]
5352

5453

55-
@pytest_asyncio.fixture
56-
async def asyncio_loop():
57-
return asyncio.get_running_loop()
54+
@pytest.fixture
55+
def asyncio_loop():
56+
loop = asyncio.new_event_loop()
57+
asyncio.set_event_loop(loop)
58+
yield loop
59+
loop.close()
5860

5961

6062
@pytest.fixture(autouse=True)

tests/test_jupyter_client.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from unittest.mock import Mock
22

3-
import pytest
43
from jupyter_client.session import Session
54

65
from pytest_jupyter.echo_kernel import EchoKernel
@@ -10,7 +9,6 @@ def test_zmq_context(zmq_context):
109
assert isinstance(zmq_context.underlying, int)
1110

1211

13-
@pytest.mark.asyncio
1412
async def test_start_kernel(start_kernel):
1513
km, kc = await start_kernel()
1614
assert km.kernel_name == "echo"

tests/test_jupyter_server.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,25 @@
22
import os
33
from unittest.mock import MagicMock
44

5-
import pytest
65
from jupyter_server.auth import Authorizer
76
from jupyter_server.serverapp import ServerApp
87
from tornado.websocket import WebSocketHandler
98

109

11-
@pytest.mark.asyncio
1210
async def test_serverapp(jp_serverapp):
1311
assert isinstance(jp_serverapp, ServerApp)
1412

1513

16-
@pytest.mark.asyncio
1714
async def test_get_api_spec(jp_fetch):
1815
response = await jp_fetch("api", "spec.yaml", method="GET")
1916
assert response.code == 200
2017

2118

22-
@pytest.mark.asyncio
2319
async def test_send_request(send_request):
2420
code = await send_request("api/spec.yaml", method="GET")
2521
assert code == 200
2622

2723

28-
@pytest.mark.asyncio
2924
async def test_connection(jp_fetch, jp_ws_fetch, jp_http_port, jp_auth_header):
3025
# Create kernel
3126
r = await jp_fetch("api", "kernels", method="POST", body="{}")
@@ -41,7 +36,6 @@ async def test_connection(jp_fetch, jp_ws_fetch, jp_http_port, jp_auth_header):
4136
ws.close()
4237

4338

44-
@pytest.mark.asyncio
4539
async def test_authorizer(jp_server_authorizer, jp_serverapp, jp_base_url):
4640
auth: Authorizer = jp_server_authorizer(parent=jp_serverapp)
4741
assert isinstance(auth, Authorizer)
@@ -56,7 +50,6 @@ async def test_authorizer(jp_server_authorizer, jp_serverapp, jp_base_url):
5650
assert auth.match_url_to_resource("/api/shutdown") == "server"
5751

5852

59-
@pytest.mark.asyncio
6053
async def test_create_notebook(jp_create_notebook):
6154
nb = jp_create_notebook("foo.ipynb")
6255
assert "nbformat" in nb

0 commit comments

Comments
 (0)