From c21889f945fe01bfecbd88caad5998141e33492b Mon Sep 17 00:00:00 2001 From: Matthias Kuhn Date: Wed, 6 Jan 2016 13:44:29 +0100 Subject: [PATCH] Add qgis.testing module for generic qgis test helpers --- ci/travis/linux/before_install.sh | 1 + ci/travis/osx/before_install.sh | 2 +- python/CMakeLists.txt | 1 + python/testing/CMakeLists.txt | 24 +++ python/testing/__init__.py | 197 ++++++++++++++++++ python/testing/mocked.py | 67 ++++++ tests/src/python/test_provider_memory.py | 39 ++-- tests/src/python/test_provider_mssql.py | 47 +++-- tests/src/python/test_provider_postgres.py | 14 +- tests/src/python/test_provider_shapefile.py | 14 +- tests/src/python/test_provider_spatialite.py | 14 +- tests/src/python/test_provider_tabfile.py | 14 +- tests/src/python/test_provider_virtual.py | 16 +- tests/src/python/test_qgis_local_server.py | 14 +- tests/src/python/test_qgsanalysis.py | 23 +- tests/src/python/test_qgsapplication.py | 4 +- tests/src/python/test_qgsappstartup.py | 3 +- tests/src/python/test_qgsatlascomposition.py | 6 +- .../src/python/test_qgsattributetablemodel.py | 13 +- tests/src/python/test_qgsauthsystem.py | 9 +- tests/src/python/test_qgsblendmodes.py | 21 +- .../test_qgscategorizedsymbolrendererv2.py | 11 +- tests/src/python/test_qgscolorscheme.py | 4 +- .../src/python/test_qgscolorschemeregistry.py | 4 +- tests/src/python/test_qgscomposereffects.py | 13 +- tests/src/python/test_qgscomposerhtml.py | 25 ++- tests/src/python/test_qgscomposerlabel.py | 6 +- tests/src/python/test_qgscomposermap.py | 16 +- tests/src/python/test_qgscomposermapgrid.py | 13 +- tests/src/python/test_qgscomposerpicture.py | 13 +- tests/src/python/test_qgscomposershapes.py | 13 +- tests/src/python/test_qgscomposition.py | 24 +-- tests/src/python/test_qgsconditionalstyle.py | 20 +- .../src/python/test_qgscoordinatetransform.py | 12 +- .../python/test_qgsdelimitedtextprovider.py | 56 ++--- tests/src/python/test_qgsdistancearea.py | 9 +- tests/src/python/test_qgsdoccoverage.py | 9 +- tests/src/python/test_qgseditwidgets.py | 15 +- tests/src/python/test_qgsexpression.py | 4 +- tests/src/python/test_qgsfeature.py | 12 +- tests/src/python/test_qgsfeatureiterator.py | 11 +- tests/src/python/test_qgsfield.py | 14 +- tests/src/python/test_qgsfontutils.py | 13 +- tests/src/python/test_qgsgeometry.py | 40 ++-- .../test_qgsgeometry_avoid_intersections.py | 6 +- .../test_qgsgeometrygeneratorsymbollayerv2.py | 46 ++-- .../test_qgsgraduatedsymbolrendererv2.py | 11 +- tests/src/python/test_qgsissue7244.py | 11 +- tests/src/python/test_qgslayerdefinition.py | 10 +- tests/src/python/test_qgslogger.py | 8 +- tests/src/python/test_qgsmaplayerregistry.py | 2 +- tests/src/python/test_qgsmapunitscale.py | 4 +- .../python/test_qgsnetworkcontentfetcher.py | 5 +- tests/src/python/test_qgspallabeling_base.py | 20 +- .../python/test_qgspallabeling_placement.py | 2 +- tests/src/python/test_qgspoint.py | 6 +- tests/src/python/test_qgsproject.py | 6 +- tests/src/python/test_qgsrangewidgets.py | 13 +- tests/src/python/test_qgsrasterfilewriter.py | 18 +- tests/src/python/test_qgsrasterlayer.py | 12 +- tests/src/python/test_qgsrectangle.py | 20 +- tests/src/python/test_qgsrelation.py | 11 +- .../src/python/test_qgsrelationeditwidget.py | 18 +- tests/src/python/test_qgsrulebasedrenderer.py | 19 +- tests/src/python/test_qgsserver.py | 2 +- .../python/test_qgsserver_accesscontrol.py | 6 +- .../python/test_qgssinglesymbolrenderer.py | 20 +- tests/src/python/test_qgssipcoverage.py | 9 +- tests/src/python/test_qgsspatialindex.py | 7 +- .../test_qgssymbolexpressionvariables.py | 50 +++-- tests/src/python/test_qgssymbollayerv2.py | 84 ++++---- tests/src/python/test_qgssymbolv2.py | 17 +- tests/src/python/test_qgsvectorcolorramp.py | 4 +- tests/src/python/test_qgsvectorfilewriter.py | 16 +- tests/src/python/test_qgsvectorlayer.py | 13 +- .../python/test_qgsvirtuallayerdefinition.py | 4 +- tests/src/python/test_qgszonalstatistics.py | 15 +- tests/src/python/test_syntactic_sugar.py | 11 +- tests/src/python/utilities.py | 73 +------ 79 files changed, 865 insertions(+), 614 deletions(-) create mode 100644 python/testing/CMakeLists.txt create mode 100644 python/testing/__init__.py create mode 100644 python/testing/mocked.py diff --git a/ci/travis/linux/before_install.sh b/ci/travis/linux/before_install.sh index 95377ae6cfe..e967c03c416 100755 --- a/ci/travis/linux/before_install.sh +++ b/ci/travis/linux/before_install.sh @@ -61,3 +61,4 @@ sudo apt-get install --force-yes --no-install-recommends --no-install-suggests \ postgresql-9.1-postgis-2.1/precise # from ubuntugis-unstable, not pgdg sudo -H pip install autopep8 # TODO when switching to trusty or above: replace python-pip with python-autopep8 +sudo -H pip install nose2 pyyaml mock diff --git a/ci/travis/osx/before_install.sh b/ci/travis/osx/before_install.sh index fb2f2a5f9ee..887126bfeda 100755 --- a/ci/travis/osx/before_install.sh +++ b/ci/travis/osx/before_install.sh @@ -18,4 +18,4 @@ mkdir -p /Users/travis/Library/Python/2.7/lib/python/site-packages echo 'import site; site.addsitedir("/usr/local/lib/python2.7/site-packages")' >> /Users/travis/Library/Python/2.7/lib/python/site-packages/homebrew.pth # Needed for Processing -pip install psycopg2 numpy +pip install psycopg2 numpy nose2 pyyaml mock diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt index 5a16662f994..1d045455a37 100644 --- a/python/CMakeLists.txt +++ b/python/CMakeLists.txt @@ -59,6 +59,7 @@ ADD_SUBDIRECTORY(console) ADD_SUBDIRECTORY(PyQt) ADD_SUBDIRECTORY(pyplugin_installer) ADD_SUBDIRECTORY(ext-libs) +ADD_SUBDIRECTORY(testing) IF(POLICY CMP0040) CMAKE_POLICY (POP) # see PUSH above diff --git a/python/testing/CMakeLists.txt b/python/testing/CMakeLists.txt new file mode 100644 index 00000000000..8a5d1b4c80d --- /dev/null +++ b/python/testing/CMakeLists.txt @@ -0,0 +1,24 @@ +# See ../CMakeLists.txt for info on staged-plugins* and clean-staged-plugins targets + +SET (QGIS_PYTHON_DIR ${QGIS_DATA_DIR}/python) +SET (PYTHON_OUTPUT_DIRECTORY ${QGIS_OUTPUT_DIRECTORY}/python) + +SET(PY_FILES + __init__.py + mocked.py +) + +FILE (MAKE_DIRECTORY ${QGIS_PYTHON_OUTPUT_DIRECTORY}/testing) +INSTALL(FILES ${PY_FILES} DESTINATION "${QGIS_PYTHON_DIR}/testing") + +ADD_CUSTOM_TARGET(pytesting ALL) +# stage to output to make available when QGIS is run from build directory +FOREACH(pyfile ${PY_FILES}) + ADD_CUSTOM_COMMAND(TARGET pytesting + POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy ${pyfile} "${QGIS_PYTHON_OUTPUT_DIRECTORY}/testing" + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + DEPENDS ${pyfile} + ) + PY_COMPILE(pyutils "${QGIS_PYTHON_OUTPUT_DIRECTORY}/testing/${pyfile}") +ENDFOREACH(pyfile) diff --git a/python/testing/__init__.py b/python/testing/__init__.py new file mode 100644 index 00000000000..ea26676c61b --- /dev/null +++ b/python/testing/__init__.py @@ -0,0 +1,197 @@ +# -*- coding: utf-8 -*- + +""" +*************************************************************************** + __init__.py + --------------------- + Date : January 2016 + Copyright : (C) 2016 by Matthias Kuhn + Email : matthias@opengis.ch +*************************************************************************** +* * +* 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 * +* the Free Software Foundation; either version 2 of the License, or * +* (at your option) any later version. * +* * +*************************************************************************** +""" + +__author__ = 'Matthias Kuhn' +__date__ = 'January 2016' +__copyright__ = '(C) 2016, Matthias Kuhn' + +# This will get replaced with a git SHA1 when you do a git archive + +__revision__ = ':%H$' + +import os +import sys + +from PyQt4.QtCore import QVariant +from qgis.core import QgsApplication, QgsFeatureRequest, QgsVectorLayer +from nose2.compat import unittest + +# Get a backup, we will patch this one later +_TestCase = unittest.TestCase + + +class TestCase(_TestCase): + + def assertLayersEqual(self, layer1, layer2, **kwargs): + """ + :param layer1: The first layer to compare + :param layer2: The second layer to compare + :param request: Optional, A feature request. This can be used to specify + an order by clause to make sure features are compared in + a given sequence if they don't match by default. + """ + + try: + request = kwargs['request'] + except KeyError: + request = QgsFeatureRequest() + + try: + compare = kwargs['compare'] + except KeyError: + compare = {} + + # Compare fields + _TestCase.assertEqual(self, layer1.fields().count(), layer2.fields().count()) + for fieldnum in range(layer1.fields().count()): + field1 = layer1.fields().at(fieldnum) + field2 = layer2.fields().at(fieldnum) + _TestCase.assertEqual(self, field1.name(), field2.name()) + # _TestCase.assertEqual(self, field1.type(), field2.type(), 'Field "{}" is not equal: {}({}) != {}({})'.format(field1.name(), field1.typeName(), field1.type(), field2.typeName(), field2.type())) + + # Compare CRS + _TestCase.assertEqual(self, layer1.dataProvider().crs().authid(), layer2.dataProvider().crs().authid()) + + # Compare features + _TestCase.assertEqual(self, layer1.featureCount(), layer2.featureCount()) + + try: + precision = compare['geometry']['precision'] + except KeyError: + precision = 17 + + for feats in zip(layer1.getFeatures(request), layer2.getFeatures(request)): + if feats[0].geometry() is not None: + geom0 = feats[0].geometry().geometry().asWkt(precision) + else: + geom0 = None + if feats[1].geometry() is not None: + geom1 = feats[1].geometry().geometry().asWkt(precision) + else: + geom1 = None + _TestCase.assertEqual( + self, + geom0, + geom1, + 'Features {}/{} differ in geometry: \n\n {}\n\n vs \n\n {}'.format( + feats[0].id(), + feats[1].id(), + geom0, + geom1 + ) + ) + + for attr0, attr1, field1, field2 in zip(feats[0].attributes(), feats[1].attributes(), layer1.fields().toList(), layer2.fields().toList()): + try: + cmp = compare['fields'][field1.name()] + except KeyError: + try: + cmp = compare['fields']['__all__'] + except KeyError: + cmp = {} + + # Skip field + if 'skip' in cmp: + continue + + # Cast field to a given type + if 'cast' in cmp: + if cmp['cast'] == 'int': + attr0 = int(attr0) if attr0 else None + attr1 = int(attr1) if attr0 else None + if cmp['cast'] == 'float': + attr0 = float(attr0) if attr0 else None + attr1 = float(attr1) if attr0 else None + if cmp['cast'] == 'str': + attr0 = str(attr0) + attr1 = str(attr1) + + # Round field (only numeric so it works with __all__) + if 'precision' in cmp and field1.type() in [QVariant.Int, QVariant.Double, QVariant.LongLong]: + attr0 = round(attr0, cmp['precision']) + attr1 = round(attr1, cmp['precision']) + + _TestCase.assertEqual( + self, + attr0, + attr1, + 'Features {}/{} differ in attributes\n\n * Field1: {} ({})\n * Field2: {} ({})\n\n * {} != {}'.format(feats[0].id(), + feats[1].id(), + field1.name(), + field1.typeName(), + field2.name(), + field2.typeName(), + repr(attr0), + repr(attr1) + ) + ) + +# Patch unittest +unittest.TestCase = TestCase + + +def start_app(): + """ + Will start a QgsApplication and call all initialization code like + registering the providers and other infrastructure. It will not load + any plugins. + + You can always get the reference to a running app by calling `QgsApplication.instance()`. + + The initialization will only happen once, so it is safe to call this method repeatedly. + + Returns + ------- + QgsApplication + + A QgsApplication singleton + """ + global QGISAPP + + try: + QGISAPP + except NameError: + myGuiFlag = True # All test will run qgis in gui mode + + # In python3 we need to convert to a bytes object (or should + # QgsApplication accept a QString instead of const char* ?) + try: + argvb = list(map(os.fsencode, sys.argv)) + except AttributeError: + argvb = sys.argv + + # Note: QGIS_PREFIX_PATH is evaluated in QgsApplication - + # no need to mess with it here. + QGISAPP = QgsApplication(argvb, myGuiFlag) + + QGISAPP.initQgis() + s = QGISAPP.showSettings() + print(s) + + return QGISAPP + + +def stop_app(): + """ + Cleans up and exits QGIS + """ + global QGISAPP + + QGISAPP.exitQgis() + del QGISAPP diff --git a/python/testing/mocked.py b/python/testing/mocked.py new file mode 100644 index 00000000000..403b81605cb --- /dev/null +++ b/python/testing/mocked.py @@ -0,0 +1,67 @@ +# -*- coding: utf-8 -*- + +""" +*************************************************************************** + mocked + --------------------- + Date : January 2016 + Copyright : (C) 2016 by Matthias Kuhn + Email : matthias@opengis.ch +*************************************************************************** +* * +* 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 * +* the Free Software Foundation; either version 2 of the License, or * +* (at your option) any later version. * +* * +*************************************************************************** +""" + +__author__ = 'Matthias Kuhn' +__date__ = 'January 2016' +__copyright__ = '(C) 2016, Matthias Kuhn' + +# This will get replaced with a git SHA1 when you do a git archive + +__revision__ = ':%H$' + +import os +import sys +import mock + +from qgis.gui import QgisInterface, QgsMapCanvas +from qgis.core import QgsApplication + +from PyQt4.QtGui import QMainWindow +from PyQt4.QtCore import QSize + +from qgis.testing import start_app + + +def get_iface(): + """ + Will return a mock QgisInterface object with some methods implemented in a generic way. + + You can further control its behavior + by using the mock infrastructure. Refer to https://docs.python.org/3/library/unittest.mock.html + for more details. + + Returns + ------- + QgisInterface + + A mock QgisInterface + """ + + start_app() + + my_iface = mock.Mock(spec=QgisInterface) + + my_iface.mainWindow.return_value = QMainWindow() + + canvas = QgsMapCanvas(my_iface.mainWindow()) + canvas.resize(QSize(400, 400)) + + my_iface.mapCanvas.return_value = canvas + + return my_iface diff --git a/tests/src/python/test_provider_memory.py b/tests/src/python/test_provider_memory.py index 7cd6d039854..85c22ae4393 100644 --- a/tests/src/python/test_provider_memory.py +++ b/tests/src/python/test_provider_memory.py @@ -17,23 +17,37 @@ import tempfile import shutil import glob -from qgis.core import QGis, QgsField, QgsPoint, QgsMapLayer, QgsVectorLayer, QgsFeatureRequest, QgsFeature, QgsProviderRegistry, \ - QgsGeometry, NULL -from PyQt4.QtCore import QSettings -from utilities import (unitTestDataPath, - getQgisTestApp, - unittest, - TestCase, - compareWkt - ) +from qgis.core import ( + QGis, + QgsField, + QgsPoint, + QgsMapLayer, + QgsVectorLayer, + QgsFeatureRequest, + QgsFeature, + QgsProviderRegistry, + QgsGeometry, + NULL +) + +from qgis.testing import ( + start_app, + unittest +) + +from utilities import ( + unitTestDataPath, + compareWkt +) + from providertestbase import ProviderTestCase from PyQt4.QtCore import QVariant -QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() +start_app() TEST_DATA_DIR = unitTestDataPath() -class TestPyQgsMemoryProvider(TestCase, ProviderTestCase): +class TestPyQgsMemoryProvider(unittest.TestCase, ProviderTestCase): @classmethod def setUpClass(cls): @@ -241,7 +255,8 @@ class TestPyQgsMemoryProvider(TestCase, ProviderTestCase): assert f == importedFields.field(f.name()) -class TestPyQgsMemoryProviderIndexed(TestCase, ProviderTestCase): +class TestPyQgsMemoryProviderIndexed(unittest.TestCase, ProviderTestCase): + """Runs the provider test suite against an indexed memory layer""" @classmethod diff --git a/tests/src/python/test_provider_mssql.py b/tests/src/python/test_provider_mssql.py index 96eea175439..a72feac6f5a 100644 --- a/tests/src/python/test_provider_mssql.py +++ b/tests/src/python/test_provider_mssql.py @@ -17,25 +17,38 @@ import os import sys from qgis.core import NULL -from qgis.core import QgsVectorLayer, QgsFeatureRequest, QgsFeature, QgsProviderRegistry -from PyQt4.QtCore import QSettings, QDate, QTime, QDateTime, QVariant -from utilities import (unitTestDataPath, - getQgisTestApp, - unittest, - TestCase - ) +from qgis.core import ( + QgsVectorLayer, + QgsFeatureRequest, + QgsFeature, + QgsProviderRegistry + + +from PyQt4.QtCore import ( + QSettings, + QDate, + QTime, + QDateTime, + QVariant +) + +from utilities import unitTestDataPath +from qgis.testing import ( + start_app, + unittest +) from providertestbase import ProviderTestCase -QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() -TEST_DATA_DIR = unitTestDataPath() +start_app() +TEST_DATA_DIR=unitTestDataPath() -class TestPyQgsMssqlProvider(TestCase, ProviderTestCase): +class TestPyQgsMssqlProvider(unittest.TestCase, ProviderTestCase): @classmethod def setUpClass(cls): """Run before all tests""" - cls.dbconn = u"dbname='gis' host=localhost\sqlexpress" + cls.dbconn=u"dbname='gis' host=localhost\sqlexpress" if 'QGIS_MSSQLTEST_DB' in os.environ: cls.dbconn = os.environ['QGIS_MSSQLTEST_DB'] # Create test layers @@ -58,23 +71,23 @@ class TestPyQgsMssqlProvider(TestCase, ProviderTestCase): # HERE GO THE PROVIDER SPECIFIC TESTS def testDateTimeTypes(self): - vl = QgsVectorLayer('%s table="qgis_test"."date_times" sql=' % (self.dbconn), "testdatetimes", "mssql") + vl=QgsVectorLayer('%s table="qgis_test"."date_times" sql=' % (self.dbconn), "testdatetimes", "mssql") assert(vl.isValid()) - fields = vl.dataProvider().fields() + fields=vl.dataProvider().fields() self.assertEqual(fields.at(fields.indexFromName('date_field')).type(), QVariant.Date) self.assertEqual(fields.at(fields.indexFromName('time_field')).type(), QVariant.Time) self.assertEqual(fields.at(fields.indexFromName('datetime_field')).type(), QVariant.DateTime) - f = vl.getFeatures(QgsFeatureRequest()).next() + f=vl.getFeatures(QgsFeatureRequest()).next() - date_idx = vl.fieldNameIndex('date_field') + date_idx=vl.fieldNameIndex('date_field') assert isinstance(f.attributes()[date_idx], QDate) self.assertEqual(f.attributes()[date_idx], QDate(2004, 3, 4)) - time_idx = vl.fieldNameIndex('time_field') + time_idx=vl.fieldNameIndex('time_field') assert isinstance(f.attributes()[time_idx], QTime) self.assertEqual(f.attributes()[time_idx], QTime(13, 41, 52)) - datetime_idx = vl.fieldNameIndex('datetime_field') + datetime_idx=vl.fieldNameIndex('datetime_field') assert isinstance(f.attributes()[datetime_idx], QDateTime) self.assertEqual(f.attributes()[datetime_idx], QDateTime(QDate(2004, 3, 4), QTime(13, 41, 52))) diff --git a/tests/src/python/test_provider_postgres.py b/tests/src/python/test_provider_postgres.py index 91479a92705..7340a100308 100644 --- a/tests/src/python/test_provider_postgres.py +++ b/tests/src/python/test_provider_postgres.py @@ -14,23 +14,21 @@ __revision__ = '$Format:%H$' import qgis import os -import sys from qgis.core import NULL from qgis.core import QgsVectorLayer, QgsFeatureRequest, QgsFeature, QgsProviderRegistry from PyQt4.QtCore import QSettings, QDate, QTime, QDateTime, QVariant -from utilities import (unitTestDataPath, - getQgisTestApp, - unittest, - TestCase - ) +from qgis.testing import (start_app, + unittest + ) +from utilities import unitTestDataPath from providertestbase import ProviderTestCase -QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() +start_app() TEST_DATA_DIR = unitTestDataPath() -class TestPyQgsPostgresProvider(TestCase, ProviderTestCase): +class TestPyQgsPostgresProvider(unittest.TestCase, ProviderTestCase): @classmethod def setUpClass(cls): diff --git a/tests/src/python/test_provider_shapefile.py b/tests/src/python/test_provider_shapefile.py index 721c8a9b1f0..d198941e235 100644 --- a/tests/src/python/test_provider_shapefile.py +++ b/tests/src/python/test_provider_shapefile.py @@ -12,6 +12,7 @@ __copyright__ = 'Copyright 2015, The QGIS Project' # This will get replaced with a git SHA1 when you do a git archive __revision__ = '$Format:%H$' +import qgis import os import tempfile import shutil @@ -19,18 +20,17 @@ import glob from qgis.core import QgsVectorLayer, QgsFeatureRequest, QgsFeature, QgsProviderRegistry from PyQt4.QtCore import QSettings -from utilities import (unitTestDataPath, - getQgisTestApp, - unittest, - TestCase - ) +from qgis.testing import (start_app, + unittest + ) +from utilities import unitTestDataPath from providertestbase import ProviderTestCase -QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() +start_app() TEST_DATA_DIR = unitTestDataPath() -class TestPyQgsShapefileProvider(TestCase, ProviderTestCase): +class TestPyQgsShapefileProvider(unittest.TestCase, ProviderTestCase): @classmethod def setUpClass(cls): diff --git a/tests/src/python/test_provider_spatialite.py b/tests/src/python/test_provider_spatialite.py index 60ffdbf1210..3d91897b64c 100644 --- a/tests/src/python/test_provider_spatialite.py +++ b/tests/src/python/test_provider_spatialite.py @@ -15,15 +15,13 @@ __revision__ = '$Format:%H$' import qgis import os import tempfile -import sys from qgis.core import QgsVectorLayer, QgsPoint, QgsFeature -from utilities import (unitTestDataPath, - getQgisTestApp, - TestCase, - unittest - ) +from qgis.testing import (start_app, + unittest + ) +from utilities import unitTestDataPath from providertestbase import ProviderTestCase from PyQt4.QtCore import QSettings @@ -34,7 +32,7 @@ except ImportError: raise ImportError # Convenience instances in case you may need them -QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() +start_app() TEST_DATA_DIR = unitTestDataPath() @@ -42,7 +40,7 @@ def die(error_message): raise Exception(error_message) -class TestQgsSpatialiteProvider(TestCase, ProviderTestCase): +class TestQgsSpatialiteProvider(unittest.TestCase, ProviderTestCase): @classmethod def setUpClass(cls): diff --git a/tests/src/python/test_provider_tabfile.py b/tests/src/python/test_provider_tabfile.py index 9d221b32763..a6ff204e310 100644 --- a/tests/src/python/test_provider_tabfile.py +++ b/tests/src/python/test_provider_tabfile.py @@ -19,19 +19,19 @@ import glob from qgis.core import QgsVectorLayer, QgsFeatureRequest, QgsFeature, QgsProviderRegistry from PyQt4.QtCore import QSettings, QDate, QTime, QDateTime, QVariant -from utilities import (unitTestDataPath, - getQgisTestApp, - unittest, - TestCase - ) +from qgis.testing import ( + start_app, + unittest +) +from utilities import unitTestDataPath -QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() +start_app() TEST_DATA_DIR = unitTestDataPath() # Note - doesn't implement ProviderTestCase as OGR provider is tested by the shapefile provider test -class TestPyQgsTabfileProvider(TestCase): +class TestPyQgsTabfileProvider(unittest.TestCase): def testDateTimeFormats(self): # check that date and time formats are correctly interpreted diff --git a/tests/src/python/test_provider_virtual.py b/tests/src/python/test_provider_virtual.py index 3ccc5ba738f..381c07df7cf 100644 --- a/tests/src/python/test_provider_virtual.py +++ b/tests/src/python/test_provider_virtual.py @@ -14,8 +14,6 @@ __revision__ = '$Format:%H$' import qgis import os -import tempfile -import sys from qgis.core import (QGis, QgsVectorLayer, @@ -33,11 +31,11 @@ from qgis.core import (QGis, QgsProject ) -from utilities import (unitTestDataPath, - getQgisTestApp, - TestCase, - unittest - ) +from qgis.testing import (start_app, + unittest + ) +from utilities import unitTestDataPath + from providertestbase import ProviderTestCase from PyQt4.QtCore import * @@ -50,11 +48,11 @@ except ImportError: import tempfile # Convenience instances in case you may need them -QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() +start_app() TEST_DATA_DIR = unitTestDataPath() -class TestQgsVirtualLayerProvider(TestCase, ProviderTestCase): +class TestQgsVirtualLayerProvider(unittest.TestCase, ProviderTestCase): @classmethod def setUpClass(cls): diff --git a/tests/src/python/test_qgis_local_server.py b/tests/src/python/test_qgis_local_server.py index 1f2c2529a59..49ec7d87c14 100644 --- a/tests/src/python/test_qgis_local_server.py +++ b/tests/src/python/test_qgis_local_server.py @@ -36,21 +36,21 @@ from qgis.core import ( from qgis_local_server import getLocalServer -from utilities import ( - TestCase, - getQgisTestApp, - unittest, - openInBrowserTab +from qgis.testing import ( + start_app, + unittest ) -QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() +from utilities import openInBrowserTab + +start_app() MAPSERV = getLocalServer() QGIS_TEST_REPORT = 'QGIS_TEST_REPORT' in os.environ TESTREPORTS = {} -class TestQgisLocalServer(TestCase): +class TestQgisLocalServer(unittest.TestCase): @classmethod def setUpClass(cls): diff --git a/tests/src/python/test_qgsanalysis.py b/tests/src/python/test_qgsanalysis.py index 50be4ae1853..5c53e4aa3e6 100644 --- a/tests/src/python/test_qgsanalysis.py +++ b/tests/src/python/test_qgsanalysis.py @@ -14,28 +14,19 @@ test_analysis.py * * ***************************************************************************/ ''' -import unittest import sys import qgis -from utilities import unitTestDataPath, getQgisTestApp +from qgis.testing import (start_app, + unittest + ) +from utilities import unitTestDataPath -# support python < 2.7 via unittest2 -# needed for expected failure decorator -if sys.version_info[0:2] < (2, 7): - try: - from unittest2 import TestCase, expectedFailure - except ImportError: - print "You should install unittest2 to run the salt tests" - sys.exit(0) -else: - from unittest import TestCase, expectedFailure - -QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() +start_app() TEST_DATA_DIR = unitTestDataPath() -class TestQgsZonalStatistics(TestCase): +class TestQgsZonalStatistics(unittest.TestCase): def setUp(self): """Run before each test.""" @@ -45,7 +36,7 @@ class TestQgsZonalStatistics(TestCase): """Run after each test.""" pass - #@expectedFailure + #@unittest.expectedFailure def testSubstitutionMap(self): """Test that we can import zonal statistics was failing as of d5f6543 """ diff --git a/tests/src/python/test_qgsapplication.py b/tests/src/python/test_qgsapplication.py index d48cb0d04bb..4bd9ade64e9 100644 --- a/tests/src/python/test_qgsapplication.py +++ b/tests/src/python/test_qgsapplication.py @@ -12,10 +12,10 @@ __copyright__ = 'Copyright 2012, The QGIS Project' __revision__ = '$Format:%H$' import qgis -from utilities import getQgisTestApp, unittest, expectedFailure +from qgis.testing import start_app, unittest -QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() +QGISAPP = start_app() class TestPyQgsApplication(unittest.TestCase): diff --git a/tests/src/python/test_qgsappstartup.py b/tests/src/python/test_qgsappstartup.py index 7edc98dfdff..8c63c5c6592 100644 --- a/tests/src/python/test_qgsappstartup.py +++ b/tests/src/python/test_qgsappstartup.py @@ -26,7 +26,8 @@ import subprocess import tempfile import errno -from utilities import unittest, unitTestDataPath +from qgis.testing import unittest +from utilities import unitTestDataPath print 'CTEST_FULL_OUTPUT' diff --git a/tests/src/python/test_qgsatlascomposition.py b/tests/src/python/test_qgsatlascomposition.py index c9f73559b55..23b8d45bba6 100644 --- a/tests/src/python/test_qgsatlascomposition.py +++ b/tests/src/python/test_qgsatlascomposition.py @@ -15,19 +15,19 @@ test_qgsatlascomposition.py ***************************************************************************/ ''' import qgis -import unittest import os import glob import shutil import tempfile -from utilities import getQgisTestApp, unitTestDataPath +from qgis.testing import start_app, unittest +from utilities import unitTestDataPath from PyQt4.QtCore import QFileInfo, QRectF, qWarning from qgis.core import QGis, QgsVectorLayer, QgsMapLayerRegistry, QgsMapSettings, QgsCoordinateReferenceSystem, \ QgsComposition, QgsFillSymbolV2, QgsSingleSymbolRendererV2, QgsComposerLabel, QgsComposerMap, QgsFontUtils, \ QgsRectangle, QgsComposerLegend, QgsFeature, QgsGeometry, QgsPoint, QgsRendererCategoryV2, QgsCategorizedSymbolRendererV2, QgsMarkerSymbolV2 from qgscompositionchecker import QgsCompositionChecker -QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() +start_app() class TestQgsAtlasComposition(unittest.TestCase): diff --git a/tests/src/python/test_qgsattributetablemodel.py b/tests/src/python/test_qgsattributetablemodel.py index 65e0d4ee620..73ea5d2d38c 100644 --- a/tests/src/python/test_qgsattributetablemodel.py +++ b/tests/src/python/test_qgsattributetablemodel.py @@ -15,15 +15,14 @@ __revision__ = '$Format:%H$' from qgis.gui import QgsAttributeTableModel, QgsEditorWidgetRegistry from qgis.core import QgsFeature, QgsGeometry, QgsPoint, QgsVectorLayer, QgsVectorLayerCache, NULL -from utilities import (unitTestDataPath, - getQgisTestApp, - TestCase, - unittest - ) -QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() +from qgis.testing import (start_app, + unittest + ) +from utilities import unitTestDataPath +start_app() -class TestQgsAttributeTableModel(TestCase): +class TestQgsAttributeTableModel(unittest.TestCase): @classmethod def setUpClass(cls): diff --git a/tests/src/python/test_qgsauthsystem.py b/tests/src/python/test_qgsauthsystem.py index fa7624662a7..570a40e21fc 100644 --- a/tests/src/python/test_qgsauthsystem.py +++ b/tests/src/python/test_qgsauthsystem.py @@ -26,9 +26,8 @@ from PyQt4.QtGui import * from PyQt4.QtNetwork import * from PyQt4.QtTest import * -from utilities import ( - TestCase, - getQgisTestApp, +from qgis.testing import ( + start_app, unittest, expectedFailure, unitTestDataPath, @@ -36,13 +35,13 @@ from utilities import ( AUTHDBDIR = tempfile.mkdtemp() os.environ['QGIS_AUTH_DB_DIR_PATH'] = AUTHDBDIR -QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() +start_app() TESTDATA = os.path.join(unitTestDataPath(), 'auth_system') PKIDATA = os.path.join(TESTDATA, 'certs_keys') -class TestQgsAuthManager(TestCase): +class TestQgsAuthManager(unittest.TestCase): @classmethod def setUpClass(cls): diff --git a/tests/src/python/test_qgsblendmodes.py b/tests/src/python/test_qgsblendmodes.py index 1376fcae3e9..57343a4875b 100644 --- a/tests/src/python/test_qgsblendmodes.py +++ b/tests/src/python/test_qgsblendmodes.py @@ -23,7 +23,6 @@ __copyright__ = '(C) 2013, Nyall Dawson, Massimo Endrighi' # This will get replaced with a git SHA1 when you do a git archive __revision__ = '$Format:%H$' -import qgis import os from PyQt4.QtCore import QSize @@ -38,22 +37,26 @@ from qgis.core import (QgsVectorLayer, QgsRectangle ) -from utilities import (unitTestDataPath, - getQgisTestApp, - TestCase, - unittest - ) +from qgis.testing import (start_app, + unittest + ) + +from qgis.testing.mocked import get_iface + +from utilities import unitTestDataPath # Convenience instances in case you may need them -QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() +start_app() TEST_DATA_DIR = unitTestDataPath() -class TestQgsBlendModes(TestCase): +class TestQgsBlendModes(unittest.TestCase): def __init__(self, methodName): """Run once on class initialisation.""" unittest.TestCase.__init__(self, methodName) + self.iface = get_iface() + # initialize class MapRegistry, Canvas, MapRenderer, Map and PAL self.mMapRegistry = QgsMapLayerRegistry.instance() @@ -89,7 +92,7 @@ class TestQgsBlendModes(TestCase): self.mMapRegistry.addMapLayer(self.mRasterLayer2) # to match blend modes test comparisons background - self.mCanvas = CANVAS + self.mCanvas = self.iface.mapCanvas() self.mCanvas.setCanvasColor(QColor(152, 219, 249)) self.mMap = self.mCanvas.map() self.mMap.resize(QSize(400, 400)) diff --git a/tests/src/python/test_qgscategorizedsymbolrendererv2.py b/tests/src/python/test_qgscategorizedsymbolrendererv2.py index 19d714f12ca..0a88ec0d116 100644 --- a/tests/src/python/test_qgscategorizedsymbolrendererv2.py +++ b/tests/src/python/test_qgscategorizedsymbolrendererv2.py @@ -14,10 +14,9 @@ __revision__ = '$Format:%H$' import qgis -from utilities import (unittest, - TestCase, - getQgisTestApp, - ) +from qgis.testing import (unittest, + start_app, + ) from qgis.core import (QgsCategorizedSymbolRendererV2, QgsRendererCategoryV2, QgsMarkerSymbolV2, @@ -34,7 +33,7 @@ from PyQt4.QtCore import Qt, QVariant from PyQt4.QtXml import QDomDocument from PyQt4.QtGui import QColor -QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() +start_app() def createMarkerSymbol(): @@ -46,7 +45,7 @@ def createMarkerSymbol(): return symbol -class TestQgsCategorizedSymbolRendererV2(TestCase): +class TestQgsCategorizedSymbolRendererV2(unittest.TestCase): def testFilter(self): """Test filter creation""" diff --git a/tests/src/python/test_qgscolorscheme.py b/tests/src/python/test_qgscolorscheme.py index 68404f1eade..5aace7f6fc0 100644 --- a/tests/src/python/test_qgscolorscheme.py +++ b/tests/src/python/test_qgscolorscheme.py @@ -13,7 +13,7 @@ __copyright__ = 'Copyright 2014, The QGIS Project' __revision__ = '$Format:%H$' import qgis -from utilities import unittest, TestCase +from qgis.testing import unittest from qgis.core import QgsColorScheme from PyQt4.QtGui import QColor @@ -40,7 +40,7 @@ class DummyColorScheme(QgsColorScheme): return DummyColorScheme() -class TestQgsColorScheme(TestCase): +class TestQgsColorScheme(unittest.TestCase): def testCreateScheme(self): """Test creating a new color scheme""" diff --git a/tests/src/python/test_qgscolorschemeregistry.py b/tests/src/python/test_qgscolorschemeregistry.py index 4b0e648b543..23f33cdbbe3 100644 --- a/tests/src/python/test_qgscolorschemeregistry.py +++ b/tests/src/python/test_qgscolorschemeregistry.py @@ -13,11 +13,11 @@ __copyright__ = 'Copyright 2014, The QGIS Project' __revision__ = '$Format:%H$' import qgis -from utilities import unittest, TestCase +from qgis.testing import unittest from qgis.core import QgsColorSchemeRegistry, QgsRecentColorScheme -class TestQgsColorSchemeRegistry(TestCase): +class TestQgsColorSchemeRegistry(unittest.TestCase): def testCreateInstance(self): """Test creating global color scheme registry instance""" diff --git a/tests/src/python/test_qgscomposereffects.py b/tests/src/python/test_qgscomposereffects.py index 19361da26b5..8ef2ef4f8d9 100644 --- a/tests/src/python/test_qgscomposereffects.py +++ b/tests/src/python/test_qgscomposereffects.py @@ -19,18 +19,17 @@ from qgis.core import (QgsComposerShape, QgsComposition, QgsMapRenderer ) -from utilities import (unitTestDataPath, - getQgisTestApp, - TestCase, - unittest - ) +from qgis.testing import (start_app, + unittest + ) +from utilities import unitTestDataPath from qgscompositionchecker import QgsCompositionChecker -QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() +start_app() TEST_DATA_DIR = unitTestDataPath() -class TestQgsComposerEffects(TestCase): +class TestQgsComposerEffects(unittest.TestCase): def __init__(self, methodName): """Run once on class initialisation.""" diff --git a/tests/src/python/test_qgscomposerhtml.py b/tests/src/python/test_qgscomposerhtml.py index 4dee2b52d43..af1daef4a65 100644 --- a/tests/src/python/test_qgscomposerhtml.py +++ b/tests/src/python/test_qgscomposerhtml.py @@ -13,7 +13,6 @@ __copyright__ = 'Copyright 2012, The QGIS Project' __revision__ = '$Format:%H$' import qgis -import unittest import os from PyQt4.QtCore import QUrl, qDebug @@ -22,22 +21,30 @@ from qgis.core import (QgsComposition, QgsComposerHtml, QgsComposerFrame, QgsComposerMultiFrame, - QgsMapSettings) + QgsMapSettings + ) from qgscompositionchecker import QgsCompositionChecker -from utilities import (unitTestDataPath, - getQgisTestApp, - TestCase - ) -QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() +from qgis.testing import ( + start_app, + unittest +) + +from qgis.testing.mocked import get_iface + +from utilities import unitTestDataPath + +start_app() + TEST_DATA_DIR = unitTestDataPath() -class TestQgsComposerHtml(TestCase): +class TestQgsComposerHtml(unittest.TestCase): def setUp(self): """Run before each test.""" + self.iface = get_iface() self.mapSettings = QgsMapSettings() self.mComposition = QgsComposition(self.mapSettings) self.mComposition.setPaperSize(297, 210) # A4 landscape @@ -136,7 +143,7 @@ class TestQgsComposerHtml(TestCase): def testComposerHtmlAccessor(self): """Test that we can retrieve the ComposerHtml instance given an item. """ - myComposition = QgsComposition(CANVAS.mapRenderer()) + myComposition = QgsComposition(self.iface.mapCanvas().mapRenderer()) mySubstitutionMap = {'replace-me': 'Foo bar'} myFile = os.path.join(TEST_DATA_DIR, 'template.qpt') myTemplateFile = file(myFile, 'rt') diff --git a/tests/src/python/test_qgscomposerlabel.py b/tests/src/python/test_qgscomposerlabel.py index 79c6e8052b0..b10cc014a5f 100644 --- a/tests/src/python/test_qgscomposerlabel.py +++ b/tests/src/python/test_qgscomposerlabel.py @@ -15,12 +15,12 @@ test_qgscomposerlabel.py ***************************************************************************/ ''' import qgis -import unittest -from utilities import getQgisTestApp, unitTestDataPath +from qgis.testing import start_app, unittest from PyQt4.QtCore import QFileInfo, QDate, QDateTime from qgis.core import QgsVectorLayer, QgsMapLayerRegistry, QgsMapRenderer, QgsComposition, QgsComposerLabel, QgsFeatureRequest, QgsFeature, QgsExpression +from utilities import unitTestDataPath -QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() +start_app() class TestQgsComposerLabel(unittest.TestCase): diff --git a/tests/src/python/test_qgscomposermap.py b/tests/src/python/test_qgscomposermap.py index f6664f5d248..2435be9c125 100644 --- a/tests/src/python/test_qgscomposermap.py +++ b/tests/src/python/test_qgscomposermap.py @@ -28,19 +28,17 @@ from qgis.core import (QgsComposerMap, QgsMultiBandColorRenderer, ) -from utilities import (unitTestDataPath, - getQgisTestApp, - TestCase, - unittest, - expectedFailure - ) +from qgis.testing import (start_app, + unittest + ) +from utilities import unitTestDataPath from qgscompositionchecker import QgsCompositionChecker -QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() +start_app() TEST_DATA_DIR = unitTestDataPath() -class TestQgsComposerMap(TestCase): +class TestQgsComposerMap(unittest.TestCase): def __init__(self, methodName): """Run once on class initialisation.""" @@ -137,7 +135,7 @@ class TestQgsComposerMap(TestCase): assert myTestResult, myMessage # Fails because addItemsFromXML has been commented out in sip - @expectedFailure + @unittest.expectedFailure def testuniqueId(self): doc = QDomDocument() documentElement = doc.createElement('ComposerItemClipboard') diff --git a/tests/src/python/test_qgscomposermapgrid.py b/tests/src/python/test_qgscomposermapgrid.py index f5917208f2c..bb4aa98dd3d 100644 --- a/tests/src/python/test_qgscomposermapgrid.py +++ b/tests/src/python/test_qgscomposermapgrid.py @@ -24,18 +24,17 @@ from qgis.core import (QgsComposerMap, QgsCoordinateReferenceSystem, QgsFontUtils ) -from utilities import (unitTestDataPath, - getQgisTestApp, - TestCase, - unittest - ) +from qgis.testing import (start_app, + unittest + ) +from utilities import unitTestDataPath from qgscompositionchecker import QgsCompositionChecker -QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() +start_app() TEST_DATA_DIR = unitTestDataPath() -class TestQgsComposerMap(TestCase): +class TestQgsComposerMap(unittest.TestCase): def __init__(self, methodName): """Run once on class initialisation.""" diff --git a/tests/src/python/test_qgscomposerpicture.py b/tests/src/python/test_qgscomposerpicture.py index 7095ef4e286..ec82a95a454 100644 --- a/tests/src/python/test_qgscomposerpicture.py +++ b/tests/src/python/test_qgscomposerpicture.py @@ -25,18 +25,17 @@ from qgis.core import (QgsComposerPicture, QgsComposition, QgsMapSettings ) -from utilities import (unitTestDataPath, - getQgisTestApp, - TestCase, - unittest - ) +from qgis.testing import (start_app, + unittest + ) +from utilities import unitTestDataPath from qgscompositionchecker import QgsCompositionChecker -QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() +start_app() TEST_DATA_DIR = unitTestDataPath() -class TestQgsComposerPicture(TestCase): +class TestQgsComposerPicture(unittest.TestCase): @classmethod def setUpClass(cls): diff --git a/tests/src/python/test_qgscomposershapes.py b/tests/src/python/test_qgscomposershapes.py index 6885085b59e..6b9ace58c71 100644 --- a/tests/src/python/test_qgscomposershapes.py +++ b/tests/src/python/test_qgscomposershapes.py @@ -20,18 +20,17 @@ from qgis.core import (QgsComposerShape, QgsComposition, QgsMapSettings ) -from utilities import (unitTestDataPath, - getQgisTestApp, - TestCase, - unittest - ) +from qgis.testing import (start_app, + unittest + ) +from utilities import unitTestDataPath from qgscompositionchecker import QgsCompositionChecker -QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() +start_app() TEST_DATA_DIR = unitTestDataPath() -class TestQgsComposerShapes(TestCase): +class TestQgsComposerShapes(unittest.TestCase): def __init__(self, methodName): """Run once on class initialisation.""" diff --git a/tests/src/python/test_qgscomposition.py b/tests/src/python/test_qgscomposition.py index c93ffd559fe..518a6ce500a 100644 --- a/tests/src/python/test_qgscomposition.py +++ b/tests/src/python/test_qgscomposition.py @@ -12,7 +12,6 @@ __copyright__ = 'Copyright 2012, The QGIS Project' # This will get replaced with a git SHA1 when you do a git archive __revision__ = '$Format:%H$' -import qgis import os from PyQt4.QtCore import QFileInfo, QDir @@ -26,22 +25,23 @@ from qgis.core import (QgsComposition, QgsMapRenderer ) -from utilities import (unitTestDataPath, - getQgisTestApp, - TestCase, - unittest - # expectedFailure - ) +from qgis.testing import (start_app, + unittest + ) -QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() +from qgis.testing.mocked import get_iface + +from utilities import unitTestDataPath + +start_app() TEST_DATA_DIR = unitTestDataPath() -class TestQgsComposition(TestCase): +class TestQgsComposition(unittest.TestCase): def setUp(self): """Run before each test.""" - pass + self.iface = get_iface() def tearDown(self): """Run after each test.""" @@ -59,7 +59,7 @@ class TestQgsComposition(TestCase): myText = 'Latitude: %s, Longitude: %s' % (myLatitude, myLongitude) # Load the composition with the substitutions - myComposition = QgsComposition(CANVAS.mapRenderer()) + myComposition = QgsComposition(self.iface.mapCanvas().mapRenderer()) mySubstitutionMap = {'replace-me': myText} myFile = os.path.join(TEST_DATA_DIR, 'template-for-substitution.qpt') myTemplateFile = file(myFile, 'rt') @@ -76,7 +76,7 @@ class TestQgsComposition(TestCase): def testNoSubstitutionMap(self): """Test that we can get a map if we use no text substitutions.""" - myComposition = QgsComposition(CANVAS.mapRenderer()) + myComposition = QgsComposition(self.iface.mapCanvas().mapRenderer()) myFile = os.path.join(TEST_DATA_DIR, 'template-for-substitution.qpt') myTemplateFile = file(myFile, 'rt') myTemplateContent = myTemplateFile.read() diff --git a/tests/src/python/test_qgsconditionalstyle.py b/tests/src/python/test_qgsconditionalstyle.py index 8fa87412c8f..176883624d5 100644 --- a/tests/src/python/test_qgsconditionalstyle.py +++ b/tests/src/python/test_qgsconditionalstyle.py @@ -13,19 +13,25 @@ __copyright__ = 'Copyright 2015, The QGIS Project' # This will get replaced with a git SHA1 when you do a git archive __revision__ = '$Format:%H$' -from qgis.core import QgsConditionalStyle, QgsFeature, QgsFields, QgsField, QgsExpressionContextUtils +from qgis.core import (QgsConditionalStyle, + QgsFeature, + QgsFields, + QgsField, + QgsExpressionContextUtils + ) +from qgis.testing import (start_app, + unittest, + ) from utilities import (unitTestDataPath, - getQgisTestApp, - unittest, - TestCase, - compareWkt) + compareWkt + ) from PyQt4.QtCore import QVariant # -QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() +start_app() TEST_DATA_DIR = unitTestDataPath() -class TestPyQgsConditionalStyle(TestCase): +class TestPyQgsConditionalStyle(unittest.TestCase): def new_context(self): feature = QgsFeature() diff --git a/tests/src/python/test_qgscoordinatetransform.py b/tests/src/python/test_qgscoordinatetransform.py index dfa2008746c..2b1d0bc3cc8 100644 --- a/tests/src/python/test_qgscoordinatetransform.py +++ b/tests/src/python/test_qgscoordinatetransform.py @@ -18,16 +18,12 @@ from qgis.core import (QgsRectangle, QgsCoordinateReferenceSystem, QgsCoordinateTransform ) -from utilities import (getQgisTestApp, - TestCase, - unittest - ) -# Convenience instances in case you may need them -# not used in this test -QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() +from qgis.testing import start_app, unittest + +start_app() -class TestQgsCoordinateTransform(TestCase): +class TestQgsCoordinateTransform(unittest.TestCase): def testTransformBoundingBox(self): """Test that we can transform a rectangular bbox from utm56s to LonLat""" diff --git a/tests/src/python/test_qgsdelimitedtextprovider.py b/tests/src/python/test_qgsdelimitedtextprovider.py index 07ddd0fee7e..aede7c72ba9 100644 --- a/tests/src/python/test_qgsdelimitedtextprovider.py +++ b/tests/src/python/test_qgsdelimitedtextprovider.py @@ -34,34 +34,38 @@ import test_qgsdelimitedtextprovider_wanted as want rebuildTests = 'REBUILD_DELIMITED_TEXT_TESTS' in os.environ -from PyQt4.QtCore import (QCoreApplication, - QUrl, - QObject - ) +import qgis -from qgis.core import (QgsProviderRegistry, - QgsVectorLayer, - QgsFeatureRequest, - QgsRectangle, - QgsMessageLog, - QGis - ) +from PyQt4.QtCore import ( + QCoreApplication, + QUrl, + QObject +) -from utilities import (getQgisTestApp, - TestCase, - unitTestDataPath, - unittest, - compareWkt - ) +from qgis.core import ( + QgsProviderRegistry, + QgsVectorLayer, + QgsFeatureRequest, + QgsRectangle, + QgsMessageLog, + QGis +) + +from qgis.testing import ( + start_app, + unittest +) + +from utilities import ( + unitTestDataPath, + compareWkt +) from providertestbase import ProviderTestCase -QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() +start_app() TEST_DATA_DIR = unitTestDataPath() -import sip -sipversion = str(sip.getapi('QVariant')) -sipwanted = '2' geomkey = "#geometry" fidkey = "#fid" @@ -245,9 +249,6 @@ def recordDifference(record1, record2): def runTest(file, requests, **params): - # No point doing test if haven't got the right SIP vesion - if sipversion != sipwanted: - return testname = inspect.stack()[1][3] verbose = not rebuildTests if verbose: @@ -322,7 +323,7 @@ def runTest(file, requests, **params): assert len(failures) == 0, "\n".join(failures) -class TestQgsDelimitedTextProviderXY(TestCase, ProviderTestCase): +class TestQgsDelimitedTextProviderXY(unittest.TestCase, ProviderTestCase): @classmethod def setUpClass(cls): @@ -349,7 +350,7 @@ class TestQgsDelimitedTextProviderXY(TestCase, ProviderTestCase): """Run after all tests""" -class TestQgsDelimitedTextProviderWKT(TestCase, ProviderTestCase): +class TestQgsDelimitedTextProviderWKT(unittest.TestCase, ProviderTestCase): @classmethod def setUpClass(cls): @@ -389,7 +390,7 @@ class TestQgsDelimitedTextProviderWKT(TestCase, ProviderTestCase): """Run after all tests""" -class TestQgsDelimitedTextProviderOther(TestCase): +class TestQgsDelimitedTextProviderOther(unittest.TestCase): @classmethod def setUpClass(cls): @@ -401,7 +402,6 @@ class TestQgsDelimitedTextProviderOther(TestCase): registry = QgsProviderRegistry.instance() metadata = registry.providerMetadata('delimitedtext') assert metadata is not None, "Delimited text provider is not installed" - assert sipversion == sipwanted, "SIP version " + sipversion + " - require version " + sipwanted + " for delimited text tests" def test_002_load_csv_file(self): # CSV file parsing diff --git a/tests/src/python/test_qgsdistancearea.py b/tests/src/python/test_qgsdistancearea.py index 85e128cc179..4a1af2ca05c 100644 --- a/tests/src/python/test_qgsdistancearea.py +++ b/tests/src/python/test_qgsdistancearea.py @@ -19,17 +19,16 @@ from qgis.core import (QgsGeometry, QgsDistanceArea ) -from utilities import (getQgisTestApp, - TestCase, - unittest) +from qgis.testing import (start_app, + unittest) # Convenience instances in case you may need them # not used in this test -QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() +start_app() -class TestQgsDistanceArea(TestCase): +class TestQgsDistanceArea(unittest.TestCase): def testMeasureLine(self): # +-+ diff --git a/tests/src/python/test_qgsdoccoverage.py b/tests/src/python/test_qgsdoccoverage.py index f511673d028..455e4ac3c83 100644 --- a/tests/src/python/test_qgsdoccoverage.py +++ b/tests/src/python/test_qgsdoccoverage.py @@ -13,12 +13,9 @@ __copyright__ = 'Copyright 2015, The QGIS Project' __revision__ = '$Format:%H$' import os -from utilities import (TestCase, - unittest, - printImportant, - DoxygenParser) +from qgis.testing import unittest -from PyQt4.QtCore import qDebug +from utilities import printImportant, DoxygenParser # DOCUMENTATION THRESHOLD # @@ -30,7 +27,7 @@ from PyQt4.QtCore import qDebug ACCEPTABLE_MISSING_DOCS = 3751 -class TestQgsDocCoverage(TestCase): +class TestQgsDocCoverage(unittest.TestCase): def testCoverage(self): print 'CTEST_FULL_OUTPUT' diff --git a/tests/src/python/test_qgseditwidgets.py b/tests/src/python/test_qgseditwidgets.py index f83159c770b..d1fb3f1b061 100644 --- a/tests/src/python/test_qgseditwidgets.py +++ b/tests/src/python/test_qgseditwidgets.py @@ -21,15 +21,16 @@ from qgis.gui import QgsEditorWidgetRegistry from PyQt4 import QtCore -from utilities import (unitTestDataPath, - getQgisTestApp, - TestCase, - unittest - ) -QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() +from qgis.testing import (start_app, + unittest + ) + +from utilities import unitTestDataPath + +start_app() -class TestQgsTextEditWidget(TestCase): +class TestQgsTextEditWidget(unittest.TestCase): @classmethod def setUpClass(cls): diff --git a/tests/src/python/test_qgsexpression.py b/tests/src/python/test_qgsexpression.py index d803653d23d..e52463ac20d 100644 --- a/tests/src/python/test_qgsexpression.py +++ b/tests/src/python/test_qgsexpression.py @@ -13,12 +13,12 @@ __copyright__ = 'Copyright 2012, The QGIS Project' __revision__ = '$Format:%H$' import qgis -from utilities import unittest, TestCase +from qgis.testing import unittest from qgis.utils import qgsfunction from qgis.core import QgsExpression -class TestQgsExpressionCustomFunctions(TestCase): +class TestQgsExpressionCustomFunctions(unittest.TestCase): @qgsfunction(1, 'testing', register=False) def testfun(values, feature, parent): diff --git a/tests/src/python/test_qgsfeature.py b/tests/src/python/test_qgsfeature.py index ea9936d2cd0..06408d8c7da 100644 --- a/tests/src/python/test_qgsfeature.py +++ b/tests/src/python/test_qgsfeature.py @@ -16,17 +16,13 @@ import qgis import os from qgis.core import QgsFeature, QgsGeometry, QgsPoint, QgsVectorLayer, NULL -from utilities import (unitTestDataPath, - getQgisTestApp, - TestCase, - unittest - ) -from unittest import expectedFailure +from qgis.testing import start_app, unittest +from utilities import unitTestDataPath -QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() +start_app() -class TestQgsFeature(TestCase): +class TestQgsFeature(unittest.TestCase): def test_CreateFeature(self): feat = QgsFeature() diff --git a/tests/src/python/test_qgsfeatureiterator.py b/tests/src/python/test_qgsfeatureiterator.py index ac052325676..8e6e5683ab6 100644 --- a/tests/src/python/test_qgsfeatureiterator.py +++ b/tests/src/python/test_qgsfeatureiterator.py @@ -16,16 +16,17 @@ import qgis import os from qgis.core import QgsVectorLayer, QgsFeatureRequest, QgsFeature +from qgis.testing import (start_app, + unittest + ) from utilities import (unitTestDataPath, - getQgisTestApp, - TestCase, - unittest + compareWkt ) -QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() +start_app() TEST_DATA_DIR = unitTestDataPath() -class TestQgsFeatureIterator(TestCase): +class TestQgsFeatureIterator(unittest.TestCase): def __init__(self, methodName): """Run once on class initialisation.""" diff --git a/tests/src/python/test_qgsfield.py b/tests/src/python/test_qgsfield.py index f0d28a362ae..fc1bcf72d5e 100644 --- a/tests/src/python/test_qgsfield.py +++ b/tests/src/python/test_qgsfield.py @@ -16,17 +16,15 @@ import qgis import os from qgis.core import QgsField, QgsVectorLayer, NULL -from utilities import (unitTestDataPath, - getQgisTestApp, - TestCase, - unittest - ) -from unittest import expectedFailure +from qgis.testing import (start_app, + unittest + ) +from utilities import unitTestDataPath -QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() +start_app() -class TestQgsFields(TestCase): +class TestQgsFields(unittest.TestCase): def test_expections(self): ml = QgsVectorLayer("Point?crs=epsg:4236&field=id:integer&field=value:double", diff --git a/tests/src/python/test_qgsfontutils.py b/tests/src/python/test_qgsfontutils.py index 1f98b17e2d8..a51807061ed 100644 --- a/tests/src/python/test_qgsfontutils.py +++ b/tests/src/python/test_qgsfontutils.py @@ -14,21 +14,20 @@ __copyright__ = 'Copyright 2014, The QGIS Project' # This will get replaced with a git SHA1 when you do a git archive __revision__ = '$Format:%H$' -import qgis - from qgis.core import QgsFontUtils +from qgis.testing import ( + start_app, + unittest +) from utilities import ( - TestCase, - getQgisTestApp, - unittest, getTestFontFamily, loadTestFonts ) -QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() +start_app() -class TestQgsFontUtils(TestCase): +class TestQgsFontUtils(unittest.TestCase): @classmethod def setUpClass(cls): diff --git a/tests/src/python/test_qgsgeometry.py b/tests/src/python/test_qgsgeometry.py index 40e3f1412f0..78f54652641 100644 --- a/tests/src/python/test_qgsgeometry.py +++ b/tests/src/python/test_qgsgeometry.py @@ -12,35 +12,39 @@ __copyright__ = 'Copyright 2012, The QGIS Project' # This will get replaced with a git SHA1 when you do a git archive __revision__ = '$Format:%H$' -import qgis import os import csv -from qgis.core import (QgsGeometry, - QgsVectorLayer, - QgsFeature, - QgsPoint, - QgsCoordinateTransform, - QgsRectangle, - QgsWKBTypes, - QGis) +from qgis.core import ( + QgsGeometry, + QgsVectorLayer, + QgsFeature, + QgsPoint, + QgsCoordinateTransform, + QgsRectangle, + QgsWKBTypes, + QGis +) -from utilities import (getQgisTestApp, - TestCase, - unittest, - compareWkt, - doubleNear, - unitTestDataPath, - writeShape) +from qgis.testing import ( + start_app, + unittest, +) +from utilities import( + compareWkt, + doubleNear, + unitTestDataPath, + writeShape +) # Convenience instances in case you may need them not used in this test -QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() +start_app() TEST_DATA_DIR = unitTestDataPath() -class TestQgsGeometry(TestCase): +class TestQgsGeometry(unittest.TestCase): def testWktPointLoading(self): myWKT = 'Point (10 10)' diff --git a/tests/src/python/test_qgsgeometry_avoid_intersections.py b/tests/src/python/test_qgsgeometry_avoid_intersections.py index 315e634e785..fcf5532ea8d 100644 --- a/tests/src/python/test_qgsgeometry_avoid_intersections.py +++ b/tests/src/python/test_qgsgeometry_avoid_intersections.py @@ -16,7 +16,7 @@ import qgis from qgis.core import QgsVectorLayer, QgsFeature, QgsGeometry, QgsProject, QgsMapLayerRegistry -from utilities import getQgisTestApp, TestCase, unittest +from qgis.testing import start_app, unittest feat_wkt = [ @@ -39,10 +39,10 @@ Incorrect result from QGIS < 2.2 (four parts): MULTIPOLYGON(((11556863.91276544518768787 1008143.53638577624224126,11632785.85562258586287498 1005192.9078143477672711,11633693.74133687280118465 996794.96495720488019288,11623253.05562258698046207 996794.96495720488019288,11623026.08419401571154594 998156.79352863342501223,11613039.34133687242865562 1000880.45067149063106626,11612394.26464514434337616 996794.96495720488019288,11605776.25562258809804916 996794.96495720488019288,11605618.44716151989996433 993165.37035266763996333,11605393.122793298214674 993232.96766313433181494,11603539.33281490206718445 993789.104656653245911,11603620.0270511582493782 996794.96495720488019288,11597281.42645414359867573 997264.49092735419981182,11595335.56990830041468143 1000426.50781434774398804,11585348.82705115899443626 1001788.33638577628880739,11585348.82705115899443626 999405.1363857762189582,11585348.82705115899443626 992546.05981434776913375,11579916.07919318228960037 992980.67964298592414707,11580355.4556225873529911 997248.9078143477672711,11578957.31162258610129356 997448.64267149078659713,11577586.53731508925557137 997644.46757256181444973,11577745.28419401682913303 1000199.53638577624224126,11573858.94101548381149769 1000375.19031474948860705,11574000.25562258623540401 1001788.33638577628880739,11564467.4556225873529911 1004058.0506714906077832,11563869.05927479639649391 1000826.71039342461153865,11557658.3127654455602169 1001107.42210006201639771,11556863.91276544518768787 1008143.53638577624224126)),((11578337.0769300926476717 993106.99982403311878443,11577309.72997828386723995 993189.18758017767686397,11577309.72997828386723995 993189.18758017779327929,11578337.0769300926476717 993106.99982403311878443)),((11604513.11028097197413445 967742.62210006208624691,11604300.94133687205612659 962862.73638577631209046,11602712.14133687317371368 962976.22210006194654852,11602840.09838385321199894 967742.62210006208624691,11603208.23603074997663498 967742.62210006208624691,11604513.11028097197413445 967742.62210006208624691)),((11584503.3406173400580883 970315.28251079504843801,11583079.11276544444262981 965472.9078143477672711,11584280.59107660688459873 961387.88155639520846307,11575362.08419401571154594 961841.36495720490347594,11575644.1196969747543335 966380.79352863342501223,11577177.85562258772552013 966380.79352863342501223,11577674.00869971700012684 971200.56627789419144392,11584503.3406173400580883 970315.28251079504843801))) """ -QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() +start_app() -class TestQgsGeometryAvoidIntersections(TestCase): +class TestQgsGeometryAvoidIntersections(unittest.TestCase): def testNoSliverPolygons(self): diff --git a/tests/src/python/test_qgsgeometrygeneratorsymbollayerv2.py b/tests/src/python/test_qgsgeometrygeneratorsymbollayerv2.py index c1907d9e69f..0ef8302e1a6 100644 --- a/tests/src/python/test_qgsgeometrygeneratorsymbollayerv2.py +++ b/tests/src/python/test_qgsgeometrygeneratorsymbollayerv2.py @@ -23,37 +23,43 @@ __copyright__ = '(C) 2015, Matthias Kuhn' # This will get replaced with a git SHA1 when you do a git archive __revision__ = '$Format:%H$' -import qgis import os from PyQt.QtCore import QSize -from qgis.core import (QgsVectorLayer, - QgsSingleSymbolRendererV2, - QgsFillSymbolV2, - QgsLineSymbolV2, - QgsMarkerSymbolV2, - QgsMapLayerRegistry, - QgsRectangle, - QgsGeometryGeneratorSymbolLayerV2, - QgsSymbolV2, - QgsMultiRenderChecker - ) -from utilities import (unitTestDataPath, - getQgisTestApp, - TestCase, - unittest - ) +from qgis.core import ( + QgsVectorLayer, + QgsSingleSymbolRendererV2, + QgsFillSymbolV2, + QgsLineSymbolV2, + QgsMarkerSymbolV2, + QgsMapLayerRegistry, + QgsRectangle, + QgsGeometryGeneratorSymbolLayerV2, + QgsSymbolV2, + QgsMultiRenderChecker +) + +from qgis.testing import ( + start_app, + unittest +) + +from qgis.testing.mocked import get_iface + +from utilities import unitTestDataPath # Convenience instances in case you may need them # not used in this test -QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() +start_app() TEST_DATA_DIR = unitTestDataPath() -class TestQgsGeometryGeneratorSymbolLayerV2(TestCase): +class TestQgsGeometryGeneratorSymbolLayerV2(unittest.TestCase): def setUp(self): + self.iface = get_iface() + polys_shp = os.path.join(TEST_DATA_DIR, 'polys.shp') points_shp = os.path.join(TEST_DATA_DIR, 'points.shp') lines_shp = os.path.join(TEST_DATA_DIR, 'lines.shp') @@ -73,7 +79,7 @@ class TestQgsGeometryGeneratorSymbolLayerV2(TestCase): self.lines_layer.setRendererV2(QgsSingleSymbolRendererV2(sym2)) self.points_layer.setRendererV2(QgsSingleSymbolRendererV2(sym3)) - self.mapsettings = CANVAS.mapSettings() + self.mapsettings = self.iface.mapCanvas().mapSettings() self.mapsettings.setOutputSize(QSize(400, 400)) self.mapsettings.setOutputDpi(96) self.mapsettings.setExtent(QgsRectangle(-133, 22, -70, 52)) diff --git a/tests/src/python/test_qgsgraduatedsymbolrendererv2.py b/tests/src/python/test_qgsgraduatedsymbolrendererv2.py index 8cfa854aa76..b75c8afc296 100644 --- a/tests/src/python/test_qgsgraduatedsymbolrendererv2.py +++ b/tests/src/python/test_qgsgraduatedsymbolrendererv2.py @@ -14,10 +14,9 @@ __revision__ = '$Format:%H$' import qgis -from utilities import (unittest, - TestCase, - getQgisTestApp, - ) +from qgis.testing import (unittest, + start_app, + ) from qgis.core import (QgsGraduatedSymbolRendererV2, QgsRendererRangeV2, QgsRendererRangeV2LabelFormat, @@ -35,7 +34,7 @@ from PyQt4.QtCore import Qt from PyQt4.QtXml import QDomDocument from PyQt4.QtGui import QColor -QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() +start_app() #=========================================================== # Utility functions @@ -156,7 +155,7 @@ def dumpGraduatedRenderer(r): # Tests -class TestQgsGraduatedSymbolRendererV2(TestCase): +class TestQgsGraduatedSymbolRendererV2(unittest.TestCase): def testQgsRendererRangeV2_1(self): """Test QgsRendererRangeV2 getter/setter functions""" diff --git a/tests/src/python/test_qgsissue7244.py b/tests/src/python/test_qgsissue7244.py index 43e004eecda..ecc283f97c7 100644 --- a/tests/src/python/test_qgsissue7244.py +++ b/tests/src/python/test_qgsissue7244.py @@ -17,22 +17,21 @@ import os from qgis.core import QgsPoint, QgsVectorLayer -from utilities import (getQgisTestApp, - TestCase, - unittest - ) +from qgis.testing import (start_app, + unittest + ) from pyspatialite import dbapi2 as sqlite3 # Convenience instances in case you may need them -QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() +start_app() def die(error_message): raise Exception(error_message) -class TestQgsSpatialiteProvider(TestCase): +class TestQgsSpatialiteProvider(unittest.TestCase): @classmethod def setUpClass(cls): diff --git a/tests/src/python/test_qgslayerdefinition.py b/tests/src/python/test_qgslayerdefinition.py index 86e8e3a2957..80c4dc255b4 100644 --- a/tests/src/python/test_qgslayerdefinition.py +++ b/tests/src/python/test_qgslayerdefinition.py @@ -20,16 +20,20 @@ from qgis.core import (QGis, QgsLayerDefinition ) -from utilities import (TestCase, unittest, getQgisTestApp, unitTestDataPath) +from qgis.testing import ( + unittest, + start_app +) +from utilities import unitTestDataPath from PyQt4.QtCore import QVariant from PyQt4.QtXml import QDomDocument -QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() +start_app() TEST_DATA_DIR = unitTestDataPath() -class TestQgsLayerDefinition(TestCase): +class TestQgsLayerDefinition(unittest.TestCase): def testDependency(self): inDoc = """ diff --git a/tests/src/python/test_qgslogger.py b/tests/src/python/test_qgslogger.py index c0c44df53f2..49f086ea052 100644 --- a/tests/src/python/test_qgslogger.py +++ b/tests/src/python/test_qgslogger.py @@ -21,15 +21,15 @@ os.environ['QGIS_DEBUG'] = '2' os.environ['QGIS_LOG_FILE'] = myFilename from qgis.core import QgsLogger -from utilities import TestCase, unittest +from qgis.testing import TestCase, unittest # Convenience instances in case you may need them # not used in this test -#from utilities import getQgisTestApp -#QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() +#from qgis.testing import start_app +#start_app() -class TestQgsLogger(TestCase): +class TestQgsLogger(unittest.TestCase): def testLogger(self): try: diff --git a/tests/src/python/test_qgsmaplayerregistry.py b/tests/src/python/test_qgsmaplayerregistry.py index af853be1c5a..bbd08800686 100644 --- a/tests/src/python/test_qgsmaplayerregistry.py +++ b/tests/src/python/test_qgsmaplayerregistry.py @@ -12,8 +12,8 @@ __copyright__ = 'Copyright 2015, The QGIS Project' # This will get replaced with a git SHA1 when you do a git archive __revision__ = '$Format:%H$' -import unittest from qgis.core import QgsMapLayerRegistry +from qgis.testing import unittest class TestQgsMapLayerRegistry(unittest.TestCase): diff --git a/tests/src/python/test_qgsmapunitscale.py b/tests/src/python/test_qgsmapunitscale.py index d9e708bd67e..e1f1d3d6a3e 100644 --- a/tests/src/python/test_qgsmapunitscale.py +++ b/tests/src/python/test_qgsmapunitscale.py @@ -16,10 +16,10 @@ import qgis from qgis.core import (QgsMapUnitScale, QgsRenderContext, QgsSymbolLayerV2Utils, QgsSymbolV2, QgsMapSettings, QgsRectangle) from PyQt4.QtCore import QSize -from utilities import (TestCase, unittest) +from qgis.testing import (TestCase, unittest) -class PyQgsMapUnitScale(TestCase): +class PyQgsMapUnitScale(unittest.TestCase): def testConstructor(self): # test creating QgsMapUnitScale diff --git a/tests/src/python/test_qgsnetworkcontentfetcher.py b/tests/src/python/test_qgsnetworkcontentfetcher.py index 9d6795e0a82..db1c403be4f 100644 --- a/tests/src/python/test_qgsnetworkcontentfetcher.py +++ b/tests/src/python/test_qgsnetworkcontentfetcher.py @@ -14,9 +14,10 @@ __revision__ = '$Format:%H$' import qgis import os -from utilities import unittest, TestCase, unitTestDataPath +from qgis.testing import unittest from qgis.utils import qgsfunction from qgis.core import QgsNetworkContentFetcher +from utilities import unitTestDataPath from PyQt4.QtCore import QUrl, QCoreApplication from PyQt4.QtNetwork import QNetworkReply import SocketServer @@ -24,7 +25,7 @@ import threading import SimpleHTTPServer -class TestQgsNetworkContentFetcher(TestCase): +class TestQgsNetworkContentFetcher(unittest.TestCase): @classmethod def setUpClass(cls): diff --git a/tests/src/python/test_qgspallabeling_base.py b/tests/src/python/test_qgspallabeling_base.py index c48ea092a6e..263d18309ef 100644 --- a/tests/src/python/test_qgspallabeling_base.py +++ b/tests/src/python/test_qgspallabeling_base.py @@ -41,10 +41,14 @@ from qgis.core import ( QgsMultiRenderChecker ) +from qgis.testing import ( + start_app, + unittest +) + +from qgis.testing.mocked import get_iface + from utilities import ( - getQgisTestApp, - TestCase, - unittest, unitTestDataPath, getTempfilePath, renderMapToImage, @@ -54,7 +58,7 @@ from utilities import ( ) -QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() +start_app() FONTSLOADED = loadTestFonts() PALREPORT = 'PAL_REPORT' in os.environ @@ -62,7 +66,7 @@ PALREPORTS = {} # noinspection PyPep8Naming,PyShadowingNames -class TestQgsPalLabeling(TestCase): +class TestQgsPalLabeling(unittest.TestCase): _TestDataDir = unitTestDataPath() _PalDataDir = os.path.join(_TestDataDir, 'labeling') @@ -87,9 +91,9 @@ class TestQgsPalLabeling(TestCase): def setUpClass(cls): """Run before all tests""" - # qgis instances - cls._QgisApp, cls._Canvas, cls._Iface, cls._Parent = \ - QGISAPP, CANVAS, IFACE, PARENT + # qgis iface + cls._Iface = get_iface() + cls._Canvas = cls._Iface.mapCanvas() # verify that spatialite provider is available msg = '\nSpatialite provider not found, SKIPPING TEST SUITE' diff --git a/tests/src/python/test_qgspallabeling_placement.py b/tests/src/python/test_qgspallabeling_placement.py index 08ccd36b583..26a6619e41f 100644 --- a/tests/src/python/test_qgspallabeling_placement.py +++ b/tests/src/python/test_qgspallabeling_placement.py @@ -24,7 +24,7 @@ from PyQt4.QtCore import Qt, QPointF, QThreadPool from PyQt4.QtGui import QFont from qgis.core import QgsPalLayerSettings, QgsSingleSymbolRendererV2, QgsMarkerSymbolV2 -from unittest import skip + from utilities import ( svgSymbolsPath, getTempfilePath, diff --git a/tests/src/python/test_qgspoint.py b/tests/src/python/test_qgspoint.py index 0b7ac329afb..077e3e1a8aa 100644 --- a/tests/src/python/test_qgspoint.py +++ b/tests/src/python/test_qgspoint.py @@ -16,12 +16,12 @@ import qgis from qgis.core import QgsPoint -from utilities import getQgisTestApp, TestCase, unittest +from qgis.testing import start_app, unittest -QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() +start_app() -class TestQgsPoint(TestCase): +class TestQgsPoint(unittest.TestCase): def __init__(self, methodName): """Run once on class initialisation.""" diff --git a/tests/src/python/test_qgsproject.py b/tests/src/python/test_qgsproject.py index c18daa38448..2553dab3666 100644 --- a/tests/src/python/test_qgsproject.py +++ b/tests/src/python/test_qgsproject.py @@ -16,12 +16,12 @@ import qgis from qgis.core import QgsProject, QgsMessageLog -from utilities import getQgisTestApp, TestCase, unittest +from qgis.testing import start_app, unittest -QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() +start_app() -class TestQgsProject(TestCase): +class TestQgsProject(unittest.TestCase): def __init__(self, methodName): """Run once on class initialisation.""" diff --git a/tests/src/python/test_qgsrangewidgets.py b/tests/src/python/test_qgsrangewidgets.py index 1c15daee234..c0eaea7db18 100644 --- a/tests/src/python/test_qgsrangewidgets.py +++ b/tests/src/python/test_qgsrangewidgets.py @@ -21,15 +21,14 @@ from qgis.gui import QgsEditorWidgetRegistry from PyQt4 import QtCore, QtGui -from utilities import (unitTestDataPath, - getQgisTestApp, - TestCase, - unittest - ) -QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() +from qgis.testing import (start_app, + unittest + ) +from utilities import unitTestDataPath +start_app() -class TestQgsRangeWidget(TestCase): +class TestQgsRangeWidget(unittest.TestCase): @classmethod def setUpClass(cls): diff --git a/tests/src/python/test_qgsrasterfilewriter.py b/tests/src/python/test_qgsrasterfilewriter.py index 3a15ffb53cb..ad27ad5de7b 100644 --- a/tests/src/python/test_qgsrasterfilewriter.py +++ b/tests/src/python/test_qgsrasterfilewriter.py @@ -23,18 +23,16 @@ from qgis.core import (QgsRasterLayer, QgsRasterPipe, QgsRasterFileWriter, QgsRasterProjector) -from utilities import (unitTestDataPath, - getQgisTestApp, - TestCase, - unittest - # expectedFailure - ) -# Convenience instances in case you may need them -# not used in this test -QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() + +from qgis.testing import (start_app, + unittest) + +from utilities import unitTestDataPath + +start_app() -class TestQgsRasterFileWriter(TestCase): +class TestQgsRasterFileWriter(unittest.TestCase): def __init__(self, methodName): unittest.TestCase.__init__(self, methodName) diff --git a/tests/src/python/test_qgsrasterlayer.py b/tests/src/python/test_qgsrasterlayer.py index 2b0a7175e71..a6e3ccaa845 100644 --- a/tests/src/python/test_qgsrasterlayer.py +++ b/tests/src/python/test_qgsrasterlayer.py @@ -14,7 +14,6 @@ __revision__ = '$Format:%H$' import qgis import os -import unittest from PyQt4.QtCore import QFileInfo, QObject, SIGNAL from PyQt4 import QtGui @@ -31,16 +30,17 @@ from qgis.core import (QgsRaster, QgsRenderChecker, QgsSingleBandGrayRenderer, QgsSingleBandPseudoColorRenderer) -from utilities import (unitTestDataPath, - getQgisTestApp, - TestCase) +from utilities import unitTestDataPath +from qgis.testing import (start_app, + unittest + ) # Convenience instances in case you may need them # not used in this test -QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() +start_app() -class TestQgsRasterLayer(TestCase): +class TestQgsRasterLayer(unittest.TestCase): def testIdentify(self): myPath = os.path.join(unitTestDataPath(), 'landsat.tif') diff --git a/tests/src/python/test_qgsrectangle.py b/tests/src/python/test_qgsrectangle.py index cf0cf875d96..383d8411ed7 100644 --- a/tests/src/python/test_qgsrectangle.py +++ b/tests/src/python/test_qgsrectangle.py @@ -16,21 +16,23 @@ import qgis from qgis.core import QgsRectangle, QgsPoint -from utilities import (getQgisTestApp, - compareWkt, - TestCase, - unittest, - expectedFailure - ) +from qgis.testing import ( + start_app, + unittest +) -QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() +from utilities import ( + compareWkt +) + +start_app() -class TestQgsRectangle(TestCase): +class TestQgsRectangle(unittest.TestCase): # Because isEmpty() is not returning expected result in 9b0fee3 - @expectedFailure + @unittest.expectedFailure def testCtor(self): rect = QgsRectangle(5.0, 5.0, 10.0, 10.0) diff --git a/tests/src/python/test_qgsrelation.py b/tests/src/python/test_qgsrelation.py index b5d41622767..5bad7816b88 100644 --- a/tests/src/python/test_qgsrelation.py +++ b/tests/src/python/test_qgsrelation.py @@ -21,12 +21,11 @@ from qgis.core import (QgsVectorLayer, QgsPoint, QgsMapLayerRegistry ) -from utilities import (getQgisTestApp, - TestCase, - unittest - ) +from qgis.testing import (start_app, + unittest + ) -QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() +start_app() def createReferencingLayer(): @@ -70,7 +69,7 @@ def formatAttributes(attrs): return repr([unicode(a) for a in attrs]) -class TestQgsRelation(TestCase): +class TestQgsRelation(unittest.TestCase): def setUp(self): self.referencedLayer = createReferencedLayer() diff --git a/tests/src/python/test_qgsrelationeditwidget.py b/tests/src/python/test_qgsrelationeditwidget.py index afadd2dd5ab..0097d3691d2 100644 --- a/tests/src/python/test_qgsrelationeditwidget.py +++ b/tests/src/python/test_qgsrelationeditwidget.py @@ -53,16 +53,14 @@ from PyQt.QtGui import ( from time import sleep -from utilities import (unitTestDataPath, - getQgisTestApp, - TestCase, - unittest, - expectedFailure - ) -QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() +from qgis.testing import (start_app, + unittest + ) +from utilities import unitTestDataPath +start_app() -class TestQgsRelationEditWidget(TestCase): +class TestQgsRelationEditWidget(unittest.TestCase): @classmethod def setUpClass(cls): @@ -147,7 +145,7 @@ class TestQgsRelationEditWidget(TestCase): self.assertEquals(self.table_view.model().rowCount(), 4) - @expectedFailure + @unittest.expectedFailure def test_add_feature(self): """ Check if a new related feature is added @@ -199,7 +197,7 @@ class TestQgsRelationEditWidget(TestCase): self.assertEquals(self.table_view.model().rowCount(), 1) - @expectedFailure + @unittest.expectedFailure def test_unlink_feature(self): """ Check if a linked feature can be unlinked diff --git a/tests/src/python/test_qgsrulebasedrenderer.py b/tests/src/python/test_qgsrulebasedrenderer.py index 0ee2fc77368..d81408c9fac 100644 --- a/tests/src/python/test_qgsrulebasedrenderer.py +++ b/tests/src/python/test_qgsrulebasedrenderer.py @@ -40,20 +40,23 @@ from qgis.core import (QgsVectorLayer, QgsGraduatedSymbolRendererV2, QgsRendererRangeV2 ) -from utilities import (unitTestDataPath, - getQgisTestApp, - TestCase, - unittest - ) +from qgis.testing import (start_app, + unittest + ) + +from qgis.testing.mocked import get_iface + +from utilities import unitTestDataPath # Convenience instances in case you may need them # not used in this test -QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() +start_app() TEST_DATA_DIR = unitTestDataPath() -class TestQgsRulebasedRenderer(TestCase): +class TestQgsRulebasedRenderer(unittest.TestCase): def setUp(self): + self.iface = get_iface() myShpFile = os.path.join(TEST_DATA_DIR, 'rectangles.shp') layer = QgsVectorLayer(myShpFile, 'Points', 'ogr') QgsMapLayerRegistry.instance().addMapLayer(layer) @@ -74,7 +77,7 @@ class TestQgsRulebasedRenderer(TestCase): self.renderer = QgsRuleBasedRendererV2(self.rootrule) layer.setRendererV2(self.renderer) - self.mapsettings = CANVAS.mapSettings() + self.mapsettings = self.iface.mapCanvas().mapSettings() self.mapsettings.setOutputSize(QSize(400, 400)) self.mapsettings.setOutputDpi(96) self.mapsettings.setExtent(QgsRectangle(-163, 22, -70, 52)) diff --git a/tests/src/python/test_qgsserver.py b/tests/src/python/test_qgsserver.py index f056d1ea5ca..a91781056d4 100644 --- a/tests/src/python/test_qgsserver.py +++ b/tests/src/python/test_qgsserver.py @@ -14,12 +14,12 @@ __revision__ = '$Format:%H$' import os import re -import unittest import urllib from mimetools import Message from StringIO import StringIO from qgis.server import QgsServer from qgis.core import QgsMessageLog +from qgis.testing import unittest from utilities import unitTestDataPath # Strip path and content length because path may vary diff --git a/tests/src/python/test_qgsserver_accesscontrol.py b/tests/src/python/test_qgsserver_accesscontrol.py index cc8412825c5..4d3f943d658 100644 --- a/tests/src/python/test_qgsserver_accesscontrol.py +++ b/tests/src/python/test_qgsserver_accesscontrol.py @@ -18,7 +18,7 @@ import os from shutil import copyfile from math import sqrt from subprocess import check_output -from unittest import TestCase, main +from qgis.testing import unittest from utilities import unitTestDataPath from osgeo import gdal from osgeo.gdalconst import GA_ReadOnly @@ -155,7 +155,7 @@ accesscontrol = RestrictedAccessControl(server_iface) server_iface.registerAccessControl(accesscontrol, 100) -class TestQgsServerAccessControl(TestCase): +class TestQgsServerAccessControl(unittest.TestCase): def setUp(self): self.testdata_path = unitTestDataPath("qgis_server_accesscontrol") @@ -1105,4 +1105,4 @@ class TestQgsServerAccessControl(TestCase): "Wrong color in result\n%s" % response) if __name__ == "__main__": - main() + unittest.main() diff --git a/tests/src/python/test_qgssinglesymbolrenderer.py b/tests/src/python/test_qgssinglesymbolrenderer.py index d9303402af6..a2a8110b55d 100644 --- a/tests/src/python/test_qgssinglesymbolrenderer.py +++ b/tests/src/python/test_qgssinglesymbolrenderer.py @@ -41,20 +41,24 @@ from qgis.core import (QgsVectorLayer, QgsRendererRangeV2, QgsFeatureRequest ) -from utilities import (unitTestDataPath, - getQgisTestApp, - TestCase, - unittest - ) +from qgis.testing import (start_app, + unittest + ) + +from qgis.testing.mocked import get_iface + +from utilities import unitTestDataPath + # Convenience instances in case you may need them # not used in this test -QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() +start_app() TEST_DATA_DIR = unitTestDataPath() -class TestQgsSingleSymbolRenderer(TestCase): +class TestQgsSingleSymbolRenderer(unittest.TestCase): def setUp(self): + self.iface = get_iface() myShpFile = os.path.join(TEST_DATA_DIR, 'polys_overlapping.shp') layer = QgsVectorLayer(myShpFile, 'Polys', 'ogr') QgsMapLayerRegistry.instance().addMapLayer(layer) @@ -66,7 +70,7 @@ class TestQgsSingleSymbolRenderer(TestCase): layer.setRendererV2(self.renderer) rendered_layers = [layer.id()] - self.mapsettings = CANVAS.mapSettings() + self.mapsettings = self.iface.mapCanvas().mapSettings() self.mapsettings.setOutputSize(QSize(400, 400)) self.mapsettings.setOutputDpi(96) self.mapsettings.setExtent(QgsRectangle(-163, 22, -70, 52)) diff --git a/tests/src/python/test_qgssipcoverage.py b/tests/src/python/test_qgssipcoverage.py index 8e9c33d3f0a..7a3a9072c10 100644 --- a/tests/src/python/test_qgssipcoverage.py +++ b/tests/src/python/test_qgssipcoverage.py @@ -13,12 +13,9 @@ __copyright__ = 'Copyright 2015, The QGIS Project' __revision__ = '$Format:%H$' import os -from utilities import (TestCase, - unittest, - printImportant, - DoxygenParser) +from qgis.testing import unittest -from PyQt4.QtCore import qDebug +from utilities import printImportant, DoxygenParser # Import all the things! from qgis.analysis import * @@ -41,7 +38,7 @@ ACCEPTABLE_MISSING_CLASSES = 0 ACCEPTABLE_MISSING_MEMBERS = 0 -class TestQgsSipCoverage(TestCase): +class TestQgsSipCoverage(unittest.TestCase): def testCoverage(self): print 'CTEST_FULL_OUTPUT' diff --git a/tests/src/python/test_qgsspatialindex.py b/tests/src/python/test_qgsspatialindex.py index 872b20c0cd5..895554bd329 100644 --- a/tests/src/python/test_qgsspatialindex.py +++ b/tests/src/python/test_qgsspatialindex.py @@ -13,7 +13,6 @@ __copyright__ = 'Copyright 2012, The QGIS Project' __revision__ = '$Format:%H$' import qgis -import unittest from qgis.core import (QgsSpatialIndex, QgsFeature, @@ -21,9 +20,11 @@ from qgis.core import (QgsSpatialIndex, QgsRectangle, QgsPoint) -from utilities import getQgisTestApp +from qgis.testing import (start_app, + unittest + ) -QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() +start_app() class TestQgsSpatialIndex(unittest.TestCase): diff --git a/tests/src/python/test_qgssymbolexpressionvariables.py b/tests/src/python/test_qgssymbolexpressionvariables.py index 2cbf487b4f3..b833348d9ab 100644 --- a/tests/src/python/test_qgssymbolexpressionvariables.py +++ b/tests/src/python/test_qgssymbolexpressionvariables.py @@ -28,39 +28,43 @@ import os from PyQt4.QtCore import QSize -from qgis.core import (QgsVectorLayer, - QgsMapLayerRegistry, - QgsRectangle, - QgsMultiRenderChecker, - QgsSingleSymbolRendererV2, - QgsFillSymbolV2, - QgsMarkerSymbolV2, - QgsRendererCategoryV2, - QgsCategorizedSymbolRendererV2, - QgsGraduatedSymbolRendererV2, - QgsRendererRangeV2, - QgsFeatureRequest - ) -from utilities import (unitTestDataPath, - getQgisTestApp, - TestCase, - unittest - ) -# Convenience instances in case you may need them -# not used in this test -QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() +from qgis.core import ( + QgsVectorLayer, + QgsMapLayerRegistry, + QgsRectangle, + QgsMultiRenderChecker, + QgsSingleSymbolRendererV2, + QgsFillSymbolV2, + QgsMarkerSymbolV2, + QgsRendererCategoryV2, + QgsCategorizedSymbolRendererV2, + QgsGraduatedSymbolRendererV2, + QgsRendererRangeV2, + QgsFeatureRequest +) + +from qgis.testing import ( + unittest, + start_app +) +from qgis.testing.mocked import get_iface + +from utilities import unitTestDataPath + +start_app() TEST_DATA_DIR = unitTestDataPath() -class TestQgsSymbolExpressionVariables(TestCase): +class TestQgsSymbolExpressionVariables(unittest.TestCase): def setUp(self): myShpFile = os.path.join(TEST_DATA_DIR, 'polys.shp') self.layer = QgsVectorLayer(myShpFile, 'Polys', 'ogr') QgsMapLayerRegistry.instance().addMapLayer(self.layer) + self.iface = get_iface() rendered_layers = [self.layer.id()] - self.mapsettings = CANVAS.mapSettings() + self.mapsettings = self.iface.mapCanvas().mapSettings() self.mapsettings.setOutputSize(QSize(400, 400)) self.mapsettings.setOutputDpi(96) self.mapsettings.setExtent(QgsRectangle(-163, 22, -70, 52)) diff --git a/tests/src/python/test_qgssymbollayerv2.py b/tests/src/python/test_qgssymbollayerv2.py index d7a3dbcf92d..9a673d457e2 100644 --- a/tests/src/python/test_qgssymbollayerv2.py +++ b/tests/src/python/test_qgssymbollayerv2.py @@ -50,33 +50,31 @@ from qgis.core import (QgsCentroidFillSymbolLayerV2, QgsVectorFieldSymbolLayer, QgsSymbolV2, ) -from utilities import (unitTestDataPath, - getQgisTestApp, - TestCase, - unittest, - expectedFailure - ) +from qgis.testing import (start_app, + unittest + ) +from utilities import unitTestDataPath # Convenience instances in case you may need them # not used in this test -QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() +start_app() -class TestQgsSymbolLayerV2(TestCase): +class TestQgsSymbolLayerV2(unittest.TestCase): - ''' - This class test the sip binding for QgsSymbolLayerV2 descendants - Every class is tested using the createFromSld implementation - An exception is done for: - - QgsLinePatternFillSymbolLayer where createFromSld implementation - returns NULL - - QgsPointPatternFillSymbolLayer where createFromSld implementation - returns NULL - - QgsVectorFieldSymbolLayer where createFromSld implementation - returns NULL - ''' + """ + This class test the sip binding for QgsSymbolLayerV2 descendants + Every class is tested using the createFromSld implementation + An exception is done for: + - QgsLinePatternFillSymbolLayer where createFromSld implementation + returns NULL + - QgsPointPatternFillSymbolLayer where createFromSld implementation + returns NULL + - QgsVectorFieldSymbolLayer where createFromSld implementation + returns NULL + """ def testBinding(self): - '''Test python bindings existance.''' + """Test python bindings existance.""" mType = type(QgsSymbolLayerV2) mExpectedType = pyqtWrapperType mMessage = 'Expected "%s" got "%s"' % (mExpectedType, mType) @@ -211,8 +209,8 @@ class TestQgsSymbolLayerV2(TestCase): assert mExpectedType == mType, mMessage def testQgsSimpleFillSymbolLayerV2(self): - '''Create a new style from a .sld file and match test. - ''' + """Create a new style from a .sld file and match test. + """ mTestName = 'QgsSimpleFillSymbolLayerV2' mFilePath = QDir.toNativeSeparators('%s/symbol_layer/%s.sld' % (unitTestDataPath(), mTestName)) @@ -250,8 +248,8 @@ class TestQgsSymbolLayerV2(TestCase): assert mExpectedValue == mValue, mMessage def testQgsGradientFillSymbolLayerV2(self): - '''Test setting and getting QgsGradientFillSymbolLayerV2 properties. - ''' + """Test setting and getting QgsGradientFillSymbolLayerV2 properties. + """ mGradientLayer = QgsGradientFillSymbolLayerV2() mExpectedValue = type(QgsGradientFillSymbolLayerV2()) @@ -332,9 +330,9 @@ class TestQgsSymbolLayerV2(TestCase): assert mExpectedValue == mValue, mMessage def testQgsCentroidFillSymbolLayerV2(self): - ''' + """ Create a new style from a .sld file and match test - ''' + """ mTestName = 'QgsCentroidFillSymbolLayerV2' mFilePath = QDir.toNativeSeparators('%s/symbol_layer/%s.sld' % (unitTestDataPath(), mTestName)) @@ -367,9 +365,9 @@ class TestQgsSymbolLayerV2(TestCase): assert mExpectedValue == mValue, mMessage def testQgsLinePatternFillSymbolLayer(self): - ''' + """ Create a new style from a .sld file and match test - ''' + """ mTestName = 'QgsLinePatternFillSymbolLayer' mFilePath = QDir.toNativeSeparators('%s/symbol_layer/%s.sld' % (unitTestDataPath(), mTestName)) @@ -406,11 +404,11 @@ class TestQgsSymbolLayerV2(TestCase): mMessage = 'Expected "%s" got "%s"' % (mExpectedValue, mValue) assert mExpectedValue == mValue, mMessage - @expectedFailure + @unittest.expectedFailure def testQgsPointPatternFillSymbolLayer(self): - ''' + """ Create a new style from a .sld file and match test - ''' + """ # at the moment there is an empty createFromSld implementation # that return nulls mTestName = 'QgsPointPatternFillSymbolLayer' @@ -455,9 +453,9 @@ class TestQgsSymbolLayerV2(TestCase): assert mExpectedValue == mValue, mMessage def testQgsSVGFillSymbolLayer(self): - ''' + """ Create a new style from a .sld file and match test - ''' + """ mTestName = 'QgsSVGFillSymbolLayer' mFilePath = QDir.toNativeSeparators('%s/symbol_layer/%s.sld' % (unitTestDataPath(), mTestName)) @@ -485,9 +483,9 @@ class TestQgsSymbolLayerV2(TestCase): assert mExpectedValue == mValue, mMessage def testQgsMarkerLineSymbolLayerV2(self): - ''' + """ Create a new style from a .sld file and match test - ''' + """ mTestName = 'QgsMarkerLineSymbolLayerV2' mFilePath = QDir.toNativeSeparators('%s/symbol_layer/%s.sld' % (unitTestDataPath(), mTestName)) @@ -525,9 +523,9 @@ class TestQgsSymbolLayerV2(TestCase): assert mExpectedValue == mValue, mMessage def testQgsSimpleLineSymbolLayerV2(self): - ''' + """ Create a new style from a .sld file and match test - ''' + """ mTestName = 'QgsSimpleLineSymbolLayerV2' mFilePath = QDir.toNativeSeparators('%s/symbol_layer/%s.sld' % (unitTestDataPath(), mTestName)) @@ -575,9 +573,9 @@ class TestQgsSymbolLayerV2(TestCase): assert mExpectedValue == mValue, mMessage def testQgsEllipseSymbolLayerV2(self): - ''' + """ Create a new style from a .sld file and match test - ''' + """ mTestName = 'QgsEllipseSymbolLayerV2' mFilePath = QDir.toNativeSeparators('%s/symbol_layer/%s.sld' % (unitTestDataPath(), mTestName)) @@ -620,9 +618,9 @@ class TestQgsSymbolLayerV2(TestCase): assert mExpectedValue == mValue, mMessage def testQgsFontMarkerSymbolLayerV2(self): - ''' + """ Create a new style from a .sld file and match test - ''' + """ mTestName = 'QgsFontMarkerSymbolLayerV2' mFilePath = QDir.toNativeSeparators('%s/symbol_layer/%s.sld' % (unitTestDataPath(), mTestName)) @@ -660,9 +658,9 @@ class TestQgsSymbolLayerV2(TestCase): assert mExpectedValue == mValue, mMessage def testQgsSvgMarkerSymbolLayerV2(self): - ''' + """ Create a new style from a .sld file and match test - ''' + """ mTestName = 'QgsSvgMarkerSymbolLayerV2' mFilePath = QDir.toNativeSeparators('%s/symbol_layer/%s.sld' % (unitTestDataPath(), mTestName)) diff --git a/tests/src/python/test_qgssymbolv2.py b/tests/src/python/test_qgssymbolv2.py index 1b4c10d5237..ac9f0b8d411 100644 --- a/tests/src/python/test_qgssymbolv2.py +++ b/tests/src/python/test_qgssymbolv2.py @@ -26,12 +26,7 @@ __revision__ = '$Format:%H$' import os import csv -from utilities import (unitTestDataPath, - getQgisTestApp, - TestCase, - unittest, - expectedFailure - ) +from utilities import unitTestDataPath from PyQt4.QtCore import QSize, QDir from PyQt4.QtGui import QImage, QColor, QPainter @@ -48,11 +43,17 @@ from qgis.core import (QgsGeometry, QgsRenderChecker ) -QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() +from qgis.testing import( + unittest, + start_app +) + + +start_app() TEST_DATA_DIR = unitTestDataPath() -class TestQgsSymbolV2(TestCase): +class TestQgsSymbolV2(unittest.TestCase): def setUp(self): #Create some simple symbols diff --git a/tests/src/python/test_qgsvectorcolorramp.py b/tests/src/python/test_qgsvectorcolorramp.py index 3e42455a317..4297a0406c5 100644 --- a/tests/src/python/test_qgsvectorcolorramp.py +++ b/tests/src/python/test_qgsvectorcolorramp.py @@ -20,10 +20,10 @@ from qgis.core import (QgsVectorGradientColorRampV2, QgsRandomColorsV2, QgsVectorColorBrewerColorRampV2) from PyQt4.QtGui import QColor, QGradient -from utilities import (TestCase, unittest) +from qgis.testing import (TestCase, unittest) -class PyQgsVectorColorRamp(TestCase): +class PyQgsVectorColorRamp(unittest.TestCase): def testQgsVectorRandomColorRampV2(self): # test gradient with only start/end color diff --git a/tests/src/python/test_qgsvectorfilewriter.py b/tests/src/python/test_qgsvectorfilewriter.py index dff0ffff0d9..884eebade28 100644 --- a/tests/src/python/test_qgsvectorfilewriter.py +++ b/tests/src/python/test_qgsvectorfilewriter.py @@ -24,15 +24,17 @@ from qgis.core import (QgsVectorLayer, ) from PyQt4.QtCore import QDate, QTime, QDateTime, QVariant, QDir import os -from utilities import (getQgisTestApp, - TestCase, - unittest, - writeShape - ) -QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() +from qgis.testing import ( + start_app, + unittest +) + +from utilities import writeShape + +start_app() -class TestQgsVectorLayer(TestCase): +class TestQgsVectorLayer(unittest.TestCase): mMemoryLayer = None diff --git a/tests/src/python/test_qgsvectorlayer.py b/tests/src/python/test_qgsvectorlayer.py index 86c3153a0d8..c8a028939be 100644 --- a/tests/src/python/test_qgsvectorlayer.py +++ b/tests/src/python/test_qgsvectorlayer.py @@ -30,12 +30,11 @@ from qgis.core import (QGis, QgsVectorJoinInfo, QgsSymbolV2, QgsSingleSymbolRendererV2) -from utilities import (unitTestDataPath, - getQgisTestApp, - TestCase, - unittest, - ) -QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() +from qgis.testing import (start_app, + unittest + ) +from utilities import unitTestDataPath +start_app() def createEmptyLayer(): @@ -102,7 +101,7 @@ def dumpEditBuffer(layer): print "%d | %s" % (f.id(), f.geometry().exportToWkt()) -class TestQgsVectorLayer(TestCase): +class TestQgsVectorLayer(unittest.TestCase): def test_FeatureCount(self): myPath = os.path.join(unitTestDataPath(), 'lines.shp') diff --git a/tests/src/python/test_qgsvirtuallayerdefinition.py b/tests/src/python/test_qgsvirtuallayerdefinition.py index 8faf5bd42e8..9ce3ccab46b 100644 --- a/tests/src/python/test_qgsvirtuallayerdefinition.py +++ b/tests/src/python/test_qgsvirtuallayerdefinition.py @@ -21,12 +21,12 @@ from qgis.core import (QGis, QgsVirtualLayerDefinition ) -from utilities import (TestCase, unittest) +from qgis.testing import (TestCase, unittest) from PyQt4.QtCore import QVariant, QUrl -class TestQgsVirtualLayerDefinition(TestCase): +class TestQgsVirtualLayerDefinition(unittest.TestCase): def test1(self): d = QgsVirtualLayerDefinition() diff --git a/tests/src/python/test_qgszonalstatistics.py b/tests/src/python/test_qgszonalstatistics.py index 203be6d20ff..d00ade14296 100644 --- a/tests/src/python/test_qgszonalstatistics.py +++ b/tests/src/python/test_qgszonalstatistics.py @@ -19,16 +19,17 @@ from PyQt4.QtCore import QDir, QFile from qgis.core import QgsVectorLayer, QgsFeature, QgsFeatureRequest from qgis.analysis import QgsZonalStatistics -from utilities import ( - unitTestDataPath, - getQgisTestApp, - TestCase, - unittest) +from qgis.testing import ( + start_app, + unittest +) -QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() +from utilities import unitTestDataPath + +start_app() -class TestQgsZonalStatistics(TestCase): +class TestQgsZonalStatistics(unittest.TestCase): """Tests for zonal stats class.""" diff --git a/tests/src/python/test_syntactic_sugar.py b/tests/src/python/test_syntactic_sugar.py index 47ac7b1e1c7..eb3d828bcbd 100644 --- a/tests/src/python/test_syntactic_sugar.py +++ b/tests/src/python/test_syntactic_sugar.py @@ -14,10 +14,9 @@ __revision__ = '$Format:%H$' import qgis -from utilities import (unittest, - TestCase, - getQgisTestApp - ) +from qgis.testing import (unittest, + start_app + ) from qgis.core import (edit, QgsFeature, QgsGeometry, @@ -25,10 +24,10 @@ from qgis.core import (edit, QgsEditError ) -getQgisTestApp() +start_app() -class TestSyntacticSugar(TestCase): +class TestSyntacticSugar(unittest.TestCase): def testEdit(self): """Test `with edit(layer):` code""" diff --git a/tests/src/python/utilities.py b/tests/src/python/utilities.py index 6c236faa64c..1f5f6fa8515 100644 --- a/tests/src/python/utilities.py +++ b/tests/src/python/utilities.py @@ -32,7 +32,7 @@ from qgis.core import ( QgsFontUtils ) from qgis.gui import QgsMapCanvas -from qgis_interface import QgisInterface +from qgis.testing import start_app import hashlib import re try: @@ -43,24 +43,6 @@ except ImportError: import webbrowser import subprocess -# Support python < 2.7 via unittest2 needed for expected failure decorator. -# Note that you should ignore unused import warnings here as these are imported -# from this module by other tests. -if sys.version_info[0:2] < (2, 7): - try: - from unittest2 import TestCase, expectedFailure - import unittest2 as unittest - except ImportError: - print("You should install unittest2 to run the salt tests") - sys.exit(0) -else: - from unittest import TestCase, expectedFailure - import unittest - -QGISAPP = None # Static variable used to hold hand to running QGis app -CANVAS = None -PARENT = None -IFACE = None GEOCRS = 4326 # constant for EPSG:GEOCRS Geographic CRS id FONTSLOADED = False @@ -98,56 +80,6 @@ def hashForFile(theFilename): return myHash -def getQgisTestApp(): - """ Start one QGis application to test agaist - - Input - NIL - - Output - handle to qgis app - - - If QGis is already running the handle to that app will be returned - """ - - global QGISAPP # pylint: disable=W0603 - - if QGISAPP is None: - myGuiFlag = True # All test will run qgis in gui mode - - # In python3 we need to conver to a bytes object (or should - # QgsApplication accept a QString instead of const char* ?) - try: - argvb = list(map(os.fsencode, sys.argv)) - except AttributeError: - argvb = sys.argv - - # Note: QGIS_PREFIX_PATH is evaluated in QgsApplication - - # no need to mess with it here. - QGISAPP = QgsApplication(argvb, myGuiFlag) - - QGISAPP.initQgis() - s = QGISAPP.showSettings() - print(s) - - global PARENT # pylint: disable=W0603 - if PARENT is None: - PARENT = QWidget() - - global CANVAS # pylint: disable=W0603 - if CANVAS is None: - CANVAS = QgsMapCanvas(PARENT) - CANVAS.resize(QSize(400, 400)) - - global IFACE # pylint: disable=W0603 - if IFACE is None: - # QgisInterface is a stub implementation of the QGIS plugin interface - IFACE = QgisInterface(CANVAS) - - return QGISAPP, CANVAS, IFACE, PARENT - - def unitTestDataPath(theSubdir=None): """Return the absolute path to the QGIS unit test data dir. @@ -377,8 +309,7 @@ def getTestFont(style='Roman', size=12): def loadTestFonts(): - if QGISAPP is None: - getQgisTestApp() + start_app() global FONTSLOADED # pylint: disable=W0603 if FONTSLOADED is False: