1
1
# -*- coding: utf-8 -*-
2
2
"""QGIS Unit tests for QgsPalLabeling: base suite setup
3
3
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
8
10
9
11
.. note:: This program is free software; you can redistribute it and/or modify
10
12
it under the terms of the GNU General Public License as published by
20
22
import os
21
23
import sys
22
24
import glob
25
+ import shutil
23
26
import StringIO
27
+ import webbrowser
24
28
from PyQt4 .QtCore import *
25
29
from PyQt4 .QtGui import *
26
30
27
31
from qgis .core import (
28
32
QGis ,
29
33
QgsCoordinateReferenceSystem ,
30
34
QgsDataSourceURI ,
31
- QgsLogger ,
32
35
QgsMapLayerRegistry ,
33
36
QgsMapRenderer ,
34
37
QgsPalLabeling ,
48
51
49
52
QGISAPP , CANVAS , IFACE , PARENT = getQgisTestApp ()
50
53
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
+
51
62
52
63
class TestQgsPalLabeling (TestCase ):
53
64
@@ -67,6 +78,8 @@ def setUpClass(cls):
67
78
cls ._QgisApp , cls ._Canvas , cls ._Iface , cls ._Parent = \
68
79
QGISAPP , CANVAS , IFACE , PARENT
69
80
81
+ cls ._PalReportDir = PALREPORTDIR
82
+
70
83
# verify that spatialite provider is available
71
84
msg = ('\n Spatialite provider not found, '
72
85
'SKIPPING TEST SUITE' )
@@ -153,7 +166,7 @@ def configTest(self, prefix, abbr):
153
166
self ._TestFunction = testid [2 ]
154
167
testheader = '\n #####_____ {0}.{1} _____#####\n ' .\
155
168
format (self ._TestGroup , self ._TestFunction )
156
- QgsLogger . debug (testheader )
169
+ qDebug (testheader )
157
170
158
171
# define the shorthand name of the test (to minimize file name length)
159
172
self ._Test = '{0}_{1}' .format (self ._TestGroupAbbr ,
@@ -200,12 +213,22 @@ def saveContolImage(self):
200
213
self ._Map .render ()
201
214
self ._Canvas .saveAsImage (imgpath )
202
215
203
- def renderCheck (self ):
216
+ def renderCheck (self , mismatch = 0 ):
204
217
chk = QgsRenderChecker ()
205
218
chk .setControlPathPrefix ('expected_' + self ._TestGroupPrefix )
206
219
chk .setControlName (self ._Test )
207
220
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 ()
209
232
msg = '\n Render check failed for "{0}"' .format (self ._Test )
210
233
return res , msg
211
234
@@ -280,12 +303,19 @@ def runSuite(module, tests):
280
303
print '\n Individual test summary:'
281
304
print '\n ' + out .getvalue ()
282
305
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
+
283
312
return res
284
313
314
+
285
315
if __name__ == '__main__' :
286
316
# NOTE: unless PAL_SUITE env var is set
287
317
# all test class methods will be run
288
318
b = 'TestQgsPalLabelingBase.'
289
319
tests = [b + 'test_write_read_settings' ]
290
320
res = runSuite (sys .modules [__name__ ], tests )
291
- sys .exit (not res .wasSuccessful ())
321
+ sys .exit (int ( not res .wasSuccessful () ))
0 commit comments