0
所以我想設置爲螺紋類單元測試在Python蟒蛇單元測試是不允許對象屬性引用
類看起來是這樣的:
@Singleton
class EventManager(threading.Thread):
def __init__(self):
self.__eventDict = {}
self.__eventDictLock = threading.Lock()
self.__eventQueue = Queue()
self.__subscriberList = []
threading.Thread.__init__(self)
def run(self):
while(True):
if self.__eventQueue.qsize() > 0:
self.__runEvent()
else:
time.sleep(1)
我的單元測試是這樣的:
eventManager = EventManager.Instance()
eventManager.start()
class EventManagerTest(unittest.TestCase):
#pre-test initialization
def setUp(self):
pass
#post-test destruction
def tearDown(self):
pass
def testRegisterEvent(self):
global eventManager
logger.debug("Entering testRegisterEvent()")
eventManager.registerEvent("MyEvent")
logger.debug("testRegisterEvent() appears to have successfully registered")
self.assertIn("MyEvent", eventManager.__subscriberList)
self.assertFalse(eventManager.__eventDict["MyEvent"])
而且我得到這樣的錯誤:
ERROR: testRegisterEvent (__main__.EventManagerTest)
Traceback (most recent call last):
File "EventManager_Test.py", line 59, in testRegisterEvent
self.assertIn("MyEvent", eventManager.__eventDict)
AttributeError: 'EventManager' object has no attribute '\_EventManagerTest\__eventDict'
_EventManagerTest__eventDict
屬性來自哪裏?這不是我打電話的屬性,它阻止了我運行單元測試。
謝謝。我絕對學到了關於python的新東西。聽起來像是我將來必須記住的細節之一。 –