Tutorial for how to do testing
The new developer website is awesome, thank you! I would like to suggest to include a tutorial on how to test a GNOME application. This is an area where I am personally unsure what is recommended, but I can contribute the following collection of notes / tips / hints.
different types of tests
the tutorial should explain that there are different types of automated tests, many of which do not (need to) test the UI, some of which do. E.g.,
- unit tests against parts of the application that do not depend on GTK, can simply be written in test frameworks depending on the programming language, e.g., pytest
- integration test that include the UI, these require approaches specific to GTK
- ...many more...
unittesting by manually controlling the event loop
Based on https://stackoverflow.com/a/14393844 and https://unpythonic.blogspot.com/2007/03/unit-testing-pygtk.html, and I ported this to the following GTK4 example:
import time
import unittest
from unittest import TestCase
import gi
gi.require_versions({"Gtk": "4.0"})
from gi.repository import GLib, Gtk # noqa: E402
class ApplicationWindow(Gtk.ApplicationWindow):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.title = "MyApp"
class ApplicationWindowTest(TestCase):
def setUp(self):
self.window = ApplicationWindow()
self.window.present()
self._refresh_gui()
def _refresh_gui(self, delay=0):
context = GLib.MainContext().default()
while context.pending():
context.iteration()
time.sleep(delay)
def _stop_testing_and_allow_manual_interaction(self):
context = GLib.MainContext().default()
while True:
context.iteration()
def test_initial_view(self):
"""Assert that the window title has been set correctly."""
assert self.window.title == "MyApp"
if __name__ == "__main__":
unittest.main()
Test Output in "TAP" Format was recommended on GNOME discourse
https://discourse.gnome.org/t/gtester-support-deprecated-in-glib/1018
TAP is the "Test Anything Protocol", https://testanything.org/
pytest can also produce TAP output, if using the additional pytest-tap package, see https://github.com/python-tap/pytest-tap
It would be nice if the tutorial could show to to integrate meson test
with TAP with language-specific test frameworks such as pytest.
Using Dogtail
Emmanuele Bassi recommended dogtail https://discourse.gnome.org/t/how-do-you-test-a-gui-application/3010
dogtail accesses applications through assistive technology
dogtail tests can be driven by different test frameworks, including the python library behave. An example of this approach can be seen in the (currently unmaintained) devassistant repository, specifically https://github.com/devassistant/devassistant/blob/master/features/steps/steps.py