如何通过 Python 运行 CANoe 测试模块?

问题描述

我希望能够在我的 CANoe 脚本中启动一个测试模块;如果我在没有 Python 的情况下开始一切,它会很好地工作。

现在,我的 Python 代码可以加载 CANoe,打开所需的配置文件,并开始模拟。 我尝试了文档和此处示例中的一些代码,但它不起作用...

entity.setRoles(user.getRoles());

并运行:

# --------------------------------------------------------------------------
# Standard library imports
import os
import sys
import subprocess
# import win32com.client
import time
import threading
from win32com.client import *
from win32com.client.connect import *

my_xml_ts = r"C:\_my_path\boaz_test2.xml"

# Vector Canoe Class
class CANoe:
    def __init__(self):
        self.application = None
        # check if there is any instance of CANoe process
        # output = subprocess.check_output('tasklist',shell=True)
        # if CANoe process is still available,kill the process
        # if "CANoe32.exe" in str(output):
        #     os.system("taskkill /im CANoe32.exe /f 2>nul >nul")

        # re-dispatch object for CANoe Application
        self.application = win32com.client.DispatchEx("CANoe.Application")
        self.ver = self.application.Version
        print('Loaded CANoe version ',self.ver.major,'.',self.ver.minor,self.ver.Build,'...')#,sep,''

        self.Measurement = self.application.Measurement.Running
        print(self.Measurement)


    def open_simulation(self,cfgname):
        # open CANoe simulation
        if (self.application != None):
            # check for valid file and it is *.cfg file
            if os.path.isfile(cfgname) and (os.path.splitext(cfgname)[1] == ".cfg"):
                self.application.Open(cfgname)
            else:
                raise RuntimeError("Can't find CANoe cfg file")
        else:
            raise RuntimeError("CANoe Application is missing,unable to open simulation")

    def set_testmodule(self):
        print("set_testmodule start")
        test_env = self.application.Configuration.TestSetup.TestEnvironments.Item("Test Environment")
        print("set_testmodule 1")
        test_env = win32com.client.CastTo(test_env,"ITestEnvironment2")
        print("set_testmodule 2")
        self.test_module = test_env.TestModules.Item("XML_Boaz_2")
        print("set_testmodule end",self.test_module)

    def test_module_run(self):
        print("test_module_run start")
        self.test_module.Start()
        print("test_module_run end")

        #     {.Sequence} property returns a collection of <TestCases> or <TestGroup>
        # or <TestSequenceItem> which is more generic
        seq = self.test_module.Sequence
        for i in range(1,seq.Count + 1):
            # Cast from <ITestSequenceItem> to <ITestCase> to access {.Verdict}
            # and the {.Enabled} property
            tc = win32com.client.CastTo(seq.Item(i),"ITestCase")
            if tc.Verdict != 1:  # Verdict 1 is pass
                tc.Enabled = True
                print(f"Enabling Test Case {tc.Ident} with verdict {tc.Verdict}")
            else:
                tc.Enabled = False
                print(f"Disabling Test Case {tc.Ident} since it has already passed")

    def close_simulation(self):
        # close CANoe simulation
        if (self.application != None):
            self.stop_Measurement()
            self.application.Quit()

        # make sure the CANoe is close properly,otherwise enforce taskkill
        output = subprocess.check_output('tasklist',shell=True)

        if "CANoe32.exe" in str(output):
            os.system("taskkill /im CANoe32.exe /f 2>nul >nul")

        self.application = None

    def start_Measurement(self):
        retry = 0
        retry_counter = 5
        # try to establish measurement within 20s timeout
        while not self.application.Measurement.Running and (retry < retry_counter):
            self.application.Measurement.Start()
            time.sleep(1)
            retry += 1
        if (retry == retry_counter):
            raise RuntimeWarning("CANoe start measuremet failed,Please Check Connection!")

这是我得到的错误代码:

import Python_CANoe
import time
import random

def try2():

    X = random.randrange(50)
    Y = random.randrange(100)
    print("start")

    CANoe = Python_CANoe.CANoe()


    CANoe.open_simulation(canoe_config_file)
    CANoe.set_testmodule()                    # Doesn't work

    print("CANoe script already Open")

    CANoe.start_Measurement()
    time.sleep(5)
    CANoe.test_module_run()                       # Doesn't work

if __name__ == "__main__":
    try2()

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)