|
| 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 | +importrandom |
| 12 | +importtime |
| 13 | + |
| 14 | + |
| 15 | +classTC: |
| 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 | +defsetup(self): |
| 25 | +print("Setting up the Test") |
| 26 | +time.sleep(0.1) |
| 27 | +self._tm.prepareReporting() |
| 28 | + |
| 29 | +defexecute(self): |
| 30 | +ifnotself._bProblem: |
| 31 | +print("Executing the test") |
| 32 | +time.sleep(0.1) |
| 33 | +else: |
| 34 | +print("Problem in setup. Test not executed.") |
| 35 | + |
| 36 | +deftearDown(self): |
| 37 | +ifnotself._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 | +defsetTM(self, tm): |
| 45 | +self._tm=tm |
| 46 | + |
| 47 | +defsetProblem(self, value): |
| 48 | +self._bProblem=value |
| 49 | + |
| 50 | + |
| 51 | +classReporter: |
| 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 | +defprepare(self): |
| 60 | +print("Reporter Class is preparing to report the results") |
| 61 | +time.sleep(0.1) |
| 62 | + |
| 63 | +defreport(self): |
| 64 | +print("Reporting the results of Test") |
| 65 | +time.sleep(0.1) |
| 66 | + |
| 67 | +defsetTM(self, tm): |
| 68 | +self._tm=tm |
| 69 | + |
| 70 | + |
| 71 | +classDB: |
| 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 | +definsert(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 | +ifrandom.randrange(1, 4) ==3: |
| 86 | +return-1 |
| 87 | + |
| 88 | +defupdate(self): |
| 89 | +print("Updating the test results in Database") |
| 90 | +time.sleep(0.1) |
| 91 | + |
| 92 | +defsetTM(self, tm): |
| 93 | +self._tm=tm |
| 94 | + |
| 95 | + |
| 96 | +classTestManager: |
| 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 | +defprepareReporting(self): |
| 110 | +rvalue=self._db.insert() |
| 111 | +ifrvalue==-1: |
| 112 | +self._tc.setProblem(1) |
| 113 | +self._reporter.prepare() |
| 114 | + |
| 115 | +defsetReporter(self, reporter): |
| 116 | +self._reporter=reporter |
| 117 | + |
| 118 | +defsetDB(self, db): |
| 119 | +self._db=db |
| 120 | + |
| 121 | +defpublishReport(self): |
| 122 | +self._db.update() |
| 123 | +self._reporter.report() |
| 124 | + |
| 125 | +defsetTC(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 | +foriinrange(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