Skip to content

Testing apps

yugui.testing.Driver runs an app without opening a window. Your view and update run unchanged; iced lays the view out and works out which widget each click or key press reaches, as it does in a window; and the messages go through your update. Tests need no display and take milliseconds, so they run anywhere, CI included.

Running and testing: the same app and widgets, in a window or headless

A first test

This test drives the counter from examples/7guis/counter.py, whose buttons are labelled Increment and Decrement. It's an ordinary pytest test:

from yugui.testing import Driver

from counter import Counter


def test_counter():
    d = Driver(Counter(), size=(300, 240))
    d.click("Increment")
    d.click("Increment")
    assert d.model == 2

size is the size of the window the driver pretends to have, in pixels. It matters because the layout depends on it: a click at a position lands on whatever the layout put there. The default is 800 by 600.

Acting like a user

A driver remembers what a window remembers between calls: the pointer position, which widget has the focus, scroll offsets, an open pick list. So a test reads as a sequence of steps.

Call Does
d.click("Save") clicks the widget that shows this text, such as a button's label
d.click_at(x, y) clicks at a position, in logical pixels from the top left
d.double_click_at(x, y) double-clicks, which selects a word in a text input
d.press_at, d.move_to, d.release_at a drag, one step at a time
d.type("Ada") types into the focused widget, one key at a time
d.type_at(x, y, "Ada") clicks a text input to focus it, then types
d.key("Enter"), d.key("s", modifiers="cmd") presses a key. Keys that no widget took go to the app's ui.on_key subscriptions
d.scroll(x, y, dy=200) turns the mouse wheel over a point
d.send(message) sends a message to update directly, as if a widget had

d.model is the current model, and d.view() the current tree of nodes.

click(text) finds text widgets only. A text input's placeholder or a pick list's label isn't one, so those need click_at or type_at.

Tasks

Tasks returned by update are collected in d.tasks. Those that act on widgets, such as ui.focus or ui.scroll_to, are also carried out, so typing after a focus task goes to the focused input. The others wait for the test:

  • A dialog (ui.alert, ui.confirm, ui.open_file, ui.save_file) doesn't open. d.answer(task, value) sends what the dialog would have sent.
  • Background work (ui.perform) doesn't start by itself. d.complete() runs the oldest pending task and sends its result, so a test can look at the model while the work is pending:
from async_fetch import FetchApp


def test_fetch():
    d = Driver(FetchApp())
    d.click("Fetch Data")
    assert d.model.loading
    d.complete()
    assert d.model.data == "Hello from the background thread!"

Snapshots

d.snapshot("view.png") renders the view as it stands (scrolled, focused, hovered) to a PNG, in the app's theme unless you pass theme=. Only the view is drawn, with no window frame, at twice the window's size. Every screenshot on this site is a snapshot: docs/screenshots.py runs each example through a driver, and the walkthroughs are sessions recorded one snapshot per step.

Snapshots help when a test fails and you want to see why, and for checking a layout by eye without opening a window. They take a few milliseconds each.

Limits

  • Some widgets react to where the pointer is. A pick list selects the option under the pointer, so move_to the option before clicking it.
  • Two clicks in a row at the same place are a double click, as in a window: click somewhere else in between if you don't want one.