mediator.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """http://dpip.testingperspective.com/?p=28"""
  4. import random
  5. import time
  6. class TC:
  7. def __init__(self):
  8. self._tm = None
  9. self._bProblem = 0
  10. def setup(self):
  11. print("Setting up the Test")
  12. time.sleep(1)
  13. self._tm.prepareReporting()
  14. def execute(self):
  15. if not self._bProblem:
  16. print("Executing the test")
  17. time.sleep(1)
  18. else:
  19. print("Problem in setup. Test not executed.")
  20. def tearDown(self):
  21. if not self._bProblem:
  22. print("Tearing down")
  23. time.sleep(1)
  24. self._tm.publishReport()
  25. else:
  26. print("Test not executed. No tear down required.")
  27. def setTM(self, tm):
  28. self._tm = tm
  29. def setProblem(self, value):
  30. self._bProblem = value
  31. class Reporter:
  32. def __init__(self):
  33. self._tm = None
  34. def prepare(self):
  35. print("Reporter Class is preparing to report the results")
  36. time.sleep(1)
  37. def report(self):
  38. print("Reporting the results of Test")
  39. time.sleep(1)
  40. def setTM(self, tm):
  41. self._tm = tm
  42. class DB:
  43. def __init__(self):
  44. self._tm = None
  45. def insert(self):
  46. print("Inserting the execution begin status in the Database")
  47. time.sleep(1)
  48. #Following code is to simulate a communication from DB to TC
  49. import random
  50. if random.randrange(1, 4) == 3:
  51. return -1
  52. def update(self):
  53. print("Updating the test results in Database")
  54. time.sleep(1)
  55. def setTM(self, tm):
  56. self._tm = tm
  57. class TestManager:
  58. def __init__(self):
  59. self._reporter = None
  60. self._db = None
  61. self._tc = None
  62. def prepareReporting(self):
  63. rvalue = self._db.insert()
  64. if rvalue == -1:
  65. self._tc.setProblem(1)
  66. self._reporter.prepare()
  67. def setReporter(self, reporter):
  68. self._reporter = reporter
  69. def setDB(self, db):
  70. self._db = db
  71. def publishReport(self):
  72. self._db.update()
  73. self._reporter.report()
  74. def setTC(self, tc):
  75. self._tc = tc
  76. if __name__ == '__main__':
  77. reporter = Reporter()
  78. db = DB()
  79. tm = TestManager()
  80. tm.setReporter(reporter)
  81. tm.setDB(db)
  82. reporter.setTM(tm)
  83. db.setTM(tm)
  84. # For simplification we are looping on the same test.
  85. # Practically, it could be about various unique test classes and their
  86. # objects
  87. while True:
  88. tc = TC()
  89. tc.setTM(tm)
  90. tm.setTC(tc)
  91. tc.setup()
  92. tc.execute()
  93. tc.tearDown()