Skip to content

Commit 28fde8c

Browse files
committed
tests/query_tests: Run unit tests without GUI
- Take advantage of the new, more compartmentalized OpenShotApp class, and run the unit tests without creating the application GUI at all. - Unit tests can even be run headless by selecting a platform plugin with no display requirements on the command line, e.g.: `python3 tests/query_tests.py --platform minimal` `python3 tests/query_tests.py --platform offscreen`
1 parent 9ae08d0 commit 28fde8c

File tree

1 file changed

+37
-12
lines changed

1 file changed

+37
-12
lines changed

src/tests/query_tests.py

+37-12
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,33 @@
2525
along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
2626
"""
2727

28-
import sys, os
28+
import sys
29+
import os
30+
import random
31+
import unittest
32+
import uuid
33+
import json
34+
35+
import openshot
36+
37+
from PyQt5.QtGui import QGuiApplication
38+
try:
39+
# QtWebEngineWidgets must be loaded prior to creating a QApplication
40+
# But on systems with only WebKit, this will fail (and we ignore the failure)
41+
from PyQt5.QtWebEngineWidgets import QWebEngineView
42+
except ImportError:
43+
pass
44+
2945
# Import parent folder (so it can find other imports)
3046
PATH = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
3147
if PATH not in sys.path:
3248
sys.path.append(PATH)
3349

34-
import random
35-
import unittest
36-
import uuid
37-
from PyQt5.QtGui import QGuiApplication
3850
from classes.app import OpenShotApp
3951
from classes import info
40-
import openshot # Python module for libopenshot (required video editing module installed separately)
41-
import json
52+
53+
54+
app = None
4255

4356
class TestQueryClass(unittest.TestCase):
4457
""" Unit test class for Query class """
@@ -47,7 +60,7 @@ class TestQueryClass(unittest.TestCase):
4760
def setUpClass(TestQueryClass):
4861
""" Init unit test data """
4962
# Create Qt application
50-
TestQueryClass.app = OpenShotApp([], mode="unittest")
63+
TestQueryClass.app = QGuiApplication.instance()
5164
TestQueryClass.clip_ids = []
5265
TestQueryClass.file_ids = []
5366
TestQueryClass.transition_ids = []
@@ -106,8 +119,7 @@ def setUpClass(TestQueryClass):
106119
@classmethod
107120
def tearDownClass(cls):
108121
"Hook method for deconstructing the class fixture after running all tests in the class."
109-
print ('Exiting Unittests: Quitting QApplication')
110-
TestQueryClass.app.window.actionQuit.trigger()
122+
TestQueryClass.app.quit()
111123

112124
def test_add_clip(self):
113125
""" Test the Clip.save method by adding multiple clips """
@@ -318,6 +330,19 @@ def test_add_file(self):
318330
self.assertEqual(len(File.filter()), num_files + 1)
319331

320332

321-
if __name__ == '__main__':
322-
app = QGuiApplication(sys.argv)
333+
def main():
334+
global app
335+
info.LOG_LEVEL_CONSOLE = "ERROR"
336+
try:
337+
app = OpenShotApp(sys.argv, mode="unittest")
338+
except Exception:
339+
from logging import logger
340+
log = logger.getlog(".")
341+
log.error("Failed to instantiate OpenShotApp", exc_info=1)
342+
sys.exit()
323343
unittest.main()
344+
app.exec_()
345+
346+
347+
if __name__ == '__main__':
348+
main()

0 commit comments

Comments
 (0)