示例工程 18-asyncio-event-loop

18-asyncio-event-loop/06-no-nested-run/test_no_nested_run.py

python · 0.6 KB

"""test_no_nested_run.py"""
 
from __future__ import annotations
 
import asyncio
 
import pytest
from no_nested_run import fixed, main, nested
 
 
def test_nested_run_reports_runtime_error() -> None:
    message = asyncio.run(nested())
    assert "cannot be called from a running event loop" in message
 
 
def test_fixed_awaits() -> None:
    assert asyncio.run(fixed()) == "awaited"
 
 
def test_main_runs(capsys: pytest.CaptureFixture[str]) -> None:
    asyncio.run(main())
    out = capsys.readouterr().out
    assert "nested: asyncio.run() cannot be called" in out
    assert "fixed: awaited" in out