@@ -113,3 +113,60 @@ def wrapped(*args, **kwargs):
113
113
sys .stdout = sys .__stdout__
114
114
115
115
return wrapped
116
+
117
+
118
+ class KnownFailureTest (Exception ):
119
+ '''Raise this exception to mark a test as a known failing test.'''
120
+ pass
121
+
122
+ def knownfailureif (fail_condition , msg = None ):
123
+ """
124
+ Make function raise KnownFailureTest exception if given condition is true.
125
+
126
+ If the condition is a callable, it is used at runtime to dynamically
127
+ make the decision. This is useful for tests that may require costly
128
+ imports, to delay the cost until the test suite is actually executed.
129
+
130
+ Parameters
131
+ ----------
132
+ fail_condition : bool or callable
133
+ Flag to determine whether to mark the decorated test as a known
134
+ failure (if True) or not (if False).
135
+ msg : str, optional
136
+ Message to give on raising a KnownFailureTest exception.
137
+ Default is None.
138
+
139
+ Returns
140
+ -------
141
+ decorator : function
142
+ Decorator, which, when applied to a function, causes SkipTest
143
+ to be raised when `skip_condition` is True, and the function
144
+ to be called normally otherwise.
145
+
146
+ Notes
147
+ -----
148
+ The decorator itself is decorated with the ``nose.tools.make_decorator``
149
+ function in order to transmit function name, and various other metadata.
150
+
151
+ """
152
+ if msg is None :
153
+ msg = 'Test skipped due to known failure'
154
+
155
+ # Allow for both boolean or callable known failure conditions.
156
+ if callable (fail_condition ):
157
+ fail_val = fail_condition
158
+ else :
159
+ fail_val = lambda : fail_condition
160
+
161
+ def knownfail_decorator (f ):
162
+ # Local import to avoid a hard nose dependency and only incur the
163
+ # import time overhead at actual test-time.
164
+ import nose
165
+ def knownfailer (* args , ** kwargs ):
166
+ if fail_val ():
167
+ raise KnownFailureTest , msg
168
+ else :
169
+ return f (* args , ** kwargs )
170
+ return nose .tools .make_decorator (f )(knownfailer )
171
+
172
+ return knownfail_decorator
0 commit comments