mediator.py 2.5 KB

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