diff --git a/tests/test_cli.py b/tests/test_cli.py index 4ebfe361..f3ae03c2 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,5 +1,3 @@ -import asyncio - from click.testing import CliRunner from aiohttp_devtools.cli import cli @@ -16,8 +14,7 @@ def test_cli_help(): assert 'Serve static files from a directory.' in result.output -def test_serve(mocker, event_loop): - asyncio.set_event_loop(event_loop) +def test_serve(mocker): mock_run_app = mocker.patch('aiohttp_devtools.cli.run_app') runner = CliRunner() result = runner.invoke(cli, ['serve', '.']) @@ -71,8 +68,7 @@ def test_runserver_error_verbose(mocker): @forked -def test_runserver_no_args(event_loop): - asyncio.set_event_loop(event_loop) +def test_runserver_no_args(): runner = CliRunner() result = runner.invoke(cli, ['runserver']) assert result.exit_code == 2 diff --git a/tests/test_runserver_config.py b/tests/test_runserver_config.py index cf7ceeb9..822c3ff6 100644 --- a/tests/test_runserver_config.py +++ b/tests/test_runserver_config.py @@ -14,7 +14,7 @@ async def test_load_simple_app(tmpworkdir): @forked -async def test_create_app_wrong_name(tmpworkdir, event_loop): +async def test_create_app_wrong_name(tmpworkdir): mktree(tmpworkdir, SIMPLE_APP) config = Config(app_path='app.py', app_factory_name='missing') with pytest.raises(AiohttpDevConfigError) as excinfo: diff --git a/tests/test_runserver_main.py b/tests/test_runserver_main.py index d519c426..04f090ed 100644 --- a/tests/test_runserver_main.py +++ b/tests/test_runserver_main.py @@ -93,7 +93,7 @@ async def check_callback(session): @forked -def test_start_runserver_app_instance(tmpworkdir, event_loop): +def test_start_runserver_app_instance(tmpworkdir): mktree(tmpworkdir, { 'app.py': """\ from aiohttp import web @@ -116,7 +116,7 @@ async def hello(request): @forked -def test_start_runserver_with_multi_app_modules(tmpworkdir, event_loop, capfd): +def test_start_runserver_with_multi_app_modules(tmpworkdir, capfd): mktree(tmpworkdir, { "app.py": f"""\ from aiohttp import web @@ -194,11 +194,11 @@ async def test_aux_app(tmpworkdir, aiohttp_client): @forked -async def test_serve_main_app(tmpworkdir, event_loop, mocker): - asyncio.set_event_loop(event_loop) +async def test_serve_main_app(tmpworkdir, mocker): + loop = asyncio.get_running_loop() mktree(tmpworkdir, SIMPLE_APP) mock_modify_main_app = mocker.patch('aiohttp_devtools.runserver.serve.modify_main_app') - event_loop.call_later(0.5, event_loop.stop) + loop.call_later(0.5, loop.stop) config = Config(app_path="app.py", main_port=0) runner = await create_main_app(config, config.import_app_factory()) @@ -210,7 +210,7 @@ async def test_serve_main_app(tmpworkdir, event_loop, mocker): @forked -async def test_start_main_app_app_instance(tmpworkdir, event_loop, mocker): +async def test_start_main_app_app_instance(tmpworkdir, mocker): mktree(tmpworkdir, { 'app.py': """\ from aiohttp import web @@ -234,11 +234,10 @@ async def hello(request): @pytest.fixture -def aux_cli(aiohttp_client, event_loop): +async def aux_cli(aiohttp_client): app = create_auxiliary_app(static_path='.') - cli = event_loop.run_until_complete(aiohttp_client(app)) - yield cli - event_loop.run_until_complete(cli.close()) + async with await aiohttp_client(app) as cli: + yield cli async def test_websocket_hello(aux_cli, smart_caplog): @@ -256,7 +255,7 @@ async def test_websocket_hello(aux_cli, smart_caplog): assert 'adev.server.aux WARNING: browser disconnected, appears no websocket connection was made' in smart_caplog -async def test_websocket_info(aux_cli, event_loop): +async def test_websocket_info(aux_cli): assert len(aux_cli.server.app[WS]) == 0 ws = await aux_cli.session.ws_connect(aux_cli.make_url('/livereload')) try: @@ -282,7 +281,7 @@ async def test_websocket_bad(aux_cli, smart_caplog): assert "adev.server.aux ERROR: unknown websocket message type binary, data: b'this is bytes'" in smart_caplog -async def test_websocket_reload(aux_cli, event_loop): +async def test_websocket_reload(aux_cli): reloads = await src_reload(aux_cli.server.app, 'foobar') assert reloads == 0 ws = await aux_cli.session.ws_connect(aux_cli.make_url('/livereload')) diff --git a/tests/test_runserver_serve.py b/tests/test_runserver_serve.py index 6a319b3c..5a132caf 100644 --- a/tests/test_runserver_serve.py +++ b/tests/test_runserver_serve.py @@ -98,7 +98,7 @@ async def test_aux_reload_runtime_error(smart_caplog): assert 'adev.server.aux ERROR: Error broadcasting change to /foo/bar, RuntimeError: foobar\n' == smart_caplog -async def test_aux_cleanup(event_loop): +async def test_aux_cleanup(): aux_app = Application() aux_app.on_cleanup.append(cleanup_aux_app) ws = MagicMock() diff --git a/tests/test_runserver_watch.py b/tests/test_runserver_watch.py index c4f3c2fa..4af690ff 100644 --- a/tests/test_runserver_watch.py +++ b/tests/test_runserver_watch.py @@ -31,7 +31,7 @@ async def __anext__(self): return awatch_mock -async def test_single_file_change(event_loop, mocker): +async def test_single_file_change(mocker): mocked_awatch = mocker.patch('aiohttp_devtools.runserver.watch.awatch') mocked_awatch.side_effect = create_awatch_mock() mock_src_reload = mocker.patch('aiohttp_devtools.runserver.watch.src_reload', return_value=create_future()) @@ -53,7 +53,7 @@ async def test_single_file_change(event_loop, mocker): await app_task._session.close() -async def test_multiple_file_change(event_loop, mocker): +async def test_multiple_file_change(mocker): mocked_awatch = mocker.patch('aiohttp_devtools.runserver.watch.awatch') mocked_awatch.side_effect = create_awatch_mock({('x', '/path/to/file'), ('x', '/path/to/file2')}) mock_src_reload = mocker.patch('aiohttp_devtools.runserver.watch.src_reload', return_value=create_future()) @@ -71,7 +71,7 @@ async def test_multiple_file_change(event_loop, mocker): await app_task._session.close() -async def test_python_no_server(event_loop, mocker): +async def test_python_no_server(mocker): mocked_awatch = mocker.patch('aiohttp_devtools.runserver.watch.awatch') mocked_awatch.side_effect = create_awatch_mock({('x', '/path/to/file.py')}) @@ -100,7 +100,7 @@ async def test_python_no_server(event_loop, mocker): await app_task._session.close() -async def test_reload_server_running(event_loop, aiohttp_client, mocker): +async def test_reload_server_running(aiohttp_client, mocker): app = Application() ws: Set[Tuple[WebSocketResponse, str]] = set(((MagicMock(), "/foo"),)) app[WS] = ws @@ -117,7 +117,7 @@ async def test_reload_server_running(event_loop, aiohttp_client, mocker): await app_task._session.close() -async def test_livereload_task_single(event_loop, mocker): +async def test_livereload_task_single(mocker): mocked_awatch = mocker.patch('aiohttp_devtools.runserver.watch.awatch') mocked_awatch.side_effect = create_awatch_mock() mock_src_reload = mocker.patch('aiohttp_devtools.runserver.watch.src_reload', return_value=create_future()) @@ -130,7 +130,7 @@ async def test_livereload_task_single(event_loop, mocker): mock_src_reload.assert_called_once_with(app, '/path/to/file') -async def test_livereload_task_multiple(event_loop, mocker): +async def test_livereload_task_multiple(mocker): mocked_awatch = mocker.patch('aiohttp_devtools.runserver.watch.awatch') mocked_awatch.side_effect = create_awatch_mock({('x', '/path/to/file'), ('x', '/path/to/file2')}) mock_src_reload = mocker.patch('aiohttp_devtools.runserver.watch.src_reload', return_value=create_future()) diff --git a/tests/test_serve.py b/tests/test_serve.py index 1a3dc9fd..c5745887 100644 --- a/tests/test_serve.py +++ b/tests/test_serve.py @@ -1,5 +1,3 @@ -import asyncio - import pytest from pytest_toolbox import mktree @@ -7,10 +5,9 @@ @pytest.fixture -def cli(event_loop, tmpworkdir, aiohttp_client): - asyncio.set_event_loop(event_loop) +async def cli(tmpworkdir, aiohttp_client): args = serve_static(static_path=str(tmpworkdir), livereload=False) - yield event_loop.run_until_complete(aiohttp_client(args["app"])) + yield await aiohttp_client(args["app"]) async def test_simple_serve(cli, tmpworkdir): @@ -39,7 +36,7 @@ async def test_file_missing(cli, tmpworkdir): assert "baz/\n" in text -async def test_browser_cache(event_loop, aiohttp_client, tmpworkdir): +async def test_browser_cache(aiohttp_client, tmpworkdir): args = serve_static(static_path=str(tmpworkdir), browser_cache=True) assert args["port"] == 8000 cli = await aiohttp_client(args["app"]) @@ -49,7 +46,7 @@ async def test_browser_cache(event_loop, aiohttp_client, tmpworkdir): assert "Cache-Control" not in r.headers -async def test_html_file_livereload(event_loop, aiohttp_client, tmpworkdir): +async def test_html_file_livereload(aiohttp_client, tmpworkdir): args = serve_static(static_path=str(tmpworkdir), livereload=True) assert args["port"] == 8000 cli = await aiohttp_client(args["app"]) @@ -68,7 +65,7 @@ async def test_html_file_livereload(event_loop, aiohttp_client, tmpworkdir): assert text.startswith('(function e(t,n,r){') -async def test_serve_index(event_loop, aiohttp_client, tmpworkdir): +async def test_serve_index(aiohttp_client, tmpworkdir): args = serve_static(static_path=str(tmpworkdir), livereload=False) assert args["port"] == 8000 cli = await aiohttp_client(args["app"])