Skip to content

Commit c96e099

Browse files
committed
[labeling] Unit test cleanup and add ability to output reports on bad render checks to web browser
1 parent d517461 commit c96e099

File tree

3 files changed

+48
-16
lines changed

3 files changed

+48
-16
lines changed

tests/src/python/CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ ADD_PYTHON_TEST(PyQgsComposerLabel test_qgscomposerlabel.py)
2424
ADD_PYTHON_TEST(PyQgsExpression test_qgsexpression.py)
2525
ADD_PYTHON_TEST(PyQgsPalLabelingBase test_qgspallabeling_base.py)
2626
ADD_PYTHON_TEST(PyQgsPalLabelingCanvas test_qgspallabeling_canvas.py)
27-
#ADD_PYTHON_TEST(PyQgsPalLabelingCanvas test_qgspallabeling_composer.py)
28-
#ADD_PYTHON_TEST(PyQgsPalLabelingCanvas test_qgspallabeling_server.py)
27+
#ADD_PYTHON_TEST(PyQgsPalLabelingComposer test_qgspallabeling_composer.py)
28+
#ADD_PYTHON_TEST(PyQgsPalLabelingServer test_qgspallabeling_server.py)
2929
ADD_PYTHON_TEST(PyQgsVectorFileWriter test_qgsvectorfilewriter.py)
3030
ADD_PYTHON_TEST(PyQgsSpatialiteProvider test_qgsspatialiteprovider.py)
3131
ADD_PYTHON_TEST(PyQgsZonalStatistics test_qgszonalstatistics.py)

tests/src/python/test_qgspallabeling_base.py

+39-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
# -*- coding: utf-8 -*-
22
"""QGIS Unit tests for QgsPalLabeling: base suite setup
33
4-
.. note:: from build dir: ctest -R PyQgsPalLabelingBase -V
5-
Set env variable PAL_SUITE to run specific tests (define in __main__)
6-
Set env variable PAL_VERBOSE to output individual test summary
7-
Set env variable PAL_CONTROL_IMAGE to trigger building of new control images
4+
From build dir: ctest -R PyQgsPalLabelingBase -V
5+
Set the following env variables when manually running tests:
6+
PAL_SUITE to run specific tests (define in __main__)
7+
PAL_VERBOSE to output individual test summary
8+
PAL_CONTROL_IMAGE to trigger building of new control images
9+
PAL_REPORT to open any failed image check reports in web browser
810
911
.. note:: This program is free software; you can redistribute it and/or modify
1012
it under the terms of the GNU General Public License as published by
@@ -20,15 +22,16 @@
2022
import os
2123
import sys
2224
import glob
25+
import shutil
2326
import StringIO
27+
import webbrowser
2428
from PyQt4.QtCore import *
2529
from PyQt4.QtGui import *
2630

2731
from qgis.core import (
2832
QGis,
2933
QgsCoordinateReferenceSystem,
3034
QgsDataSourceURI,
31-
QgsLogger,
3235
QgsMapLayerRegistry,
3336
QgsMapRenderer,
3437
QgsPalLabeling,
@@ -48,6 +51,14 @@
4851

4952
QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()
5053

54+
PALREPORTDIR = ''
55+
if 'PAL_REPORT' in os.environ:
56+
PALREPORTDIR = os.path.join(str(QDir.tempPath()), 'pal_test_report')
57+
# clear out old reports, if temp dir exists
58+
if os.path.exists(PALREPORTDIR):
59+
shutil.rmtree(PALREPORTDIR, True)
60+
os.mkdir(PALREPORTDIR)
61+
5162

5263
class TestQgsPalLabeling(TestCase):
5364

@@ -67,6 +78,8 @@ def setUpClass(cls):
6778
cls._QgisApp, cls._Canvas, cls._Iface, cls._Parent = \
6879
QGISAPP, CANVAS, IFACE, PARENT
6980

81+
cls._PalReportDir = PALREPORTDIR
82+
7083
# verify that spatialite provider is available
7184
msg = ('\nSpatialite provider not found, '
7285
'SKIPPING TEST SUITE')
@@ -153,7 +166,7 @@ def configTest(self, prefix, abbr):
153166
self._TestFunction = testid[2]
154167
testheader = '\n#####_____ {0}.{1} _____#####\n'.\
155168
format(self._TestGroup, self._TestFunction)
156-
QgsLogger.debug(testheader)
169+
qDebug(testheader)
157170

158171
# define the shorthand name of the test (to minimize file name length)
159172
self._Test = '{0}_{1}'.format(self._TestGroupAbbr,
@@ -200,12 +213,22 @@ def saveContolImage(self):
200213
self._Map.render()
201214
self._Canvas.saveAsImage(imgpath)
202215

203-
def renderCheck(self):
216+
def renderCheck(self, mismatch=0):
204217
chk = QgsRenderChecker()
205218
chk.setControlPathPrefix('expected_' + self._TestGroupPrefix)
206219
chk.setControlName(self._Test)
207220
chk.setMapRenderer(self._MapRenderer)
208-
res = chk.runTest(self._Test)
221+
res = chk.runTest(self._Test, mismatch)
222+
if self._PalReportDir and not res: # don't report ok checks
223+
testname = self._TestGroup + ' . ' + self._Test
224+
report = '<html>'
225+
report += '<head><title>{0}</title></head>'.format(testname)
226+
report += '<body>' + chk.report().toLocal8Bit() + '</body>'
227+
report += '</html>'
228+
f = QFile(os.path.join(self._PalReportDir, testname + '.html'))
229+
if f.open(QIODevice.ReadWrite | QIODevice.Truncate):
230+
f.write(report)
231+
f.close()
209232
msg = '\nRender check failed for "{0}"'.format(self._Test)
210233
return res, msg
211234

@@ -280,12 +303,19 @@ def runSuite(module, tests):
280303
print '\nIndividual test summary:'
281304
print '\n' + out.getvalue()
282305
out.close()
306+
307+
if PALREPORTDIR:
308+
for report in os.listdir(PALREPORTDIR):
309+
webbrowser.open_new_tab('file://' +
310+
os.path.join(PALREPORTDIR, report))
311+
283312
return res
284313

314+
285315
if __name__ == '__main__':
286316
# NOTE: unless PAL_SUITE env var is set
287317
# all test class methods will be run
288318
b = 'TestQgsPalLabelingBase.'
289319
tests = [b + 'test_write_read_settings']
290320
res = runSuite(sys.modules[__name__], tests)
291-
sys.exit(not res.wasSuccessful())
321+
sys.exit(int(not res.wasSuccessful()))

tests/src/python/test_qgspallabeling_canvas.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
# -*- coding: utf-8 -*-
22
"""QGIS unit tests for QgsPalLabeling: label rendering to screen canvas
33
4-
.. note:: from build dir: ctest -R PyQgsPalLabelingCanvas -V
5-
Set env variable PAL_SUITE to run specific tests (define in __main__)
6-
Set env variable PAL_VERBOSE to output individual test summary
7-
Set env variable PAL_CONTROL_IMAGE to trigger building of new control images
4+
From build dir: ctest -R PyQgsPalLabelingCanvas -V
5+
Set the following env variables when manually running tests:
6+
PAL_SUITE to run specific tests (define in __main__)
7+
PAL_VERBOSE to output individual test summary
8+
PAL_CONTROL_IMAGE to trigger building of new control images
9+
PAL_REPORT to open any failed image check reports in web browser
810
911
.. note:: This program is free software; you can redistribute it and/or modify
1012
it under the terms of the GNU General Public License as published by
@@ -104,4 +106,4 @@ def test_text_color(self):
104106

105107
tests = [sp + 'test_text_size_map_unit']
106108
res = runSuite(sys.modules[__name__], tests)
107-
sys.exit(not res.wasSuccessful())
109+
sys.exit(int(not res.wasSuccessful()))

0 commit comments

Comments
 (0)