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
1012it under the terms of the GNU General Public License as published by
2022import os
2123import sys
2224import glob
25+ import shutil
2326import StringIO
27+ import webbrowser
2428from PyQt4 .QtCore import *
2529from PyQt4 .QtGui import *
2630
2731from qgis .core import (
2832 QGis ,
2933 QgsCoordinateReferenceSystem ,
3034 QgsDataSourceURI ,
31- QgsLogger ,
3235 QgsMapLayerRegistry ,
3336 QgsMapRenderer ,
3437 QgsPalLabeling ,
4851
4952QGISAPP , 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
5263class 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 = ('\n Spatialite 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 = '\n Render check failed for "{0}"' .format (self ._Test )
210233 return res , msg
211234
@@ -280,12 +303,19 @@ def runSuite(module, tests):
280303 print '\n Individual 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+
285315if __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 () ))
0 commit comments