Skip to content
Permalink
Browse files

[labeling] Unit test cleanup and add ability to output reports on bad…

… render checks to web browser
  • Loading branch information
dakcarto committed Aug 13, 2013
1 parent d517461 commit c96e0991597167a9ad3a95e57724a22c5b2109be
@@ -24,8 +24,8 @@ ADD_PYTHON_TEST(PyQgsComposerLabel test_qgscomposerlabel.py)
ADD_PYTHON_TEST(PyQgsExpression test_qgsexpression.py)
ADD_PYTHON_TEST(PyQgsPalLabelingBase test_qgspallabeling_base.py)
ADD_PYTHON_TEST(PyQgsPalLabelingCanvas test_qgspallabeling_canvas.py)
#ADD_PYTHON_TEST(PyQgsPalLabelingCanvas test_qgspallabeling_composer.py)
#ADD_PYTHON_TEST(PyQgsPalLabelingCanvas test_qgspallabeling_server.py)
#ADD_PYTHON_TEST(PyQgsPalLabelingComposer test_qgspallabeling_composer.py)
#ADD_PYTHON_TEST(PyQgsPalLabelingServer test_qgspallabeling_server.py)
ADD_PYTHON_TEST(PyQgsVectorFileWriter test_qgsvectorfilewriter.py)
ADD_PYTHON_TEST(PyQgsSpatialiteProvider test_qgsspatialiteprovider.py)
ADD_PYTHON_TEST(PyQgsZonalStatistics test_qgszonalstatistics.py)
@@ -1,10 +1,12 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for QgsPalLabeling: base suite setup
.. note:: from build dir: ctest -R PyQgsPalLabelingBase -V
Set env variable PAL_SUITE to run specific tests (define in __main__)
Set env variable PAL_VERBOSE to output individual test summary
Set env variable PAL_CONTROL_IMAGE to trigger building of new control images
From build dir: ctest -R PyQgsPalLabelingBase -V
Set the following env variables when manually running tests:
PAL_SUITE to run specific tests (define in __main__)
PAL_VERBOSE to output individual test summary
PAL_CONTROL_IMAGE to trigger building of new control images
PAL_REPORT to open any failed image check reports in web browser
.. note:: This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -20,15 +22,16 @@
import os
import sys
import glob
import shutil
import StringIO
import webbrowser
from PyQt4.QtCore import *
from PyQt4.QtGui import *

from qgis.core import (
QGis,
QgsCoordinateReferenceSystem,
QgsDataSourceURI,
QgsLogger,
QgsMapLayerRegistry,
QgsMapRenderer,
QgsPalLabeling,
@@ -48,6 +51,14 @@

QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()

PALREPORTDIR = ''
if 'PAL_REPORT' in os.environ:
PALREPORTDIR = os.path.join(str(QDir.tempPath()), 'pal_test_report')
# clear out old reports, if temp dir exists
if os.path.exists(PALREPORTDIR):
shutil.rmtree(PALREPORTDIR, True)
os.mkdir(PALREPORTDIR)


class TestQgsPalLabeling(TestCase):

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

cls._PalReportDir = PALREPORTDIR

# verify that spatialite provider is available
msg = ('\nSpatialite provider not found, '
'SKIPPING TEST SUITE')
@@ -153,7 +166,7 @@ def configTest(self, prefix, abbr):
self._TestFunction = testid[2]
testheader = '\n#####_____ {0}.{1} _____#####\n'.\
format(self._TestGroup, self._TestFunction)
QgsLogger.debug(testheader)
qDebug(testheader)

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

def renderCheck(self):
def renderCheck(self, mismatch=0):
chk = QgsRenderChecker()
chk.setControlPathPrefix('expected_' + self._TestGroupPrefix)
chk.setControlName(self._Test)
chk.setMapRenderer(self._MapRenderer)
res = chk.runTest(self._Test)
res = chk.runTest(self._Test, mismatch)
if self._PalReportDir and not res: # don't report ok checks
testname = self._TestGroup + ' . ' + self._Test
report = '<html>'
report += '<head><title>{0}</title></head>'.format(testname)
report += '<body>' + chk.report().toLocal8Bit() + '</body>'
report += '</html>'
f = QFile(os.path.join(self._PalReportDir, testname + '.html'))
if f.open(QIODevice.ReadWrite | QIODevice.Truncate):
f.write(report)
f.close()
msg = '\nRender check failed for "{0}"'.format(self._Test)
return res, msg

@@ -280,12 +303,19 @@ def runSuite(module, tests):
print '\nIndividual test summary:'
print '\n' + out.getvalue()
out.close()

if PALREPORTDIR:
for report in os.listdir(PALREPORTDIR):
webbrowser.open_new_tab('file://' +
os.path.join(PALREPORTDIR, report))

return res


if __name__ == '__main__':
# NOTE: unless PAL_SUITE env var is set
# all test class methods will be run
b = 'TestQgsPalLabelingBase.'
tests = [b + 'test_write_read_settings']
res = runSuite(sys.modules[__name__], tests)
sys.exit(not res.wasSuccessful())
sys.exit(int(not res.wasSuccessful()))
@@ -1,10 +1,12 @@
# -*- coding: utf-8 -*-
"""QGIS unit tests for QgsPalLabeling: label rendering to screen canvas
.. note:: from build dir: ctest -R PyQgsPalLabelingCanvas -V
Set env variable PAL_SUITE to run specific tests (define in __main__)
Set env variable PAL_VERBOSE to output individual test summary
Set env variable PAL_CONTROL_IMAGE to trigger building of new control images
From build dir: ctest -R PyQgsPalLabelingCanvas -V
Set the following env variables when manually running tests:
PAL_SUITE to run specific tests (define in __main__)
PAL_VERBOSE to output individual test summary
PAL_CONTROL_IMAGE to trigger building of new control images
PAL_REPORT to open any failed image check reports in web browser
.. note:: This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -104,4 +106,4 @@ def test_text_color(self):

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

0 comments on commit c96e099

Please sign in to comment.
You can’t perform that action at this time.