Skip to content

Commit

Permalink
[labeling] Unit test cleanup and add ability to output reports on bad…
Browse files Browse the repository at this point in the history
… render checks to web browser
  • Loading branch information
dakcarto committed Aug 13, 2013
1 parent d517461 commit c96e099
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 16 deletions.
4 changes: 2 additions & 2 deletions tests/src/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
48 changes: 39 additions & 9 deletions tests/src/python/test_qgspallabeling_base.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
Expand All @@ -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):

Expand All @@ -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')
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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()))
12 changes: 7 additions & 5 deletions tests/src/python/test_qgspallabeling_canvas.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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.