Adds Mox cleanup code to any MoxTestBase method. Always unsets stubs after a test. Will verify all mocks for tests that otherwise pass. Args: cls: MoxTestBase or subclass; the class whose test method we are altering. func: method; the method of the MoxTestBase test class we
(cls, func)
| 1356 | |
| 1357 | @staticmethod |
| 1358 | def CleanUpTest(cls, func): |
| 1359 | """Adds Mox cleanup code to any MoxTestBase method. |
| 1360 | |
| 1361 | Always unsets stubs after a test. Will verify all mocks for tests that |
| 1362 | otherwise pass. |
| 1363 | |
| 1364 | Args: |
| 1365 | cls: MoxTestBase or subclass; the class whose test method we are altering. |
| 1366 | func: method; the method of the MoxTestBase test class we wish to alter. |
| 1367 | |
| 1368 | Returns: |
| 1369 | The modified method. |
| 1370 | """ |
| 1371 | def new_method(self, *args, **kwargs): |
| 1372 | mox_obj = getattr(self, 'mox', None) |
| 1373 | cleanup_mox = False |
| 1374 | if mox_obj and isinstance(mox_obj, Mox): |
| 1375 | cleanup_mox = True |
| 1376 | try: |
| 1377 | func(self, *args, **kwargs) |
| 1378 | finally: |
| 1379 | if cleanup_mox: |
| 1380 | mox_obj.UnsetStubs() |
| 1381 | if cleanup_mox: |
| 1382 | mox_obj.VerifyAll() |
| 1383 | new_method.__name__ = func.__name__ |
| 1384 | new_method.__doc__ = func.__doc__ |
| 1385 | new_method.__module__ = func.__module__ |
| 1386 | return new_method |
| 1387 | |
| 1388 | |
| 1389 | class MoxTestBase(unittest.TestCase): |