From 58eef83796e78cebdda7436734a4c75a892f0fcd Mon Sep 17 00:00:00 2001 From: Rodja Trappe Date: Tue, 30 Jul 2024 18:58:01 +0200 Subject: [PATCH] reworked as described in docs (#3413) --- examples/pytests/main.py | 27 +++---------------- examples/pytests/{ => pytests}/__init__.py | 0 examples/pytests/pytests/startup.py | 21 +++++++++++++++ examples/pytests/tests/conftest.py | 25 +++++++++++++---- .../tests/test_with_auto_index_page.py | 27 ------------------- ...st_with_routing.py => test_with_screen.py} | 14 ---------- ...h_user_simulation.py => test_with_user.py} | 9 ------- 7 files changed, 45 insertions(+), 78 deletions(-) rename examples/pytests/{ => pytests}/__init__.py (100%) create mode 100644 examples/pytests/pytests/startup.py delete mode 100644 examples/pytests/tests/test_with_auto_index_page.py rename examples/pytests/tests/{test_with_routing.py => test_with_screen.py} (81%) rename examples/pytests/tests/{test_with_user_simulation.py => test_with_user.py} (79%) diff --git a/examples/pytests/main.py b/examples/pytests/main.py index 6df6d7fe3..dde9ffc78 100755 --- a/examples/pytests/main.py +++ b/examples/pytests/main.py @@ -1,27 +1,8 @@ #!/usr/bin/env python3 -from nicegui import Client, ui +from pytests.startup import startup -# pylint: disable=missing-function-docstring +from nicegui import app, ui +app.on_startup(startup) -@ui.page('/') -def main_page() -> None: - ui.markdown('Try running `pytest` on this project!') - ui.button('Click me', on_click=lambda: ui.notify('Button clicked!')) - ui.link('go to subpage', '/subpage') - - -@ui.page('/subpage') -def sub_page() -> None: - ui.markdown('This is a subpage') - - -@ui.page('/with_connected') -async def with_connected(client: Client) -> None: - ui.markdown('This is an async connection demo') - await client.connected() - ui.markdown('Connected!') - - -if __name__ in {'__main__', '__mp_main__'}: - ui.run() +ui.run() diff --git a/examples/pytests/__init__.py b/examples/pytests/pytests/__init__.py similarity index 100% rename from examples/pytests/__init__.py rename to examples/pytests/pytests/__init__.py diff --git a/examples/pytests/pytests/startup.py b/examples/pytests/pytests/startup.py new file mode 100644 index 000000000..954aa6fe0 --- /dev/null +++ b/examples/pytests/pytests/startup.py @@ -0,0 +1,21 @@ +from nicegui import Client, ui + +# pylint: disable=missing-function-docstring + + +def startup() -> None: + @ui.page('/') + def main_page() -> None: + ui.markdown('Try running `pytest` on this project!') + ui.button('Click me', on_click=lambda: ui.notify('Button clicked!')) + ui.link('go to subpage', '/subpage') + + @ui.page('/subpage') + def sub_page() -> None: + ui.markdown('This is a subpage') + + @ui.page('/with_connected') + async def with_connected(client: Client) -> None: + ui.markdown('This is an async connection demo') + await client.connected() + ui.markdown('Connected!') diff --git a/examples/pytests/tests/conftest.py b/examples/pytests/tests/conftest.py index 087b72d5b..8ae72baa9 100644 --- a/examples/pytests/tests/conftest.py +++ b/examples/pytests/tests/conftest.py @@ -1,5 +1,20 @@ -""" -NOTE: This is uncommented because pytest doesn't allow for conftest.py files in subdirectories. - If you want to use this example as a template, you have to uncomment the next line -""" -# pytest_plugins = ['nicegui.testing.fixtures'] +from typing import Generator + +import pytest +from pytests.startup import startup + +from nicegui.testing import Screen, User + +pytest_plugins = ['nicegui.testing.fixtures'] + + +@pytest.fixture +def user(user: User) -> Generator[User, None, None]: + startup() + yield user + + +@pytest.fixture +def screen(screen: Screen) -> Generator[Screen, None, None]: + startup() + yield screen diff --git a/examples/pytests/tests/test_with_auto_index_page.py b/examples/pytests/tests/test_with_auto_index_page.py deleted file mode 100644 index ed37bd97a..000000000 --- a/examples/pytests/tests/test_with_auto_index_page.py +++ /dev/null @@ -1,27 +0,0 @@ -from nicegui.testing import Screen - -from ..main import main_page, sub_page - -# pylint: disable=missing-function-docstring - - -def test_markdown_message(screen: Screen) -> None: - main_page() - - screen.open('/') - screen.should_contain('Try running') - - -def test_button_click(screen: Screen) -> None: - main_page() - - screen.open('/') - screen.click('Click me') - screen.should_contain('Button clicked!') - - -def test_sub_page(screen: Screen) -> None: - sub_page() - - screen.open('/') - screen.should_contain('This is a subpage') diff --git a/examples/pytests/tests/test_with_routing.py b/examples/pytests/tests/test_with_screen.py similarity index 81% rename from examples/pytests/tests/test_with_routing.py rename to examples/pytests/tests/test_with_screen.py index 9f951a2dc..90678372d 100644 --- a/examples/pytests/tests/test_with_routing.py +++ b/examples/pytests/tests/test_with_screen.py @@ -1,45 +1,31 @@ -import importlib - from nicegui.testing import Screen -from .. import main - # pylint: disable=missing-function-docstring def test_markdown_message(screen: Screen) -> None: - importlib.reload(main) - screen.open('/') screen.should_contain('Try running') def test_button_click(screen: Screen) -> None: - importlib.reload(main) - screen.open('/') screen.click('Click me') screen.should_contain('Button clicked!') def test_sub_page(screen: Screen) -> None: - importlib.reload(main) - screen.open('/subpage') screen.should_contain('This is a subpage') def test_with_connected(screen: Screen) -> None: - importlib.reload(main) - screen.open('/with_connected') screen.should_contain('This is an async connection demo') screen.should_contain('Connected!') def test_navigation(screen: Screen) -> None: - importlib.reload(main) - screen.open('/') screen.click('go to subpage') screen.should_contain('This is a subpage') diff --git a/examples/pytests/tests/test_with_user_simulation.py b/examples/pytests/tests/test_with_user.py similarity index 79% rename from examples/pytests/tests/test_with_user_simulation.py rename to examples/pytests/tests/test_with_user.py index 4ba053f71..daf765e1a 100644 --- a/examples/pytests/tests/test_with_user_simulation.py +++ b/examples/pytests/tests/test_with_user.py @@ -1,39 +1,30 @@ -import pytest - from nicegui.testing import User -from .. import main - # pylint: disable=missing-function-docstring -@pytest.mark.module_under_test(main) async def test_markdown_message(user: User) -> None: await user.open('/') await user.should_see('Try running') -@pytest.mark.module_under_test(main) async def test_button_click(user: User) -> None: await user.open('/') user.find('Click me').click() await user.should_see('Button clicked!') -@pytest.mark.module_under_test(main) async def test_sub_page(user: User) -> None: await user.open('/subpage') await user.should_see('This is a subpage') -@pytest.mark.module_under_test(main) async def test_with_connected(user: User) -> None: await user.open('/with_connected') await user.should_see('This is an async connection demo') await user.should_see('Connected!') -@pytest.mark.module_under_test(main) async def test_navigation(user: User) -> None: await user.open('/') user.find('go to subpage').click()