|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +""" |
| 5 | +http://web.archive.org/web/20120309135549/http://dpip.testingperspective.com/?p=28 |
| 6 | +
|
| 7 | +*TL;DR80 |
| 8 | +Encapsulates how a set of objects interact. |
| 9 | +""" |
| 10 | + |
| 11 | +import random |
| 12 | +import time |
| 13 | + |
| 14 | + |
| 15 | +class TC: |
| 16 | + """TestCategory |
| 17 | + responsible for running the tests with the help of setup(), execute() and tearDown() methods |
| 18 | + """ |
| 19 | + |
| 20 | + def __init__(self): |
| 21 | + self._tm = None |
| 22 | + self._bProblem = 0 |
| 23 | + |
| 24 | + def setup(self): |
| 25 | + print("Setting up the Test") |
| 26 | + time.sleep(0.1) |
| 27 | + self._tm.prepareReporting() |
| 28 | + |
| 29 | + def execute(self): |
| 30 | + if not self._bProblem: |
| 31 | + print("Executing the test") |
| 32 | + time.sleep(0.1) |
| 33 | + else: |
| 34 | + print("Problem in setup. Test not executed.") |
| 35 | + |
| 36 | + def tearDown(self): |
| 37 | + if not self._bProblem: |
| 38 | + print("Tearing down") |
| 39 | + time.sleep(0.1) |
| 40 | + self._tm.publishReport() |
| 41 | + else: |
| 42 | + print("Test not executed. No tear down required.") |
| 43 | + |
| 44 | + def setTM(self, tm): |
| 45 | + self._tm = tm |
| 46 | + |
| 47 | + def setProblem(self, value): |
| 48 | + self._bProblem = value |
| 49 | + |
| 50 | + |
| 51 | +class Reporter: |
| 52 | + """ |
| 53 | + - calls its prepare() method while TestCategory starts getting executed |
| 54 | + - calls its report() method when TestCategory finishes its execution. |
| 55 | + """ |
| 56 | + def __init__(self): |
| 57 | + self._tm = None |
| 58 | + |
| 59 | + def prepare(self): |
| 60 | + print("Reporter Class is preparing to report the results") |
| 61 | + time.sleep(0.1) |
| 62 | + |
| 63 | + def report(self): |
| 64 | + print("Reporting the results of Test") |
| 65 | + time.sleep(0.1) |
| 66 | + |
| 67 | + def setTM(self, tm): |
| 68 | + self._tm = tm |
| 69 | + |
| 70 | + |
| 71 | +class DB: |
| 72 | + """ |
| 73 | + stores the execution status of the test category by first |
| 74 | + calling the insert() method while the test category is in setup(), |
| 75 | + and then calls the update() method once the test category has finished execution. |
| 76 | + In this way, at any given point of time, the test execution status is available for framework user to query from the database |
| 77 | + """ |
| 78 | + def __init__(self): |
| 79 | + self._tm = None |
| 80 | + |
| 81 | + def insert(self): |
| 82 | + print("Inserting the execution begin status in the Database") |
| 83 | + time.sleep(0.1) |
| 84 | + # Following code is to simulate a communication from DB to TC |
| 85 | + if random.randrange(1, 4) == 3: |
| 86 | + return -1 |
| 87 | + |
| 88 | + def update(self): |
| 89 | + print("Updating the test results in Database") |
| 90 | + time.sleep(0.1) |
| 91 | + |
| 92 | + def setTM(self, tm): |
| 93 | + self._tm = tm |
| 94 | + |
| 95 | + |
| 96 | +class TestManager: |
| 97 | + """ |
| 98 | + Mediator between Class TC, Class Reporter and Class DB, the Colleagues in the system |
| 99 | + coordinates for |
| 100 | + test category execution (Class TC) |
| 101 | + and fetching the reports (Class Reporter) |
| 102 | + and getting the test execution status in database (Class DB) with the help of prepareReporting() and publishReport() methods |
| 103 | + """ |
| 104 | + def __init__(self): |
| 105 | + self._reporter = None |
| 106 | + self._db = None |
| 107 | + self._tc = None |
| 108 | + |
| 109 | + def prepareReporting(self): |
| 110 | + rvalue = self._db.insert() |
| 111 | + if rvalue == -1: |
| 112 | + self._tc.setProblem(1) |
| 113 | + self._reporter.prepare() |
| 114 | + |
| 115 | + def setReporter(self, reporter): |
| 116 | + self._reporter = reporter |
| 117 | + |
| 118 | + def setDB(self, db): |
| 119 | + self._db = db |
| 120 | + |
| 121 | + def publishReport(self): |
| 122 | + self._db.update() |
| 123 | + self._reporter.report() |
| 124 | + |
| 125 | + def setTC(self, tc): |
| 126 | + self._tc = tc |
| 127 | + |
| 128 | + |
| 129 | +if __name__ == '__main__': |
| 130 | + reporter = Reporter() |
| 131 | + db = DB() |
| 132 | + tm = TestManager() |
| 133 | + tm.setReporter(reporter) |
| 134 | + tm.setDB(db) |
| 135 | + reporter.setTM(tm) |
| 136 | + db.setTM(tm) |
| 137 | + # For simplification we are looping on the same test. |
| 138 | + # Practically, it could be about various unique test classes and their |
| 139 | + # objects |
| 140 | + for i in range(3): |
| 141 | + tc = TC() |
| 142 | + tc.setTM(tm) |
| 143 | + tm.setTC(tc) |
| 144 | + tc.setup() |
| 145 | + tc.execute() |
| 146 | + tc.tearDown() |
| 147 | + |
| 148 | +### OUTPUT ### |
| 149 | +# Setting up the Test |
| 150 | +# Inserting the execution begin status in the Database |
| 151 | +# Executing the test |
| 152 | +# Tearing down |
| 153 | +# Updating the test results in Database |
| 154 | +# Reporting the results of Test |
| 155 | +# Setting up the Test |
| 156 | +# Inserting the execution begin status in the Database |
| 157 | +# Reporter Class is preparing to report the results |
| 158 | +# Problem in setup. Test not executed. |
| 159 | +# Test not executed. No tear down required. |
| 160 | +# Setting up the Test |
| 161 | +# Inserting the execution begin status in the Database |
| 162 | +# Executing the test |
| 163 | +# Tearing down |
| 164 | +# Updating the test results in Database |
| 165 | +# Reporting the results of Test |
0 commit comments