mox_test_helper.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/usr/bin/python2.4
  2. #
  3. # Copyright 2008 Google Inc.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. """A very basic test class derived from mox.MoxTestBase, used by mox_test.py.
  17. The class defined in this module is used to test the features of
  18. MoxTestBase and is not intended to be a standalone test. It needs to
  19. be in a separate module, because otherwise the tests in this class
  20. (which should not all pass) would be executed as part of the
  21. mox_test.py test suite.
  22. See mox_test.MoxTestBaseTest for how this class is actually used.
  23. """
  24. import os
  25. import mox
  26. class ExampleMoxTestMixin(object):
  27. """Mix-in class for mox test case class.
  28. It stubs out the same function as one of the test methods in
  29. the example test case. Both tests must pass as meta class wraps
  30. test methods in all base classes.
  31. """
  32. def testStat(self):
  33. self.mox.StubOutWithMock(os, 'stat')
  34. os.stat(self.DIR_PATH)
  35. self.mox.ReplayAll()
  36. os.stat(self.DIR_PATH)
  37. class ExampleMoxTest(mox.MoxTestBase, ExampleMoxTestMixin):
  38. DIR_PATH = '/path/to/some/directory'
  39. def testSuccess(self):
  40. self.mox.StubOutWithMock(os, 'listdir')
  41. os.listdir(self.DIR_PATH)
  42. self.mox.ReplayAll()
  43. os.listdir(self.DIR_PATH)
  44. def testExpectedNotCalled(self):
  45. self.mox.StubOutWithMock(os, 'listdir')
  46. os.listdir(self.DIR_PATH)
  47. self.mox.ReplayAll()
  48. def testUnexpectedCall(self):
  49. self.mox.StubOutWithMock(os, 'listdir')
  50. os.listdir(self.DIR_PATH)
  51. self.mox.ReplayAll()
  52. os.listdir('/path/to/some/other/directory')
  53. os.listdir(self.DIR_PATH)
  54. def testFailure(self):
  55. self.assertTrue(False)
  56. def testStatOther(self):
  57. self.mox.StubOutWithMock(os, 'stat')
  58. os.stat(self.DIR_PATH)
  59. self.mox.ReplayAll()
  60. os.stat(self.DIR_PATH)