Skip to content

Commit

Permalink
Python unittest.expectedFailure improvements
Browse files Browse the repository at this point in the history
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 c79aeba
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 4 deletions.
64 changes: 64 additions & 0 deletions python/testing/__init__.py
Expand Up @@ -28,6 +28,7 @@
import os
import sys
import difflib
import functools

from PyQt4.QtCore import QVariant
from qgis.core import QgsApplication, QgsFeatureRequest, QgsVectorLayer
Expand Down Expand Up @@ -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():
Expand Down
6 changes: 2 additions & 4 deletions tests/src/python/test_qgsvectorfilewriter.py
Expand Up @@ -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'),
Expand Down

1 comment on commit c79aeba

@nyalldawson
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@m-kuhn really nice, thank you!

Please sign in to comment.