0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

NiceGUIのフィクスチャを使ったGUIのテスト方法

Last updated at Posted at 2025-03-04

概要

NiceGUIベースの書籍管理システムを通して、pytestによるGUIのテスト方法を紹介します。

紹介すること

  • userフィクスチャによる軽量テスト
  • screenフィクスチャによるブラウザベースのテスト

実行に必要なもの

  • uv
  • Python 3.12以上

NiceGUIについて

NiceGUIは、PythonでWebアプリケーションやWebAPIを作成するためのフレームワークです。

書籍管理システムについて

書籍管理システムは、著者の追加・削除、書籍の追加・削除ができるシステムです。
実行するには、次のリポジトリをgit cloneしてください。

git clone後に、次のようにして実行できます。

uv run book-list

著者リストと書籍リストの2つのページがあります。

pytestについて

pytestは、Pythonで人気のテストフレームワークです。

紹介するリポジトリのテストは次のようにして実行します。

uv run pytest

次の6つをテストします。

  • userフィクスチャを使ったテスト
    • test_label: 表示項目のテスト
    • test_add_author: 著者追加のテスト
    • test_show_book: 書籍表示のテスト
  • screenフィクスチャを使ったテスト
    • test_label: 表示項目のテスト
    • test_add_author: 著者追加のテスト
    • test_show_book: 書籍表示のテスト

userフィクスチャとscreenフィクスチャについて

userフィクスチャとscreenフィクスチャは、NiceGUIで用意されているGUIのテスト用のフィクスチャです。
GUIのテストをシンプルに記述できます。

screenフィクスチャを使ったテスト関数を実行すると関数終了時の画面のスナップショットが自動でscreenshotsフォルダーに保存されます。

テストに必要なもの

パッケージ

  • pytest: テストフレームワーク
  • pytest-asyncio: pytestの非同期用
  • pytest-selenium: pytestのヘッドレスブラウザ用

ファイル

  • conftest.py: pytest_pluginsを指定する
  • src/tests/__init__.py: 空ファイル
  • src/tests/conftest.py: フィクスチャなどの作成
  • src/tests/test_views.py: テストコードの記述

conftest.py

userフィクスチャとscreenフィクスチャを使用するには、次の記述が必要です。

pytest_plugins = ["nicegui.testing.plugin"]

pytest_pluginsは、ルートのconftest.pyに書く必要があります。

src/tests/conftest.py

テスト用DBのフィクスチャ

DBはsqlite3です。テストではインメモリで使います。
ORMはtortoise-ormを使います。

次のフィクスチャを用意することで、テストの関数ごとに空のDBを利用できます。

@pytest.fixture(autouse=True)
def db() -> Iterable[None]:
    """DBの開始と終了"""
    asyncio.run(Tortoise.init(db_url="sqlite://:memory:", modules={"models": ["nicegui_book_list.models"]}))
    asyncio.run(Tortoise.generate_schemas())
    yield
    asyncio.run(Tortoise.close_connections())

userフィクスチャとscreenフィクスチャ

テストの関数でuserフィクスチャを使えますが、ここでは次のようにページの登録(ルーティング)もしています。screenフィクスチャも同様です。

@pytest.fixture
def user(user: User) -> User:
    """ページを登録してuserフィクスチャを返す"""
    importlib.reload(nicegui_pytest.views)
    return user

テストの実装

次は、userフィクスチャの使用例です。userフィクスチャは軽量に動作します。

  • user.open("/")で、トップページを開く
  • user.should_see()で、画面の内容を検証する
async def test_label(user: User) -> None:
    """表示項目のテスト"""
    await user.open("/")  # 著者リストのページを開く

    # 画面に「書籍管理システム」が表示されていることの確認
    await user.should_see("書籍管理システム")

次は、screenフィクスチャの使用例です。screenフィクスチャは、ブラウザを操作するので少し重いです。

  • screen.open("/")で、トップページを開く
  • screen.should_contain()で、画面の内容を検証する
def test_label(screen: Screen) -> None:
    """表示項目のテスト"""
    screen.open("/")
    # 画面に「書籍管理システム」が表示されていることの確認
    screen.should_contain("書籍管理システム")

より詳しいテストの書き方については、次も参考にしてください。

以上

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?