Skip to content

Commit fef6e2b

Browse files
committed
[labeling] Unit tests update
- Fix unpacking of render check results (fix false positives) - Push opening of report to background process for Linux - Report all failures to one random temp HTML file - Better HTML output for report
1 parent 0607202 commit fef6e2b

2 files changed

Lines changed: 38 additions & 32 deletions

File tree

‎tests/src/python/test_qgspallabeling_base.py‎

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@
2121

2222
import os
2323
import sys
24+
import datetime
2425
import glob
25-
import shutil
2626
import StringIO
27-
import webbrowser
27+
import subprocess
28+
import tempfile
2829
from PyQt4.QtCore import *
2930
from PyQt4.QtGui import *
3031

@@ -51,13 +52,8 @@
5152

5253
QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()
5354

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)
55+
PALREPORT = 'PAL_REPORT' in os.environ
56+
PALREPORTS = {}
6157

6258

6359
class TestQgsPalLabeling(TestCase):
@@ -78,8 +74,6 @@ def setUpClass(cls):
7874
cls._QgisApp, cls._Canvas, cls._Iface, cls._Parent = \
7975
QGISAPP, CANVAS, IFACE, PARENT
8076

81-
cls._PalReportDir = PALREPORTDIR
82-
8377
# verify that spatialite provider is available
8478
msg = ('\nSpatialite provider not found, '
8579
'SKIPPING TEST SUITE')
@@ -116,8 +110,7 @@ def setUpClass(cls):
116110
# default for labeling test data sources: WGS 84 / UTM zone 13N
117111
crs.createFromSrid(32613)
118112
cls._MapRenderer.setDestinationCrs(crs)
119-
# TODO: match and store platform's native logical output dpi
120-
cls._MapRenderer.setOutputSize(QSize(600, 400), 72)
113+
# use platform's native logical output dpi for QgsMapRenderer on launch
121114

122115
cls._Pal = QgsPalLabeling()
123116
cls._MapRenderer.setLabelingEngine(cls._Pal)
@@ -219,16 +212,9 @@ def renderCheck(self, mismatch=0):
219212
chk.setControlName(self._Test)
220213
chk.setMapRenderer(self._MapRenderer)
221214
res = chk.runTest(self._Test, mismatch)
222-
if self._PalReportDir and not res: # don't report ok checks
215+
if PALREPORT and not res: # don't report ok checks
223216
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()
217+
PALREPORTS[testname] = str(chk.report().toLocal8Bit())
232218
msg = '\nRender check failed for "{0}"'.format(self._Test)
233219
return res, msg
234220

@@ -304,10 +290,30 @@ def runSuite(module, tests):
304290
print '\n' + out.getvalue()
305291
out.close()
306292

307-
if PALREPORTDIR:
308-
for report in os.listdir(PALREPORTDIR):
309-
webbrowser.open_new_tab('file://' +
310-
os.path.join(PALREPORTDIR, report))
293+
if PALREPORTS:
294+
teststamp = 'PAL Test Report: ' + \
295+
datetime.datetime.now().strftime('%Y-%m-%d %X')
296+
report = '<html><head><title>{0}</title></head><body>'.format(teststamp)
297+
report += '\n<h2>Failed Tests: {0}</h2>'.format(len(PALREPORTS))
298+
for k, v in PALREPORTS.iteritems():
299+
report += '\n<h3>{0}</h3>\n{1}'.format(k, v)
300+
report += '</body></html>'
301+
302+
tmp = tempfile.NamedTemporaryFile(suffix=".html", delete=False)
303+
palreport = tmp.name
304+
tmp.write(report)
305+
tmp.close()
306+
307+
if sys.platform[:3] in ('win', 'dar'):
308+
import webbrowser
309+
webbrowser.open_new_tab("file://{0}".format(palreport))
310+
else:
311+
# some Linux OS pause execution on webbrowser open, so background it
312+
cmd = 'import webbrowser;' \
313+
'webbrowser.open_new_tab("file://{0}")'.format(palreport)
314+
p = subprocess.Popen([sys.executable, "-c", cmd],
315+
stdout=subprocess.PIPE,
316+
stderr=subprocess.STDOUT).pid
311317

312318
return res
313319

@@ -318,4 +324,4 @@ def runSuite(module, tests):
318324
b = 'TestQgsPalLabelingBase.'
319325
tests = [b + 'test_write_read_settings']
320326
res = runSuite(sys.modules[__name__], tests)
321-
sys.exit(int(not res.wasSuccessful()))
327+
sys.exit(not res.wasSuccessful())

‎tests/src/python/test_qgspallabeling_canvas.py‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def test_default_label(self):
5757
# Verify basic default label placement and text size in points
5858
self.lyr.writeToLayer(self.layer)
5959
self.saveContolImage()
60-
self.assertTrue(self.renderCheck())
60+
self.assertTrue(*self.renderCheck())
6161

6262
def test_text_size_map_unit(self):
6363
# Verify label text size in map units
@@ -67,14 +67,14 @@ def test_text_size_map_unit(self):
6767
self.lyr.textFont = tmpFont
6868
self.lyr.writeToLayer(self.layer)
6969
self.saveContolImage()
70-
self.assertTrue(self.renderCheck())
70+
self.assertTrue(*self.renderCheck())
7171

7272
def test_text_color(self):
7373
# Verify label color change
7474
self.lyr.textColor = Qt.blue
7575
self.lyr.writeToLayer(self.layer)
7676
self.saveContolImage()
77-
self.assertTrue(self.renderCheck())
77+
self.assertTrue(*self.renderCheck())
7878

7979

8080
# class TestQgsPalLabelingLine(TestQgsPalLabeling):
@@ -104,6 +104,6 @@ def test_text_color(self):
104104
sg = 'TestQgsPalLabelingPolygon.'
105105
mf = 'TestQgsPalLabelingMultiFeature.'
106106

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

0 commit comments

Comments
 (0)