mediator.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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(0.1)
  13. self._tm.prepareReporting()
  14. def execute(self):
  15. if not self._bProblem:
  16. print("Executing the test")
  17. time.sleep(0.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(0.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(0.1)
  37. def report(self):
  38. print("Reporting the results of Test")
  39. time.sleep(0.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(0.1)
  48. # Following code is to simulate a communication from DB to TC
  49. if random.randrange(1, 4) == 3:
  50. return -1
  51. def update(self):
  52. print("Updating the test results in Database")
  53. time.sleep(0.1)
  54. def setTM(self, tm):
  55. self._tm = tm
  56. class TestManager:
  57. def __init__(self):
  58. self._reporter = None
  59. self._db = None
  60. self._tc = None
  61. def prepareReporting(self):
  62. rvalue = self._db.insert()
  63. if rvalue == -1:
  64. self._tc.setProblem(1)
  65. self._reporter.prepare()
  66. def setReporter(self, reporter):
  67. self._reporter = reporter
  68. def setDB(self, db):
  69. self._db = db
  70. def publishReport(self):
  71. self._db.update()
  72. self._reporter.report()
  73. def setTC(self, tc):
  74. self._tc = tc
  75. if __name__ == '__main__':
  76. reporter = Reporter()
  77. db = DB()
  78. tm = TestManager()
  79. tm.setReporter(reporter)
  80. tm.setDB(db)
  81. reporter.setTM(tm)
  82. db.setTM(tm)
  83. # For simplification we are looping on the same test.
  84. # Practically, it could be about various unique test classes and their
  85. # objects
  86. for i in range(3):
  87. tc = TC()
  88. tc.setTM(tm)
  89. tm.setTC(tc)
  90. tc.setup()
  91. tc.execute()
  92. tc.tearDown()
  93. ### OUTPUT ###
  94. # Setting up the Test
  95. # Inserting the execution begin status in the Database
  96. # Executing the test
  97. # Tearing down
  98. # Updating the test results in Database
  99. # Reporting the results of Test
  100. # Setting up the Test
  101. # Inserting the execution begin status in the Database
  102. # Reporter Class is preparing to report the results
  103. # Problem in setup. Test not executed.
  104. # Test not executed. No tear down required.
  105. # Setting up the Test
  106. # Inserting the execution begin status in the Database
  107. # Executing the test
  108. # Tearing down
  109. # Updating the test results in Database
  110. # Reporting the results of Test