【CANOE范例程序讲解】--- Python调用CANOE运行
1. 概述
本期讲解的是Python调用CANOE的范例程序。
工程路径:
C:\Users\Public\Documents\Vector\CANoe\Sample Configurations 14.0.83\Programming\Python
2. 调用演示
打开工程路径文件夹显示如下。
在当前文件夹下打开cmd窗口。

在窗口中输入python RunAllTests.py。

按回车键运行。在cmd窗口中可以看到测试用例开始执行和执行完成的打印。

在cmd调用python脚本运行期间,canoe窗口自动打开,并自动运行测试用例脚本。执行如下所示。执行了TestModule和TestUnit两种类型的测试脚本。TestModule是canoe自带的测试脚本开发方式;TestUnit是使用vTESTstudio开发的测试脚本,可以在canoe中调用运行。

3. 程序讲解
3.1 类定义
程序中主要定义了5个类,分别CanoeSync、CanoeMeasurementEvents、CanoeTestModule、CanoeTestConfiguration、CanoeTestEvents。逐个讲解下关键代码。
CanoeSync类
主要建立与canoe的通信,执行打开canoe工程、执行测试用例等操作。
def __init__(self): 类初始化函数self.Running、self.WaitForStart、self.WaitForStop:创建了3个匿名函数,用于监测canoe 运行状态、开始和停止;WithEvents(self.App.Measurement, CanoeMeasurementEvents):将canoe的Measurement 属性与回调函数 CanoeMeasurementEvents 绑定。这个回调函数主要用于赋值运行的变量和状态打印。
def Load(self, cfgPath): 打开指定路径canoe工程COM调用链:Application->Open()
def LoadTestSetup(self, testsetup): 在canoe工程中添加 Test Environment,后缀为tseTraverseTestItem 函数:将添加的Test Environment 中所有 Test Module 提取到一个列表里,其中可能有嵌套文件夹的,这个函数可以对文件里的嵌套Module都遍历出来。
def LoadTestConfiguration(self, testcfgname, testunits): 在canoe工程中添加单个 Test Configuration,并将输入的 测试单元添加到这个Configuration中,测试单元后缀为vtuexe
def Start(self)、def Stop(self): 控制canoe工程的启停COM调用链:Application->Measurement->Start()COM调用链:Application->Measurement->Stop()WaitForStart() : 在初始化中定义的匿名函数,函数会阻塞等待,等待canoe工程启动完成后,继续向后运行WaitForStop() : 在初始化中定义的匿名函数,函数会阻塞等待,等待canoe工程停止完成后,继续向后运行启动完成和停止完成的状态值由“CanoeMeasurementEvents”类中的回调函数来配置。
def RunTestModules(self)、def RunTestConfigs(self): 控制测试模块、测试单元的启动控制所有测试模块、测试单元都运行,然后100ms周期检测,所有模块运行完成后再继续执行后续操作。
def TraverseTestItem(self, parent, testf): 遍历获取Test Environment中的所有Test Module
CanoeMeasurementEvents类
作为 CanoeSync 类中的 Measurement 属性的回调。
def OnStart(self)、def OnStop(self) : Measurement的COM回调方法,检测到canoe运行和停止时会自动调用这两个函数。COM调用链:Application->Measurement->OnStartCOM调用链:Application->Measurement->OnStop
这里的 CanoeSync 是直接调用的类名然后调用它的属性,是合法的。因为CanoeSync的属性 Started 和 Stopped 都是类级别的(定义在类中而非实例中),可直接通过类名访问。
CanoeTestModule类
封装 Test Module 对象,绑定回调函数。
DispatchWithEvents 将Test Module 与 CanoeTestEvents 事件类绑定。在测试用例开始和停止运行时触发。
CanoeTestConfiguration类
封装 Test Configuration 对象, 绑定回调函数。绑定同CanoeTestModule类。
CanoeTestEvents类
创建 Test Module 和 Test Configuration 对象的回调事件。
def OnStart(self)、def OnStop(self) : TSTestModule 和 TestConfiguration 的COM回调方法,检测到测试用例运行和停止时会自动调用这两个函数。COM调用链:Application->Configuration->TestSetup->TestEnvironments->TestEnvironment->TestModules->TSTestModule->OnStartCOM调用链:Application->Configuration->TestSetup->TestEnvironments->TestEnvironment->TestModules->TSTestModule->OnStopCOM调用链:Application->Configuration->TestConfigurations->TestConfiguration->OnStartCOM调用链:Application->Configuration->TestConfigurations->TestConfiguration->OnStop
3.2 调用序列
定义完成类之后,开始实际的调用canoe步骤。
| 1 | 实例化了一个CanoeSync对象 | app = CanoeSync() |
| 2 | CanoeSync对象加载cfg工程 | app.Load... |
| 3 | 在cfg工程中添加tse | app.LoadTestSetup... |
| 4 | 在cfg工程中添加vtuexe | app.LoadTestConfiguration... |
| 5 | 启动canoe | app.Start() |
| 6 | 运行所有TestModule | app.RunTestModules() |
| 7 | 运行所有TestConfiguration | app.RunTestConfigs() |
| 8 | 运行完成等待键盘操作 | while not msvcrt.kbhit()... |
| 9 | 停止canoe | app.Stop() |
原始序列代码如下:
# ----------------------------------------------------------------------------- # main # ----------------------------------------------------------------------------- app = CanoeSync() # loads the sample configuration app.Load('CANoeConfig\PythonBasicEmpty.cfg') # add test modules to the configuration app.LoadTestSetup('TestEnvironments\Test Environment.tse') # add a test configuration and a list of test units app.LoadTestConfiguration('TestConfiguration', ['TestConfiguration\EasyTest\EasyTest.vtuexe']) # start the measurement app.Start() # runs the test modules app.RunTestModules() # runs the test configurations app.RunTestConfigs() # wait for a keypress to end the program print("Press any key to exit ...") while not msvcrt.kbhit(): DoEvents() # stops the measurement app.Stop()4. 总结
以上就是 vector 官方的 python 调用 canoe 范例的代码讲解,对于我们实际项目应用中可以对这些范例代码进行借鉴,比如实际中的 canoe 工程不需要通过代码添加测试用例,那么就可以跳过用例添加的步骤,取而代之的是识别当前已有的测试用例,然后直接执行。具体的用法大家可以根据实际情况来调整,欢迎评论区讨论。