[MERGE] Support old API properly
John Arbash Meinel
john at arbash-meinel.com
Thu Aug 17 14:05:24 BST 2006
Aaron Bentley wrote:
> John Arbash Meinel wrote:
>>> When testing deprecated APIs, the way I test them is to have them not
>>> directly import warnings.warn or symbol_versioning.warn. Instead to
>>> import symbol_versioning.
>>>
>>> After that, there is the helper function:
>>> symbol_versioning.set_warning_method()
>>>
>>> This lets you override the default warning method with a collector. And
>>> then you have the opportunity to both assert that the deprecation is
>>> being emitted *and* that it does the right thing.
>
> Thanks for the tip. I didn't realize we had that.
>
> I believe this method is generally useful, so I have automated it as
> assertDeprecated (similar to assertRaises).
>
> Aaron
+ def assertDeprecated(self, expected, callable, *args, **kwargs):
+ """Assert that a callable is deprecated in a particular way.
+
+ :param expected: a list of the deprecation warnings expected,
in order
+ :param callable: The callable to call
+ :param args: The positional arguments for the callable
+ :param kwargs: The keyword arguments for the callable
+ """
+ local_warnings = []
+ def capture_warnings(msg, cls, stacklevel=None):
+ self.assertEqual(cls, DeprecationWarning)
+ local_warnings.append(msg)
+ method = symbol_versioning.warn
+ symbol_versioning.set_warning_method(capture_warnings)
+ try:
+ callable(*args, **kwargs)
+ finally:
+ result = symbol_versioning.set_warning_method(method)
+ self.assertEqual(expected, local_warnings)
+ return result
+
^- I think assertDeprecated should return the callable's result, not the
result from symbol_versioning.
try:
result = callable(*args, **kwargs)
finally:
symbol_versioning.set_warning_method(method)
self.assertEqual(expected, local_warnings)
return result
That way, we can call something, and have it return something.
Otherwise +1 from me after you write a couple of selftest cases for Robert.
John
=:->
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 254 bytes
Desc: OpenPGP digital signature
Url : https://lists.ubuntu.com/archives/bazaar/attachments/20060817/7056e33f/attachment.pgp
More information about the bazaar
mailing list