Skip to content

Commit

Permalink
pytest function helper for qt bot
Browse files Browse the repository at this point in the history
  • Loading branch information
mattclifford1 committed Oct 9, 2024
1 parent adf3d5f commit b68a9f0
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 148 deletions.
47 changes: 47 additions & 0 deletions tests/QtBot_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Author: Matt Clifford <[email protected]>
# License: BSD 3-Clause License
import pytest
from PyQt6 import QtTest, QtWidgets, QtCore
from pytestqt.plugin import QtBot
import IQM_Vis


def default_UI():
app = IQM_Vis.make_UI(test=True)
return app


# building and closing function of UI for testing
class BotTester:
def __init__(self, get_UI=default_UI, wait_time=100, final_wait=False):
self.get_UI = get_UI
self.wait_time = wait_time
self.final_wait = final_wait

@pytest.fixture(scope='function')
def build_IQM_Vis(self):
# Setup
test_window = self.get_UI()
qtbotbis = QtBot(test_window.window)

yield test_window, qtbotbis

# Clean up
QtTest.QTest.qWait(1000)

# need to handle the closing dialog
def handle_dialog():
messagebox = QtWidgets.QApplication.activeWindow()
yes_button = messagebox.button(
QtWidgets.QMessageBox.StandardButton.Yes)
qtbotbis.mouseClick(
yes_button, QtCore.Qt.MouseButton.LeftButton, delay=1)

QtCore.QTimer.singleShot(100, handle_dialog)

test_window.window.main.close()
# if self.final_wait:
QtTest.QTest.qWait(1000)



37 changes: 6 additions & 31 deletions tests/UI/test_sliders.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Author: Matt Clifford <[email protected]>
# License: BSD 3-Clause License

import pytest
from PyQt6 import QtTest, QtWidgets, QtCore
from pytestqt.plugin import QtBot
import IQM_Vis
import sys
import os
sys.path.append(os.path.abspath('..'))
from tests.QtBot_utils import BotTester


def get_UI():
Expand Down Expand Up @@ -49,33 +49,8 @@ def get_UI():
return test_app


# building and closing function of UI for testing
@pytest.fixture(scope='function')
def build_IQM_Vis():
# QtTest.QTest.qWait(5000)
# Setup
test_window = get_UI()
qtbotbis = QtBot(test_window.window)

yield test_window, qtbotbis

# Clean up
QtTest.QTest.qWait(100)

# need to handle the closing dialog
def handle_dialog():
messagebox = QtWidgets.QApplication.activeWindow()
yes_button = messagebox.button(
QtWidgets.QMessageBox.StandardButton.Yes)
qtbotbis.mouseClick(
yes_button, QtCore.Qt.MouseButton.LeftButton, delay=1)

QtCore.QTimer.singleShot(100, handle_dialog)

# test_window.window.quit()
test_window.window.main.close()
# QtTest.QTest.qWait(5000)

build_IQM_Vis = BotTester(get_UI=get_UI, wait_time=1000,
final_wait=True).build_IQM_Vis

def test_sliders(build_IQM_Vis):
test_window, qtbotbis = build_IQM_Vis
Expand Down
33 changes: 5 additions & 28 deletions tests/notebook_examples/test_1_making_the_UI.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,19 @@
# Author: Matt Clifford <[email protected]>
# License: BSD 3-Clause License

import pytest
from PyQt6 import QtTest, QtWidgets, QtCore
from pytestqt.plugin import QtBot
import IQM_Vis

import sys
import os
sys.path.append(os.path.abspath('..'))
from tests.QtBot_utils import BotTester

def get_UI():
app = IQM_Vis.make_UI(test=True)
return app


# building and closing function of UI for testing
@pytest.fixture(scope='function')
def build_IQM_Vis():
# Setup
test_window = get_UI()
qtbotbis = QtBot(test_window.window)

yield test_window, qtbotbis

# Clean up
QtTest.QTest.qWait(100)

# need to handle the closing dialog
def handle_dialog():
messagebox = QtWidgets.QApplication.activeWindow()
yes_button = messagebox.button(
QtWidgets.QMessageBox.StandardButton.Yes)
qtbotbis.mouseClick(
yes_button, QtCore.Qt.MouseButton.LeftButton, delay=1)

QtCore.QTimer.singleShot(100, handle_dialog)

# test_window.window.quit()
test_window.window.main.close()
# QtTest.QTest.qWait(1000)
build_IQM_Vis = BotTester(get_UI=get_UI).build_IQM_Vis


def test_build_1(build_IQM_Vis):
Expand Down
37 changes: 7 additions & 30 deletions tests/notebook_examples/test_2_simple_customisation.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# Author: Matt Clifford <[email protected]>
# License: BSD 3-Clause License

import pytest
from PyQt6 import QtTest, QtWidgets, QtCore
from pytestqt.plugin import QtBot
from tests.QtBot_utils import BotTester
import IQM_Vis

import sys
import os
sys.path.append(os.path.abspath('..'))


def get_UI():
image1 = IQM_Vis.examples.images.IMAGE1
Expand Down Expand Up @@ -50,35 +52,10 @@ def get_UI():
return test_app


# building and closing function of UI for testing
@pytest.fixture(scope='function')
def build_IQM_Vis():
# QtTest.QTest.qWait(5000)
# Setup
test_window = get_UI()
qtbotbis = QtBot(test_window.window)

yield test_window, qtbotbis

# Clean up
QtTest.QTest.qWait(100)

# need to handle the closing dialog
def handle_dialog():
messagebox = QtWidgets.QApplication.activeWindow()
yes_button = messagebox.button(
QtWidgets.QMessageBox.StandardButton.Yes)
qtbotbis.mouseClick(
yes_button, QtCore.Qt.MouseButton.LeftButton, delay=1)

QtCore.QTimer.singleShot(100, handle_dialog)

# test_window.window.quit()
test_window.window.main.close()
# QtTest.QTest.qWait(5000)
build_IQM_Vis = BotTester(get_UI=get_UI).build_IQM_Vis


def test_build_2(build_IQM_Vis):
def test_build_1(build_IQM_Vis):
test_window, _ = build_IQM_Vis
assert test_window.showing == True

Expand Down
40 changes: 8 additions & 32 deletions tests/notebook_examples/test_3_customisation_details.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@

import numpy as np
import pytest
from PyQt6 import QtTest, QtWidgets, QtCore
from pytestqt.plugin import QtBot

import IQM_Vis
import sys
import os
sys.path.append(os.path.abspath('..'))
from tests.QtBot_utils import BotTester

def custom_MAE_function(im_ref, im_comp, **kwargs):
L1 = np.abs(im_ref - im_comp)
Expand Down Expand Up @@ -50,38 +53,11 @@ def get_UI():
return test_app


# building and closing function of UI for testing
@pytest.fixture(scope='function')
def build_IQM_Vis():
# Setup
test_window = get_UI()
qtbotbis = QtBot(test_window.window)

yield test_window, qtbotbis

# Clean up
QtTest.QTest.qWait(1000)

# need to handle the closing dialog
def handle_dialog():
messagebox = QtWidgets.QApplication.activeWindow()
yes_button = messagebox.button(
QtWidgets.QMessageBox.StandardButton.Yes)
qtbotbis.mouseClick(
yes_button, QtCore.Qt.MouseButton.LeftButton, delay=1)
# build_IQM_Vis = BotTester(get_UI=get_UI, wait_time=1000, final_wait=True).build_IQM_Vis
build_IQM_Vis = BotTester(get_UI=get_UI).build_IQM_Vis

QtCore.QTimer.singleShot(100, handle_dialog)

# test_window.window.quit()
test_window.window.main.close()
QtTest.QTest.qWait(1000)


@pytest.fixture(scope='function')
# @pytest.fixture(scope='function')
def test_build_3(build_IQM_Vis):
test_window, _ = build_IQM_Vis
assert test_window.showing == True


def test_3(test_build_3):
return
33 changes: 7 additions & 26 deletions tests/notebook_examples/test_4_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@
# License: BSD 3-Clause License

import pytest
from PyQt6 import QtTest, QtWidgets, QtCore
from pytestqt.plugin import QtBot, _close_widgets
from PyQt6 import QtTest, QtCore

import IQM_Vis
import sys
import os
sys.path.append(os.path.abspath('..'))
from tests.QtBot_utils import BotTester


def get_UI():
Expand Down Expand Up @@ -47,30 +51,7 @@ def get_UI():
return test_app


# building and closing function of UI for testing
@pytest.fixture(scope='function')
def build_IQM_Vis():
# Setup
test_window = get_UI()
qtbotbis = QtBot(test_window.window)

yield test_window, qtbotbis

# Clean Up
QtTest.QTest.qWait(500)

# need to handle the closing dialog
def handle_dialog():
messagebox = QtWidgets.QApplication.activeWindow()
yes_button = messagebox.button(
QtWidgets.QMessageBox.StandardButton.Yes)
qtbotbis.mouseClick(
yes_button, QtCore.Qt.MouseButton.LeftButton, delay=1)

QtCore.QTimer.singleShot(100, handle_dialog)

# test_window.window.quit()
test_window.window.main.close()
build_IQM_Vis = BotTester(get_UI=get_UI).build_IQM_Vis


@pytest.fixture(scope='function')
Expand Down
2 changes: 1 addition & 1 deletion tests/reports/junit/junit.xml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?xml version="1.0" encoding="utf-8"?><testsuites><testsuite name="pytest" errors="0" failures="0" skipped="0" tests="0" time="3.470" timestamp="2024-10-09T13:01:49.633122" hostname="Dell-5320" /></testsuites>
<?xml version="1.0" encoding="utf-8"?><testsuites><testsuite name="pytest" errors="0" failures="0" skipped="0" tests="13" time="27.636" timestamp="2024-10-09T16:14:46.626846" hostname="Dell-5320"><testcase classname="tests.test_metrics" name="test_all_metrics_initialise" time="0.247" /><testcase classname="tests.test_metrics" name="test_all_metrics_images_initialise" time="0.252" /><testcase classname="tests.test_metrics" name="test_all_metrics_process_image" time="2.069" /><testcase classname="tests.test_metrics" name="test_all_metric_images_process_image" time="0.280" /><testcase classname="tests.test_transforms" name="test_all_transforms_init_value_no_change_image" time="0.259" /><testcase classname="tests.test_transforms" name="test_all_transforms_min_max_specified" time="0.256" /><testcase classname="tests.test_transforms" name="test_all_transforms_min_change_image" time="0.301" /><testcase classname="tests.test_transforms" name="test_all_transforms_max_change_image" time="0.359" /><testcase classname="tests.UI.test_sliders" name="test_sliders" time="3.176" /><testcase classname="tests.notebook_examples.test_1_making_the_UI" name="test_build_1" time="3.073" /><testcase classname="tests.notebook_examples.test_2_simple_customisation" name="test_build_1" time="3.203" /><testcase classname="tests.notebook_examples.test_3_customisation_details" name="test_build_3" time="3.173" /><testcase classname="tests.notebook_examples.test_4_experiment" name="test_experiment_runs" time="8.071" /></testsuite></testsuites>

0 comments on commit b68a9f0

Please sign in to comment.