Skip to content
Permalink
Browse files

Python unittest.expectedFailure improvements

Raises an _UnexpectedSuccess exception
Takes a boolean parameter with a condition under which the test is
expected to fail
  • Loading branch information
m-kuhn committed Feb 26, 2016
1 parent d8ad5a3 commit c79aeba0dbd3aa40b8d74c9739b65321e53b91e6
Showing with 66 additions and 4 deletions.
  1. +64 −0 python/testing/__init__.py
  2. +2 −4 tests/src/python/test_qgsvectorfilewriter.py
@@ -28,6 +28,7 @@
import os
import sys
import difflib
import functools

from PyQt4.QtCore import QVariant
from qgis.core import QgsApplication, QgsFeatureRequest, QgsVectorLayer
@@ -154,8 +155,71 @@ def assertFilesEqual(self, filepath_expected, filepath_result):
self.assertEqual(0, len(diff), ''.join(diff))


class _UnexpectedSuccess(Exception):
"""
The test was supposed to fail, but it didn't!
"""
pass


def expectedFailure(*args):
"""
Will decorate a unittest function as an expectedFailure. A function
flagged as expectedFailure will be succeed if it raises an exception.
If it does not raise an exception, this will throw an
`_UnexpectedSuccess` exception.
@expectedFailure
def my_test(self):
self.assertTrue(False)
The decorator also accepts a parameter to only expect a failure under
certain conditions.
@expectedFailure(time.localtime().tm_year < 2002)
def my_test(self):
self.assertTrue(qgisIsInvented())
"""
if hasattr(args[0], '__call__'):
# We got a function as parameter: assume usage like
# @expectedFailure
# def testfunction():
func = args[0]

@functools.wraps(func)
def wrapper(*args, **kwargs):
try:
func(*args, **kwargs)
except Exception:
pass
else:
raise _UnexpectedSuccess
return wrapper
else:
# We got a function as parameter: assume usage like
# @expectedFailure(failsOnThisPlatform)
# def testfunction():
condition = args[0]

def realExpectedFailure(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
if condition:
try:
func(*args, **kwargs)
except Exception:
pass
else:
raise _UnexpectedSuccess
else:
func(*args, **kwargs)
return wrapper

return realExpectedFailure

# Patch unittest
unittest.TestCase = TestCase
unittest.expectedFailure = expectedFailure


def start_app():
@@ -172,13 +172,11 @@ def testDateTimeWriteTabfile(self):
assert isinstance(f.attributes()[datetime_idx], QDateTime)
self.assertEqual(f.attributes()[datetime_idx], QDateTime(QDate(2014, 3, 5), QTime(13, 45, 22)))

# This test fails on Travis Linux build for unknown reason (probably GDAL version related)
@unittest.expectedFailure(os.environ.get('TRAVIS') and 'linux' in platform.system().lower())
def testWriteShapefileWithZ(self):
"""Check writing geometries with Z dimension to an ESRI shapefile."""

if os.environ.get('TRAVIS') and 'linux' in platform.system().lower():
# This test fails on Travis Linux build for unknown reason (probably GDAL version related)
return

#start by saving a memory layer and forcing z
ml = QgsVectorLayer(
('Point?crs=epsg:4326&field=id:int'),

1 comment on commit c79aeba

@nyalldawson

This comment has been minimized.

Copy link
Collaborator

@nyalldawson nyalldawson commented on c79aeba Feb 26, 2016

@m-kuhn really nice, thank you!

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