From a8cdde5232edbec985e1ab76bacc37a69562d6f5 Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Fri, 23 Jun 2017 11:36:06 +1000 Subject: [PATCH] Add test that python exception is caught when executing an alg --- .../processing/tests/QgisAlgorithmsTest.py | 32 +++++++++++++++++++ .../plugins/processing/tools/dataobjects.py | 2 +- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/python/plugins/processing/tests/QgisAlgorithmsTest.py b/python/plugins/processing/tests/QgisAlgorithmsTest.py index b306cb6b361..a5d770d4249 100644 --- a/python/plugins/processing/tests/QgisAlgorithmsTest.py +++ b/python/plugins/processing/tests/QgisAlgorithmsTest.py @@ -30,7 +30,28 @@ import AlgorithmsTestBase import nose2 import shutil +from qgis.core import (QgsProcessingAlgorithm, + QgsProcessingFeedback) from qgis.testing import start_app, unittest +from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException +from processing.tools.dataobjects import createContext + + +class TestAlg(QgsProcessingAlgorithm): + + def __init__(self): + super().__init__() + + def name(self): + return 'testalg' + + def displayName(self): + return 'testalg' + + def processAlgorithm(self, parameters, context, feedback): + raise GeoAlgorithmExecutionException( + self.tr('Exception while processing')) + return {} class TestQgisAlgorithms(unittest.TestCase, AlgorithmsTestBase.AlgorithmsTest): @@ -52,6 +73,17 @@ class TestQgisAlgorithms(unittest.TestCase, AlgorithmsTestBase.AlgorithmsTest): def test_definition_file(self): return 'qgis_algorithm_tests.yaml' + def testProcessingException(self): + """ + Test that Python exception is caught when running an alg + """ + + alg = TestAlg() + context = createContext() + feedback = QgsProcessingFeedback() + results, ok = alg.run({}, context, feedback) + self.assertFalse(ok) + if __name__ == '__main__': nose2.main() diff --git a/python/plugins/processing/tools/dataobjects.py b/python/plugins/processing/tools/dataobjects.py index bd05601c520..93b63b7c8f5 100644 --- a/python/plugins/processing/tools/dataobjects.py +++ b/python/plugins/processing/tools/dataobjects.py @@ -65,7 +65,7 @@ TYPE_FILE = 4 TYPE_TABLE = 5 -def createContext(feedback): +def createContext(feedback=None): """ Creates a default processing context """