使用 Python 在 CANoe 中运行测试模块
Posted
技术标签:
【中文标题】使用 Python 在 CANoe 中运行测试模块【英文标题】:Running Test Modules in CANoe Using Python 【发布时间】:2020-09-12 00:56:25 【问题描述】:我开发了一个基于 Python 的 CANOE 自动化代码,其中我启动 CANOE,打开配置并加载测试规范并运行它。
现在我想等到测试模块执行完成来记录判决。但不确定如何去做。任何帮助将不胜感激。
"""Execute XML Test Cases without a pass verdict"""
from time import sleep
import win32com.client as win32
import configparser
import time
from pandas.core.computation.expressions import set_test_mode
import pythoncom
testComplete = False
class TestModuleEvents(object):
def OnReportGenerated(self,Success, SourceFullName, GeneratedFullName):
print("Report Generated")
testComplete = True
def OnStop(self, value):
print("Test Module Stopped")
testComplete = True
def OnStart(self):
print("Test Module Started")
testComplete = True
class TestConfigurationEvents(object):
def OnStart(self):
print("Measurement Started")
testComplete = False
def OnStop(self):
print("Measurement Stopped")
testComplete = True
config = configparser.RawConfigParser()
config.read('usecase02_configuration.properties')
configurationPath = config.get('TESTCONFIGURATION', 'configurationpath')
testspec = config.get('TESTCONFIGURATION', 'testspecification')
CANoe = win32.DispatchEx("CANoe.Application")
CANoe.Open(configurationPath)
testSetup = CANoe.Configuration.TestSetup
testSetup.TestEnvironments.Add(testspec)
test_env = testSetup.TestEnvironments.Item('Test Environment')
test_env = win32.CastTo(test_env, "ITestEnvironment2")
print(report.FullName)
# Get the XML TestModule (type <TSTestModule>) in the test setup
test_module = test_env.TestModules.Item('Tester')
CANoe.Measurement.Start()
sleep(5) # Sleep because measurement start is not instantaneous
win32.WithEvents(test_module, TestModuleEvents)
test_module.Start()
# sleep(60)
while test_module.Verdict==0:
time.sleep(1)
# test_module.Stop()
print(test_module.Verdict)
【问题讨论】:
您的问题到底是什么?您已经有了启动测试模块的代码以及等待测试模块完成的代码。那么你的问题到底是什么? 感谢您查看代码。如果您查看 while 循环while test_module.Verdict==0: time.sleep(1)
我假设默认情况下的判定为 0,通过或失败将变为 1 或 2。但它并没有在 while 中等待,因为默认情况下判定为 1。然后我尝试更改 while 如下所示while not testComplete: time.sleep(1)
希望当 testmodule 运行完成时,它会在 TestModuleEvents 中调用 onStop 并设置 testCompleted=true。但这也没有发生。在调用 onStop 和终止 while 时需要支持。
【参考方案1】:
你已经准备好了所有的部分。我认为唯一的问题是对全局变量在 python 中的工作方式的误解。
您在 python 文件的全局范围内声明 testComplete
。在TestModuleEvents.OnStop
内部,您声明了另一个名为testComplete
的变量。此实例与全局范围内的变量完全无关。
将您的 OnStop
-handler(以及其他人)更改为如下内容:
def OnStop(self, value):
global testComplete
print("Test Module Stopped")
testComplete = True
这会将全局变量导入到您的作用域中,并将其设置为True
,而不是创建一个新变量。
完成之后,将你的 while 循环更改为:
while not testComplete:
time.sleep(1)
【讨论】:
以上是关于使用 Python 在 CANoe 中运行测试模块的主要内容,如果未能解决你的问题,请参考以下文章
在 CANoe 中使用 Visual Studio 中的 .NET 设置信号值
从零开始学习CANoe(二十)—— Python和CANoe的数据交互