Skip to content

Commit

Permalink
reworked as described in docs (#3413)
Browse files Browse the repository at this point in the history
  • Loading branch information
rodja committed Jul 30, 2024
1 parent 7afcc7c commit 58eef83
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 78 deletions.
27 changes: 4 additions & 23 deletions examples/pytests/main.py
Original file line number Diff line number Diff line change
@@ -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()
File renamed without changes.
21 changes: 21 additions & 0 deletions examples/pytests/pytests/startup.py
Original file line number Diff line number Diff line change
@@ -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!')
25 changes: 20 additions & 5 deletions examples/pytests/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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
27 changes: 0 additions & 27 deletions examples/pytests/tests/test_with_auto_index_page.py

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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')
Original file line number Diff line number Diff line change
@@ -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()
Expand Down

0 comments on commit 58eef83

Please sign in to comment.