在此问题立即被标记为重复之前,让我说我已经尝试了每个 解决方案 ,这是与我的情况最相关的两个问题.如果有人能在必要时关闭此问题之前至少查看我的特定问题,我将不胜感激.
我有一个名为e的有限状态机对象,它是一个MCFiniteSM对象.e的核心是名为state_dict的字典,其存储"进程"id('1','2'等)以及存储关于每个"进程"的更多信息的相关字典.我正在运行unittests来添加进程,根据给定的参数更改状态等.但是,在unittest文件中的函数调用之间,有限状态机似乎被清除了.我已经查看了上面列出的两个问题,以避免这种情况并坚持更改,但无论我尝试对有限状态机的更改都不会持久化.这是最简单的文件.
from finite_state_machine import MCFiniteSM
from unittest import TestLoader, TestCase, main as unimain
from datetime import datetime
import time, calendar
class MyUnitTest(TestCase):
@classmethod
def setUpClass(cls):
cls.e = MCFiniteSM()
cls.timestamp = datetime.strftime(datetime.fromtimestamp(calendar.timegm(time.gmtime())), '%Y/%m/%d %H:%M:%S')
class TestFSM(MyUnitTest):
@classmethod
def setUpClass(cls):
super(TestFSM, cls).setUpClass()
#e = MCFiniteSM()
#timestamp = datetime.strftime(datetime.fromtimestamp(calendar.timegm(time.gmtime())), '%Y/%m/%d %H:%M:%S')
def test_add_convert_processes(self):
self.e.add_process('1', 'S', self.timestamp, 100, 'start message for process 1')
self.e.add_process('2', 'S', self.timestamp, 200, 'start message for process 2')
self.e.add_process('3', 'S', self.timestamp, 300, 'start message for process 3')
self.e.add_process('4', 'S', self.timestamp, 400, 'start message for process 4')
self.e.add_process('5', 'S', self.timestamp, 500, 'start message for process 5')
self.e.add_process('6', 'S', self.timestamp, 600, 'start message for process 6')
self.assertEqual(self.e.state_dict['1'], {'id':'1', 'message_type': 'S', 'timestamp':self.timestamp, 'state': 'i', 'threshold':100, 'message': 'start message for process 1'})
self.assertEqual(self.e.state_dict['2'], {'id':'2', 'message_type': 'S', 'timestamp':self.timestamp, 'state': 'i', 'threshold':200, 'message': 'start message for process 2'})
self.assertEqual(self.e.state_dict['3'], {'id':'3', 'message_type': 'S', 'timestamp':self.timestamp, 'state': 'i', 'threshold':300, 'message': 'start message for process 3'})
self.assertEqual(self.e.state_dict['4'], {'id':'4', 'message_type': 'S', 'timestamp':self.timestamp, 'state': 'i', 'threshold':400, 'message': 'start message for process 4'})
self.assertEqual(self.e.state_dict['5'], {'id':'5', 'message_type': 'S', 'timestamp':self.timestamp, 'state': 'i', 'threshold':500, 'message': 'start message for process 5'})
self.assertEqual(self.e.state_dict['6'], {'id':'6', 'message_type': 'S', 'timestamp':self.timestamp, 'state': 'i', 'threshold':600, 'message': 'start message for process 6'})
self.e.convert_state('1', 'S')
self.e.convert_state('2', 'S')
self.e.convert_state('3', 'S')
self.e.convert_state('4', 'S')
self.e.convert_state('5', 'S')
self.e.convert_state('6', 'S')
self.assertEqual(self.e.state_dict['2']['state'], 'a')
self.assertEqual(self.e.state_dict['3']['state'], 'a')
self.assertEqual(self.e.state_dict['4']['state'], 'a')
self.e.add_process('2', 'E', self.timestamp, None, 'end message for process 2')
self.e.add_process('3', 'E', self.timestamp, None, 'end message for process 3')
self.e.add_process('4', 'E', self.timestamp, None, 'end message for process 4')
self.assertEqual(self.e.state_dict['2']['state'], 'i')
self.assertEqual(self.e.state_dict['3']['state'], 'i')
self.assertEqual(self.e.state_dict['4']['state'], 'i')
def test_active_start_conversion(self):
print self.e
print 'trying...'
import sys
from StringIO import StringIO
orig = sys.stdout
try:
output = StringIO()
sys.stdout = output
self.e.convert_state('1', 'S')
out = output.getvalue().strip()
test = "Process ID:", '1', "\n" \
"Process Message Type:", 'S', "\n" \
"Process timestamp:", self.timestamp, "\n" \
"Process Threshold:", 100, "\n" \
"Process Message:", 'start message for process 1', "\n" \
"Log Message:", "PROCESS WITH ID 1 SENT MULTIPLE START MESSAGES", "\n"
self.assertEqual(out, test)
finally:
sys.stdout = orig
if __name__ == '__main__':
unimain()
Run Code Online (Sandbox Code Playgroud)
'e'是我想在函数调用之间保持修改的变量.当我TestFSM.test_active_start_conversion打印到0的打印尺寸时,它应该是6.该TestFSM.test_add_convert_processes方法成功运行.
实际错误是一个关键错误.这是堆栈跟踪:
Error
Traceback (most recent call last):
File "/home/Desktop/fsm_unit_tests.py", line 68, in test_active_start_conversion
self.e.convert_state('1', 'S')
File "/home/Desktop/finite_state_machine.py", line 26, in convert_state
cb_id = self.state_dict[id]['id']
KeyError: '1'
Run Code Online (Sandbox Code Playgroud)
抛出错误的行是self.e.convert_state('1', 'S')我告诉fsm改变ID为'1'的进程状态的地方.它认为没有ID为1的进程,并且在打印fsm的大小时,它认为有限状态机是空的.这必须是因为e没有持续维护,但我不明白为什么.
我尝试过转换self.e为self.__class__.e(与timestamp相同)以及只保持e和timestamp全局和调用TestFSM.e以及TestFSM.timestamp持续更改.这些解决方案都列在其他问题中,它们都产生了一个关键错误.我尝试过设置setUpClass(),但仍然产生了一个关键错误.
我怎样才能坚持e和时间戳?
测试方法按字母顺序执行.所以 test_active_start_conversion以前执行过test_add_convert_processes.
但是,实际上您的测试应该是独立的 - 您应该在实际setUp方法中进行设置,而不是在测试方法中进行.
| 归档时间: |
|
| 查看次数: |
268 次 |
| 最近记录: |