mediator.py 2.6 KB

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