Skip to content

Commit

Permalink
Use base class render check methods in more tests, avoids
Browse files Browse the repository at this point in the history
tests which pollute test failure report with unnecessary noise
  • Loading branch information
nyalldawson committed Oct 6, 2023
1 parent 3eddef5 commit b552c15
Show file tree
Hide file tree
Showing 23 changed files with 458 additions and 839 deletions.
11 changes: 9 additions & 2 deletions python/testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ def image_check(cls,
image: QImage,
control_name=None,
color_tolerance: int = 2,
allowed_mismatch: int = 20) -> bool:
allowed_mismatch: int = 20,
size_tolerance: Optional[int] = None,
expect_fail: bool = False) -> bool:
temp_dir = QDir.tempPath() + '/'
file_name = temp_dir + name + ".png"
image.save(file_name, "PNG")
Expand All @@ -125,8 +127,13 @@ def image_check(cls,
checker.setControlName(control_name or "expected_" + reference_image)
checker.setRenderedImage(file_name)
checker.setColorTolerance(color_tolerance)
checker.setExpectFail(expect_fail)
if size_tolerance is not None:
checker.setSizeTolerance(size_tolerance, size_tolerance)

result = checker.runTest(name, allowed_mismatch)
if not result:
if (not expect_fail and not result) or \
(expect_fail and result):
cls.report += f"<h2>Render {name}</h2>\n"
cls.report += checker.report()

Expand Down
1 change: 1 addition & 0 deletions src/core/qgsmultirenderchecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ bool QgsMultiRenderChecker::runTest( const QString &testName, unsigned int misma
checker.setControlPathSuffix( suffix );
checker.setControlName( mControlName );
checker.setMapSettings( mMapSettings );
checker.setExpectFail( mExpectFail );

if ( !mRenderedImage.isNull() )
{
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsrenderchecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void QgsRenderChecker::setControlImagePath( const QString &path )

QString QgsRenderChecker::report( bool ignoreSuccess ) const
{
return ( ignoreSuccess && mResult ) ? QString() : mReport;
return ( ( ignoreSuccess && mResult ) || ( mExpectFail && !mResult ) ) ? QString() : mReport;
}

void QgsRenderChecker::setControlName( const QString &name )
Expand Down
8 changes: 0 additions & 8 deletions tests/src/python/test_qgsannotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,6 @@ class TestQgsAnnotation(QgisTestCase):
def control_path_prefix(cls):
return "annotations"

def setUp(self):
self.report = "<h1>Python QgsAnnotation Tests</h1>\n"

def tearDown(self):
report_file_path = f"{QDir.tempPath()}/qgistest.html"
with open(report_file_path, 'a') as report_file:
report_file.write(self.report)

def testTextAnnotation(self):
""" test rendering a text annotation"""
a = QgsTextAnnotation()
Expand Down
35 changes: 6 additions & 29 deletions tests/src/python/test_qgsannotationlayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
__copyright__ = 'Copyright 2020, The QGIS Project'

import qgis # NOQA
from qgis.PyQt.QtCore import QDir, QSize, QTemporaryDir
from qgis.PyQt.QtCore import QSize, QTemporaryDir
from qgis.PyQt.QtGui import QColor, QImage, QPainter
from qgis.PyQt.QtXml import QDomDocument
from qgis.core import (
Expand All @@ -38,7 +38,6 @@
QgsProject,
QgsReadWriteContext,
QgsRectangle,
QgsRenderChecker,
QgsRenderContext,
QgsVertexId,
)
Expand All @@ -54,16 +53,8 @@
class TestQgsAnnotationLayer(QgisTestCase):

@classmethod
def setUpClass(cls):
super().setUpClass()
cls.report = "<h1>Python QgsAnnotationLayer Tests</h1>\n"

@classmethod
def tearDownClass(cls):
report_file_path = f"{QDir.tempPath()}/qgistest.html"
with open(report_file_path, 'a') as report_file:
report_file.write(cls.report)
super().tearDownClass()
def control_path_prefix(cls):
return 'annotation_layer'

def testItems(self):
layer = QgsAnnotationLayer('test', QgsAnnotationLayer.LayerOptions(QgsProject.instance().transformContext()))
Expand Down Expand Up @@ -357,7 +348,7 @@ def testRenderLayer(self):
finally:
painter.end()

self.assertTrue(self.imageCheck('layer_render', 'layer_render', image))
self.assertTrue(self.image_check('layer_render', 'layer_render', image))

# also check details of rendered items
item_details = renderer.takeRenderedItemDetails()
Expand Down Expand Up @@ -417,7 +408,7 @@ def testRenderWithTransform(self):
finally:
painter.end()

self.assertTrue(self.imageCheck('layer_render_transform', 'layer_render_transform', image))
self.assertTrue(self.image_check('layer_render_transform', 'layer_render_transform', image))

# also check details of rendered items
item_details = renderer.takeRenderedItemDetails()
Expand Down Expand Up @@ -482,7 +473,7 @@ def testRenderLayerWithReferenceScale(self):
finally:
painter.end()

self.assertTrue(self.imageCheck('layer_render_reference_scale', 'layer_render_reference_scale', image))
self.assertTrue(self.image_check('layer_render_reference_scale', 'layer_render_reference_scale', image))

# also check details of rendered items
item_details = renderer.takeRenderedItemDetails()
Expand Down Expand Up @@ -607,20 +598,6 @@ def test_render_via_job_with_transform(self):
self.assertTrue(compareWkt(result, expected, tol=1000), "mismatch Expected:\n{}\nGot:\n{}\n".format(expected,
result))

def imageCheck(self, name, reference_image, image):
TestQgsAnnotationLayer.report += f"<h2>Render {name}</h2>\n"
temp_dir = QDir.tempPath() + '/'
file_name = temp_dir + 'patch_' + name + ".png"
image.save(file_name, "PNG")
checker = QgsRenderChecker()
checker.setControlPathPrefix("annotation_layer")
checker.setControlName("expected_" + reference_image)
checker.setRenderedImage(file_name)
checker.setColorTolerance(2)
result = checker.compareImages(name, 20)
TestQgsAnnotationLayer.report += checker.report()
return result


if __name__ == '__main__':
unittest.main()
35 changes: 6 additions & 29 deletions tests/src/python/test_qgsannotationlineitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
__copyright__ = 'Copyright 2020, The QGIS Project'

import qgis # NOQA
from qgis.PyQt.QtCore import QDir, QSize
from qgis.PyQt.QtCore import QSize
from qgis.PyQt.QtGui import QColor, QImage, QPainter
from qgis.PyQt.QtXml import QDomDocument
from qgis.core import (
Expand All @@ -34,7 +34,6 @@
QgsProject,
QgsReadWriteContext,
QgsRectangle,
QgsRenderChecker,
QgsRenderContext,
QgsVertexId,
)
Expand All @@ -50,16 +49,8 @@
class TestQgsAnnotationLineItem(QgisTestCase):

@classmethod
def setUpClass(cls):
super().setUpClass()
cls.report = "<h1>Python QgsAnnotationLineItem Tests</h1>\n"

@classmethod
def tearDownClass(cls):
report_file_path = f"{QDir.tempPath()}/qgistest.html"
with open(report_file_path, 'a') as report_file:
report_file.write(cls.report)
super().tearDownClass()
def control_path_prefix(cls):
return "annotation_layer"

def testBasic(self):
item = QgsAnnotationLineItem(QgsLineString([QgsPoint(12, 13), QgsPoint(14, 13), QgsPoint(14, 15)]))
Expand Down Expand Up @@ -194,7 +185,7 @@ def testRenderLineString(self):
finally:
painter.end()

self.assertTrue(self.imageCheck('linestring_item', 'linestring_item', image))
self.assertTrue(self.image_check('linestring_item', 'linestring_item', image))

def testRenderCurve(self):
item = QgsAnnotationLineItem(QgsCircularString(QgsPoint(12, 13.2), QgsPoint(14, 13.4), QgsPoint(14, 15)))
Expand All @@ -220,7 +211,7 @@ def testRenderCurve(self):
finally:
painter.end()

self.assertTrue(self.imageCheck('line_circularstring', 'line_circularstring', image))
self.assertTrue(self.image_check('line_circularstring', 'line_circularstring', image))

def testRenderWithTransform(self):
item = QgsAnnotationLineItem(QgsLineString([QgsPoint(11, 13), QgsPoint(12, 13), QgsPoint(12, 15)]))
Expand All @@ -247,21 +238,7 @@ def testRenderWithTransform(self):
finally:
painter.end()

self.assertTrue(self.imageCheck('linestring_item_transform', 'linestring_item_transform', image))

def imageCheck(self, name, reference_image, image):
TestQgsAnnotationLineItem.report += f"<h2>Render {name}</h2>\n"
temp_dir = QDir.tempPath() + '/'
file_name = temp_dir + 'patch_' + name + ".png"
image.save(file_name, "PNG")
checker = QgsRenderChecker()
checker.setControlPathPrefix("annotation_layer")
checker.setControlName("expected_" + reference_image)
checker.setRenderedImage(file_name)
checker.setColorTolerance(2)
result = checker.compareImages(name, 20)
TestQgsAnnotationLineItem.report += checker.report()
return result
self.assertTrue(self.image_check('linestring_item_transform', 'linestring_item_transform', image))


if __name__ == '__main__':
Expand Down
41 changes: 9 additions & 32 deletions tests/src/python/test_qgsannotationlinetextitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
__copyright__ = 'Copyright 2020, The QGIS Project'

import qgis # NOQA
from qgis.PyQt.QtCore import QDir, QSize, Qt
from qgis.PyQt.QtCore import QSize
from qgis.PyQt.QtGui import QColor, QImage, QPainter
from qgis.PyQt.QtXml import QDomDocument
from qgis.core import (
Expand All @@ -31,7 +31,6 @@
QgsProject,
QgsReadWriteContext,
QgsRectangle,
QgsMultiRenderChecker,
QgsRenderContext,
QgsTextFormat,
QgsVertexId,
Expand All @@ -50,16 +49,8 @@
class TestQgsAnnotationLineTextItem(QgisTestCase):

@classmethod
def setUpClass(cls):
super().setUpClass()
cls.report = "<h1>Python QgsAnnotationLineTextItem Tests</h1>\n"

@classmethod
def tearDownClass(cls):
report_file_path = f"{QDir.tempPath()}/qgistest.html"
with open(report_file_path, 'a') as report_file:
report_file.write(cls.report)
super().tearDownClass()
def control_path_prefix(cls):
return "annotation_layer"

def testBasic(self):
item = QgsAnnotationLineTextItem('my text', QgsLineString(((12, 13), (13, 13.1), (14, 13))))
Expand Down Expand Up @@ -242,7 +233,7 @@ def testRenderLine(self):
finally:
painter.end()

self.assertTrue(self.imageCheck('linetext_item', 'linetext_item', image))
self.assertTrue(self.image_check('linetext_item', 'linetext_item', image))

def testRenderLineOffsetPositive(self):
item = QgsAnnotationLineTextItem('my text', QgsLineString(((12, 13), (13, 13.1), (14, 12))))
Expand Down Expand Up @@ -276,7 +267,7 @@ def testRenderLineOffsetPositive(self):
finally:
painter.end()

self.assertTrue(self.imageCheck('linetext_item_offset_positive', 'linetext_item_offset_positive', image))
self.assertTrue(self.image_check('linetext_item_offset_positive', 'linetext_item_offset_positive', image))

def testRenderLineOffsetNegative(self):
item = QgsAnnotationLineTextItem('my text', QgsLineString(((12, 13), (13, 13.1), (14, 12))))
Expand Down Expand Up @@ -310,7 +301,7 @@ def testRenderLineOffsetNegative(self):
finally:
painter.end()

self.assertTrue(self.imageCheck('linetext_item_offset_negative', 'linetext_item_offset_negative', image))
self.assertTrue(self.image_check('linetext_item_offset_negative', 'linetext_item_offset_negative', image))

def testRenderLineTruncate(self):
item = QgsAnnotationLineTextItem('my text', QgsLineString(((12, 13), (13, 13.1), (14, 12))))
Expand Down Expand Up @@ -342,7 +333,7 @@ def testRenderLineTruncate(self):
finally:
painter.end()

self.assertTrue(self.imageCheck('linetext_item_truncate', 'linetext_item_truncate', image))
self.assertTrue(self.image_check('linetext_item_truncate', 'linetext_item_truncate', image))

def testRenderLineTextExpression(self):
item = QgsAnnotationLineTextItem('[% 1 + 1.5 %]', QgsLineString(((12, 13), (13, 13.1), (14, 12))))
Expand Down Expand Up @@ -374,7 +365,7 @@ def testRenderLineTextExpression(self):
finally:
painter.end()

self.assertTrue(self.imageCheck('linetext_item_expression', 'linetext_item_expression', image))
self.assertTrue(self.image_check('linetext_item_expression', 'linetext_item_expression', image))

def testRenderWithTransform(self):
item = QgsAnnotationLineTextItem('my text', QgsLineString(
Expand Down Expand Up @@ -408,21 +399,7 @@ def testRenderWithTransform(self):
finally:
painter.end()

self.assertTrue(self.imageCheck('linetext_item_transform', 'linetext_item_transform', image))

def imageCheck(self, name, reference_image, image):
TestQgsAnnotationLineTextItem.report += f"<h2>Render {name}</h2>\n"
temp_dir = QDir.tempPath() + '/'
file_name = temp_dir + 'annotation_' + name + ".png"
image.save(file_name, "PNG")
checker = QgsMultiRenderChecker()
checker.setControlPathPrefix("annotation_layer")
checker.setControlName("expected_" + reference_image)
checker.setRenderedImage(file_name)
checker.setColorTolerance(2)
result = checker.runTest(name, 20)
TestQgsAnnotationLineTextItem.report += checker.report()
return result
self.assertTrue(self.image_check('linetext_item_transform', 'linetext_item_transform', image))


if __name__ == '__main__':
Expand Down
33 changes: 5 additions & 28 deletions tests/src/python/test_qgsannotationmarkeritem.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
__copyright__ = 'Copyright 2020, The QGIS Project'

import qgis # NOQA
from qgis.PyQt.QtCore import QDir, QSize
from qgis.PyQt.QtCore import QSize
from qgis.PyQt.QtGui import QColor, QImage, QPainter
from qgis.PyQt.QtXml import QDomDocument
from qgis.core import (
Expand All @@ -32,7 +32,6 @@
QgsProject,
QgsReadWriteContext,
QgsRectangle,
QgsRenderChecker,
QgsRenderContext,
QgsVertexId,
)
Expand All @@ -48,16 +47,8 @@
class TestQgsAnnotationMarkerItem(QgisTestCase):

@classmethod
def setUpClass(cls):
super().setUpClass()
cls.report = "<h1>Python QgsAnnotationMarkerItem Tests</h1>\n"

@classmethod
def tearDownClass(cls):
report_file_path = f"{QDir.tempPath()}/qgistest.html"
with open(report_file_path, 'a') as report_file:
report_file.write(cls.report)
super().tearDownClass()
def control_path_prefix(cls):
return "annotation_layer"

def testBasic(self):
item = QgsAnnotationMarkerItem(QgsPoint(12, 13))
Expand Down Expand Up @@ -180,7 +171,7 @@ def testRenderMarker(self):
finally:
painter.end()

self.assertTrue(self.imageCheck('marker_item', 'marker_item', image))
self.assertTrue(self.image_check('marker_item', 'marker_item', image))

def testRenderWithTransform(self):
item = QgsAnnotationMarkerItem(QgsPoint(12, 13))
Expand All @@ -207,21 +198,7 @@ def testRenderWithTransform(self):
finally:
painter.end()

self.assertTrue(self.imageCheck('marker_item_transform', 'marker_item_transform', image))

def imageCheck(self, name, reference_image, image):
TestQgsAnnotationMarkerItem.report += f"<h2>Render {name}</h2>\n"
temp_dir = QDir.tempPath() + '/'
file_name = temp_dir + 'patch_' + name + ".png"
image.save(file_name, "PNG")
checker = QgsRenderChecker()
checker.setControlPathPrefix("annotation_layer")
checker.setControlName("expected_" + reference_image)
checker.setRenderedImage(file_name)
checker.setColorTolerance(2)
result = checker.compareImages(name, 20)
TestQgsAnnotationMarkerItem.report += checker.report()
return result
self.assertTrue(self.image_check('marker_item_transform', 'marker_item_transform', image))


if __name__ == '__main__':
Expand Down
2 changes: 1 addition & 1 deletion tests/src/python/test_qgsannotationpointtextitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
__copyright__ = 'Copyright 2020, The QGIS Project'

import qgis # NOQA
from qgis.PyQt.QtCore import QDir, QSize, Qt
from qgis.PyQt.QtCore import QSize, Qt
from qgis.PyQt.QtGui import QColor, QImage, QPainter
from qgis.PyQt.QtXml import QDomDocument
from qgis.core import (
Expand Down
Loading

0 comments on commit b552c15

Please sign in to comment.