Skip to content

Commit

Permalink
Merge branch 'integration-test-framework' into testing-docs
Browse files Browse the repository at this point in the history
  • Loading branch information
falkoschindler committed Aug 2, 2024
2 parents 6c140b5 + b54adf7 commit 461cf26
Show file tree
Hide file tree
Showing 18 changed files with 64 additions and 96 deletions.
2 changes: 2 additions & 0 deletions examples/authentication/test_authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

# pylint: disable=missing-function-docstring

pytest_plugins = ['nicegui.testing.plugin']


@pytest.mark.module_under_test(main)
async def test_login_logoff(user: User) -> None:
Expand Down
2 changes: 2 additions & 0 deletions examples/chat_app/test_chat_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

from . import main

pytest_plugins = ['nicegui.testing.plugin']


@pytest.mark.module_under_test(main)
async def test_basic_startup_appearance(user: User) -> None:
Expand Down
File renamed without changes.
21 changes: 21 additions & 0 deletions examples/pytests/app/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!')
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 app.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()
2 changes: 2 additions & 0 deletions examples/pytests/pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[pytest]
asyncio_mode = auto
1 change: 1 addition & 0 deletions examples/pytests/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
nicegui
icecream
pytest-asyncio
pytest-selenium
20 changes: 20 additions & 0 deletions examples/pytests/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from typing import Generator

import pytest
from app.startup import startup

from nicegui.testing import Screen, User

pytest_plugins = ['nicegui.testing.plugin']


@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
2 changes: 2 additions & 0 deletions examples/todo_list/test_todo_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

# pylint: disable=missing-function-docstring

pytest_plugins = ['nicegui.testing.plugin']


@pytest.mark.module_under_test(main)
async def test_checking_items(user: User) -> None:
Expand Down
File renamed without changes.
4 changes: 1 addition & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ build-backend = "poetry.core.masonry.api"
[tool.pytest.ini_options]
addopts = "--driver Chrome"
asyncio_mode = "auto"
testpaths = ["tests"]

[tool.mypy]
python_version = "3.8"
Expand Down Expand Up @@ -129,6 +130,3 @@ ignore = [
exclude = [
"website/documentation/content/*",
]

[tool.poetry.plugins.pytest11]
pytest-nicegui = "nicegui.testing.fixtures"
4 changes: 4 additions & 0 deletions test_startup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,9 @@ do
elif test -f $path/main.py; then
check $path/main.py || error=1
fi
if pytest -q --collect-only $path >/dev/null 2>&1; then
echo "running tests for $path"
pytest $path || error=1
fi
done
test $error -eq 0
3 changes: 1 addition & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
# pylint: disable=wildcard-import,unused-wildcard-import
from nicegui.testing.fixtures import * # noqa: F403
pytest_plugins = ['nicegui.testing.plugin']
8 changes: 4 additions & 4 deletions tests/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from fastapi.responses import PlainTextResponse

from nicegui import app, ui
from nicegui.testing import Screen, fixtures
from nicegui.testing import Screen, plugin


@pytest.fixture
Expand All @@ -25,7 +25,7 @@ def test():
screen.open('/')
screen.click('Download')
screen.wait(0.5)
assert (fixtures.DOWNLOAD_DIR / 'test.txt').read_text() == 'test'
assert (plugin.DOWNLOAD_DIR / 'test.txt').read_text() == 'test'


def test_downloading_local_file_as_src(screen: Screen):
Expand All @@ -36,7 +36,7 @@ def test_downloading_local_file_as_src(screen: Screen):
route_count_before_download = len(app.routes)
screen.click('download')
screen.wait(0.5)
assert (fixtures.DOWNLOAD_DIR / 'slide1.jpg').exists()
assert (plugin.DOWNLOAD_DIR / 'slide1.jpg').exists()
assert len(app.routes) == route_count_before_download


Expand All @@ -46,4 +46,4 @@ def test_download_raw_data(screen: Screen):
screen.open('/')
screen.click('download')
screen.wait(0.5)
assert (fixtures.DOWNLOAD_DIR / 'test.txt').read_text() == 'test'
assert (plugin.DOWNLOAD_DIR / 'test.txt').read_text() == 'test'
14 changes: 0 additions & 14 deletions tests/test_element_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,20 +100,6 @@ def test_find_within_marker2():
assert texts(ElementFilter(kind=ui.label).not_within(marker='a')) == ['Label 3']


def test_find_within_marker_combination():
with ui.card().mark('a'):
ui.label('Label 1')
with ui.card().mark('b'):
ui.label('Label 2')
with ui.card().mark('a'):
ui.label('Label 3')
with ui.card().mark('b'):
ui.label('Label 4')

assert texts(ElementFilter(kind=ui.label).within(marker='a b')) == ['Label 2']
assert texts(ElementFilter(kind=ui.label).within(marker='a').within(marker='b')) == ['Label 2']


def test_find_within_instance():
ui.button('button A')
ui.label('label A')
Expand Down

0 comments on commit 461cf26

Please sign in to comment.