Skip to content

Commit

Permalink
improve running tests from gui
Browse files Browse the repository at this point in the history
  • Loading branch information
olivierdalang committed Jan 4, 2023
1 parent 15997ca commit b6d4b49
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 12 deletions.
43 changes: 34 additions & 9 deletions travel_time_platform_plugin/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,24 +246,49 @@ def show_config(self):
self.config_dialog.exec_()

def run_tests(self):

box = QMessageBox(
QMessageBox.Question,
"Run software tests",
"You are about to run automated software tests. There is usually no reason to run them, unless you want to share a report to track down issues that you may encounter.\n\n"
"Warning:\n"
"- the current project will be closed without saving\n"
"- there may be some side-effects on your QGIS user profile\n"
"- this will use your API quota\n"
"- do not interact with QGIS while the tests run",
)
box.setInformativeText("Do you want to proceed ?")
box.setStandardButtons(QMessageBox.Yes | QMessageBox.Cancel)

if box.exec_() != QMessageBox.Yes:
return

with io.StringIO() as buf:
with contextlib.redirect_stdout(buf):
result = tests.run_suite(stream=buf)
output = buf.getvalue()

log(output, "-tests")
log(tests.system_info(), "TravelTimePlatform tests output")
log(output, "TravelTimePlatform tests output")

success = result.wasSuccessful()

box = QMessageBox(
QMessageBox.Question if success else QMessageBox.Critical,
"Test results",
f"Ran {result.testsRun} tests, of which {len(result.errors) + len(result.failures)} failed.",
)
box.setInformativeText("Do you want to copy the test report ?")
box.setStandardButtons(QMessageBox.Yes | QMessageBox.Discard)
if success:
box = QMessageBox(
QMessageBox.Information,
"Success",
f"All {result.testsRun} tests succeeded.",
)
else:
box = QMessageBox(
QMessageBox.Critical,
"Failure",
f"{len(result.errors) + len(result.failures)} tests failed out of {result.testsRun}.",
)
box.setInformativeText("Do you want to copy the report to the clipboard ?")
box.setStandardButtons(QMessageBox.Yes | QMessageBox.No)

if box.exec_():
if box.exec_() == QMessageBox.Yes:
QGuiApplication.clipboard().setText(f"{tests.system_info()}\n{output}")

def show_splash(self):
Expand Down
9 changes: 8 additions & 1 deletion travel_time_platform_plugin/tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
from .. import auth
from ..main import TTPPlugin

# Set to true to save artifacts and add some delay for visual inspection
SHOW_OUTPUT = False

TEST_MODE = os.environ.get("TEST_MODE", "DESKTOP") # HEADLESS | HEADFULL | DESKTOP
assert TEST_MODE in ["HEADLESS", "HEADFULL", "DESKTOP"]

Expand Down Expand Up @@ -88,12 +91,16 @@ def _feedback(self, seconds=1):
"""Waits a little so we can see what happens when running the tests with GUI"""

self.__feedback_step += 1
QgsApplication.processEvents()

# Unless we want to show output, nothing to do
if not SHOW_OUTPUT:
return

if TEST_MODE != "HEADLESS":
t = datetime.now()
while (datetime.now() - t).total_seconds() < seconds:
QgsApplication.processEvents()
QgsApplication.processEvents()

# Save artifacts
artifact_name = f"{self.__class__.__name__}-{self._testMethodName}-{self.__feedback_step:03}"
Expand Down
5 changes: 3 additions & 2 deletions travel_time_platform_plugin/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from datetime import datetime, timedelta, timezone

from qgis.core import (
Qgis,
QgsCoordinateReferenceSystem,
QgsFeature,
QgsGeometry,
Expand Down Expand Up @@ -53,8 +54,8 @@ def clone_feature(request, source_layer, output_fields=None):
return new_feature


def log(msg, suffix=""):
QgsMessageLog.logMessage(str(msg), f"TimeTravelPlatform{suffix}")
def log(msg, tag="TimeTravelPlatform", level=Qgis.Info):
QgsMessageLog.logMessage(str(msg), tag, level=level)


def tr(string):
Expand Down

0 comments on commit b6d4b49

Please sign in to comment.