1. 概述
工程路径:
C:\Users\Public\Documents\Vector\CANoe\Sample Configurations 14.0.83\Programming\Python
Python 脚本通过 COM 接口调用 Vector CANoe 软件执行测试用例。文章展示了工程路径、CMD 运行方式及核心类定义(CanoeSync 等)。流程包括实例化对象、加载配置、启动测量、运行测试模块与单元、等待结束并停止。代码封装了测试环境遍历与事件回调机制,适用于自动化测试场景的参考借鉴。

工程路径:
C:\Users\Public\Documents\Vector\CANoe\Sample Configurations 14.0.83\Programming\Python
打开工程路径文件夹显示如下。
在当前文件夹下打开 cmd 窗口。

在窗口中输入 python RunAllTests.py。

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

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

程序中主要定义了 5 个类,分别 CanoeSync、CanoeMeasurementEvents、CanoeTestModule、CanoeTestConfiguration、CanoeTestEvents。逐个讲解下关键代码。
主要建立与 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
作为 CanoeSync 类中的 Measurement 属性的回调。
def OnStart(self)、def OnStop(self) : Measurement 的 COM 回调方法,检测到 canoe 运行和停止时会自动调用这两个函数。COM 调用链:Application->Measurement->OnStartCOM 调用链:Application->Measurement->OnStop
这里的 CanoeSync 是直接调用的类名然后调用它的属性,是合法的。因为 CanoeSync 的属性 Started 和 Stopped 都是类级别的(定义在类中而非实例中),可直接通过类名访问。
封装 Test Module 对象,绑定回调函数。
DispatchWithEvents 将 Test Module 与 CanoeTestEvents 事件类绑定。在测试用例开始和停止运行时触发。
封装 Test Configuration 对象,绑定回调函数。绑定同 CanoeTestModule 类。
创建 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
定义完成类之后,开始实际的调用 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()
以上就是 Vector 提供的 Python 调用 CANoe 范例的代码讲解,对于我们实际项目应用中可以对这些范例代码进行借鉴,比如实际中的 CANoe 工程不需要通过代码添加测试用例,那么就可以跳过用例添加的步骤,取而代之的是识别当前已有的测试用例,然后直接执行。具体的用法大家可以根据实际情况来调整。

微信公众号「极客日志」,在微信中扫描左侧二维码关注。展示文案:极客日志 zeeklog
解析常见 curl 参数并生成 fetch、axios、PHP curl 或 Python requests 示例代码。 在线工具,curl 转代码在线工具,online
将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online
将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online
将 Markdown(GFM)转为 HTML 片段,浏览器内 marked 解析;与 HTML转Markdown 互为补充。 在线工具,Markdown转HTML在线工具,online
将 HTML 片段转为 GitHub Flavored Markdown,支持标题、列表、链接、代码块与表格等;浏览器内处理,可链接预填。 在线工具,HTML转Markdown在线工具,online
通过删除不必要的空白来缩小和压缩JSON。 在线工具,JSON 压缩在线工具,online