Skip to content

Commit

Permalink
Headless gui tests (#61)
Browse files Browse the repository at this point in the history
* Fix gui tests for headless server

* Add display before import statements

* Fix gui init for callback tests

* Factor out transparent colour setting
  • Loading branch information
rbaltrusch committed Jan 22, 2023
1 parent eccd0ff commit 2ded224
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 20 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/pytest-unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ jobs:
conda install pip
pip install -e .
- name: Install test dependencies
# sudo apt-get install xvfb x11-utils gnumeric required for gui tests running in headless mode
run: |
conda install pip
pip install -r dev-requirements.txt
sudo apt-get install xvfb x11-utils gnumeric
- name: Test with pytest
working-directory: tests
run: |
Expand Down
10 changes: 9 additions & 1 deletion desktop_shop/gui/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,18 @@
from desktop_shop.gui.views import views


def _set_transparent_colour():
# this apparently fails on linux for no particular reason
try:
gui.root.wm_attributes("-transparentcolor", "purple")
except tk.TclError:
pass


def init_root():
"""Initialises and configures tk root"""
gui.root.title("OfflineShop")
gui.root.wm_attributes("-transparentcolor", "purple")
_set_transparent_colour()
gui.root.config(bg=config.BG)
for _ in range(21):
gui.root.add_row(30)
Expand Down
1 change: 1 addition & 0 deletions dev-requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ tox
# Testing.
pytest
pytest-cov
pyvirtualdisplay

# Type checking.
mypy
Expand Down
28 changes: 23 additions & 5 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,17 @@ click==8.0.4
# via
# black
# mkdocs
colorama==0.4.6
# via
# click
# pytest
# tox
coverage[toml]==6.3.2
# via pytest-cov
distlib==0.3.4
# via virtualenv
exceptiongroup==1.1.0
# via pytest
filelock==3.6.0
# via
# tox
Expand All @@ -50,7 +57,9 @@ griffe==0.12.6
idna==3.3
# via requests
importlib-metadata==4.11.1
# via mkdocs
# via
# markdown
# mkdocs
iniconfig==1.1.1
# via pytest
isort==5.10.1
Expand Down Expand Up @@ -97,12 +106,12 @@ mkdocs-literate-nav==0.5.0
mkdocs-material==8.2.7
# via -r dev-requirements.in
mkdocs-material-extensions==1.0.3
# via mkdocs-material
mkdocstrings[python]==0.19.0
# via
# -r dev-requirements.in
# mkdocstrings-python
mkdocstrings-python==0.6.3
# via mkdocs-material
mkdocstrings[python]==0.19.0
# via mkdocstrings
mypy==0.982
# via -r dev-requirements.in
Expand Down Expand Up @@ -149,6 +158,8 @@ pytest-cov==4.0.0
# via -r dev-requirements.in
python-dateutil==2.8.2
# via ghp-import
pyvirtualdisplay==3.0
# via -r dev-requirements.in
pyyaml==6.0
# via
# mkdocs
Expand All @@ -167,11 +178,18 @@ six==1.16.0
soupsieve==2.3.1
# via beautifulsoup4
tomli==2.0.1
# via coverage
# via
# black
# coverage
# mypy
# pytest
# tox
tox==3.27.1
# via -r dev-requirements.in
typing-extensions==4.1.1
# via mypy
# via
# black
# mypy
urllib3==1.26.8
# via requests
virtualenv==20.13.1
Expand Down
17 changes: 17 additions & 0 deletions tests/unit/gui/callbacks_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,21 @@

# pylint: disable=missing-function-docstring

import os
import sys

import sqlite3
import pytest

# fix for headless machines
display = None # pylint: disable=invalid-name
if sys.platform.startswith("linux") and os.environ.get("DISPLAY") is None:
from pyvirtualdisplay import Display

display = Display(visible=False, size=(100, 60))
display.start()

# pylint: disable=wrong-import-position
from desktop_shop.gui import callbacks, init
from desktop_shop import gui
from desktop_shop.user import UserSignUpData
Expand All @@ -21,6 +33,11 @@
ITERATIONS = 1


def teardown():
if display is not None:
display.stop()


def init_gui(monkeypatch: pytest.MonkeyPatch):
generate_data.generate(
TEST_DB,
Expand Down
30 changes: 16 additions & 14 deletions tests/unit/gui/init_test.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
# -*- coding: utf-8 -*-
"""Smoke tests for gui init function"""

# pylint: disable=missing-function-docstring, missing-class-docstring

from collections import namedtuple
import os
import sys

import pytest

from desktop_shop.gui.views import checkout


# pylint: disable=wrong-import-position
from desktop_shop import database, server
from desktop_shop import gui
from desktop_shop.gui import init

Product = namedtuple("Product", ["id", "name", "price"])


def test_gui_init(monkeypatch):
if sys.platform.startswith("linux") and os.environ.get("DISPLAY") is None: # FIXME
pytest.skip("Tkinter cannot be initialised on headless server")

from desktop_shop import gui
from desktop_shop.gui import init
from desktop_shop.gui.views import views

Product = namedtuple("Product", ["id", "name", "price"])
if sys.platform.startswith("linux") and os.environ.get("DISPLAY") is None:
pytest.skip()

def fake_products_query(*_, **__):
return [
Expand All @@ -35,10 +38,8 @@ def fake_products_query(*_, **__):


def test_checkout_view():
if sys.platform.startswith("linux") and os.environ.get("DISPLAY") is None: # FIXME
pytest.skip("Tkinter cannot be initialised on headless server")

from desktop_shop import gui
if sys.platform.startswith("linux") and os.environ.get("DISPLAY") is None:
pytest.skip()

class MonkeyPatch:
def __enter__(self):
Expand All @@ -50,9 +51,10 @@ def __enter__(self):
def __exit__(self, *_):
server.query_product_data_from_product_table_by_product_ids = self.func

checkout_view = checkout.View.create(gui.root, init.init_builder())
with MonkeyPatch():
gui.app.data["cart"] = ["0"]
gui.app["checkout"].init_checkout()
checkout_view.init_checkout()

# call again to cover code for the case of an already-built checkout View
gui.app["checkout"].init_checkout()
checkout_view.init_checkout()

0 comments on commit 2ded224

Please sign in to comment.