-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.py
More file actions
135 lines (103 loc) · 4.8 KB
/
Copy pathtest.py
File metadata and controls
135 lines (103 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# -*- coding: utf-8 -*-
# SPDX-FileCopyrightText: 2025 Deutsche Telekom Technik GmbH <f.vonstudsinske@telekom.de>
# SPDX-License-Identifier: GPL-3.0-only
import sys
import traceback
import os
from datetime import datetime
from pathlib import Path
from qgis.PyQt.QtWidgets import QMessageBox
def run_pytest_from_console():
import pytest
from submodules.base.functions import get_test_folders, get_additional_sys_path_folders
from submodules.base.qgis.qgis_env import setup_qgis
from utilities.exceptions import TestErrorException
# append additional paths
# keep the path-order to prioritize the imports!
for path in get_additional_sys_path_folders():
if path not in [Path(p) for p in sys.path]:
# append path to sys, if not added yet
sys.path.append(path.as_posix())
# read the path for several credentials to load per test
credentials_path = os.environ.get("QGIS_PYTEST_AUTHENTICATION_CONFIG_DIR")
if credentials_path:
credentials_path = Path(credentials_path)
# setup the QgsApplication, setup some sys.paths and start the application
setup_qgis(credentials_path)
root_dir = Path(__file__).parent
root_folders = get_test_folders(root_dir, ends_with=["tests"])
# set the path to the config file
config = root_dir / "pytest.ini"
code = pytest.main([f"--config-file={config.as_posix()}"] + root_folders)
if code != pytest.ExitCode.OK:
raise TestErrorException(f"Exitcode from pytest is {code}, but {pytest.ExitCode.OK} is expected")
def run_pytest_from_ui(plugin):
"""
Invoking pytest.main from the current Python process.
"""
from .submodules.base.functions import get_test_folders
# ignore paths (absolute or re-pattern)
ignore_paths = []
# get the test folders from the current plugin path
root_folders = get_test_folders(Path(plugin.plugin_dir), ends_with=["tests"], ignore_paths=ignore_paths)
if not root_folders:
QMessageBox.warning(plugin.iface.mainWindow(),
"pytest",
"Keine 'tests'-Ordner gefunden."
"Es können keine Tests ausgeführt werden")
return
try:
import pytest
except ModuleNotFoundError as e:
QMessageBox.warning(plugin.iface.mainWindow(),
"pytest",
f"pytest ist nicht für QGIS korrekt installiert:\n{e}")
return
# save temporary std-streams
stderr = sys.stderr
stdout = sys.stdout
stdin = sys.stdin
# create the temporary output file for test results
stamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S_%f") + ".log"
python_log_file = Path(plugin.log_dir) / ("python_" + stamp)
pytest_log_file = Path(plugin.log_dir) / ("pytest_" + stamp)
# set the absolute path to the pytest ini to be uses in the tests
config = Path(plugin.plugin_dir) / "pytest.ini"
# open the log file and set the file object as std-target
with python_log_file.open("w+") as file:
try:
# setup the logging file name
sys.stderr = file
sys.stdout = file
sys.stdin = file
# run pytest
code = pytest.main([f"--log-file={pytest_log_file.as_posix()}",
f"--config-file={config.as_posix()}"]
+ root_folders)
if code != pytest.ExitCode.OK:
QMessageBox.warning(plugin.iface.mainWindow(),
"pytest",
f"Exitcode von pytest ist {code}, aber {pytest.ExitCode.OK} wird erwartet.\n"
f"QGIS-Neustart empfohlen!")
plugin.iface.messageBar().pushCritical(
"pytest",
"Die Tests wurden ausgeführt. Weitere Ergebnisse im Log. QGIS-Neustart empfohlen!")
else:
plugin.iface.messageBar().pushSuccess(
"pytest",
"Die Tests wurden ausgeführt. Weitere Ergebnisse im Log. QGIS-Neustart empfohlen!")
QMessageBox.information(plugin.iface.mainWindow(),
"pytest",
f"Tests wurden ausgeführt. QGIS-Neustart empfohlen!")
except Exception as e:
# write the exception to the log file
file.write("\n" + str(traceback.format_exc()) + "\n")
plugin.iface.messageBar().pushWarning("pytest", "Fehler bei Testausführung. Weitere Ergebnisse im Log.")
# restore the previous std-streams
sys.stderr = stderr
sys.stdout = stdout
sys.stdin = stdin
plugin.webbrowser_open(python_log_file)
if __name__ == "__main__":
# run from the console only the pytest tests
run_pytest_from_console()