參考文獻:
1、CANoe幫助文檔 Sample Configurations 11.0.42\Programming\Python
2、https://blog.csdn.net/m0_47334080/article/details/107862527
3、https://www.cnpython.com/pypi/python-canoe
RunAllTests.py
# ----------------------------------------------------------------------------- # Example: Test Feature Set via Python # # This sample demonstrates how to start the test modules and test # configurations via COM API using a Python script. # The script uses the included PythonBasicEmpty.cfg configuration but is # working also with any other CANoe configuration containing test modules # and test configurations. # # Limitations: # - only the first test environment is supported. If the configuration # contains more than one test environment, the other test environments # are ignored # - the script does not wait for test reports to be finished. If the test # reports are enabled, they may run in the background after the test is # finished # ----------------------------------------------------------------------------- # Copyright (c) 2017 by Vector Informatik GmbH. All rights reserved. # ----------------------------------------------------------------------------- import time, os, msvcrt from win32com.client import * from win32com.client.connect import * import subprocess # ----------------------------------------------------------------------------- # 線程等待處理 # 如果條件為假(con = False),則程序一直等待掛起; # 如果條件為真(con = True),則程序跳出; # ----------------------------------------------------------------------------- def DoEvents(): # 在當前線程下彈出所有等待消息 pythoncom.PumpWaitingMessages() # 延時0.1秒,線程掛起 time.sleep(.1) def DoEventsUntil(cond): while not cond(): DoEvents() # ----------------------------------------------------------------------------- # 定義CANoe應用 # ----------------------------------------------------------------------------- class CanoeSync(object): """Wrapper class for CANoe Application object""" #定義類變量 Started = False Stopped = False ConfigPath = "" # ------------------------------------------------------------------------- # 構造方法,Self代表類的示例 # ------------------------------------------------------------------------- def __init__(self): # 創造CANoe應用對象實例 app = DispatchEx('CANoe.Application') # CANoe工程加載或新建時不能被修改 app.Configuration.Modified = False # CANoe版本 ver = app.Version print('Loaded CANoe version ', ver.major, '.', ver.minor, '.', ver.Build, '...', sep='') self.App = app # 獲取CANoe測量配置 self.Measurement = app.Measurement # 匿名函數,獲取CANoe的運行狀態 self.Running = lambda: self.Measurement.Running # 匿名函數,等待CANoe啟動 self.WaitForStart = lambda: DoEventsUntil(lambda: CanoeSync.Started) # 匿名函數,等待CANoe停止 self.WaitForStop = lambda: DoEventsUntil(lambda: CanoeSync.Stopped) # 定義Measurement時間 WithEvents(self.App.Measurement, CanoeMeasurementEvents) # 獲取CANoe環境變量 self.Environment = app.Environment # ------------------------------------------------------------------------- # 加載CANoe工程,cfgPath - 相對路徑 # ------------------------------------------------------------------------- def Load(self, cfgPath): # current dir must point to the script file # 把當前工程根目錄和給定子目錄結合起來 cfg = os.path.join(os.curdir, cfgPath) # 找到CANoe的絕對路徑 cfg = os.path.abspath(cfg) print('Opening: ', cfg) # 返回CANoe工程的路徑名稱 self.ConfigPath = os.path.dirname(cfg) # 定義CANoe仿真配置 self.Configuration = self.App.Configuration # 打開CANoe工程 self.App.Open(cfg) # ------------------------------------------------------------------------- # 加載TestModules測試配置 # ------------------------------------------------------------------------- def LoadTestSetup(self, testsetup): # 返回測試配置 self.TestSetup = self.App.Configuration.TestSetup # 把當前工程根目錄和給定子目錄結合起來 path = os.path.join(self.ConfigPath, testsetup) # 把.tes測試環境TestEnvironments加入TestSetup testenv = self.TestSetup.TestEnvironments.Add(path) # ITestEnvironment2屬於Vector自定義強制類型 testenv = CastTo(testenv, "ITestEnvironment2") # TestModules property to access the test modules # 創建TestModules列表 self.TestModules = [] # Test Module添加到.tes中,List末尾添加新對象 self.TraverseTestItem(testenv, lambda tm: self.TestModules.append(CanoeTestModule(tm))) # ------------------------------------------------------------------------- # 加載vTEST測試配置 # ------------------------------------------------------------------------- def LoadTestConfiguration(self, testcfgname, testunits): """ Adds a test configuration and initialize it with a list of existing test units """ # 添加Test Configurations到CANoe工程 tc = self.App.Configuration.TestConfigurations.Add() # Test Configurations名稱 tc.Name = testcfgname # ITestUnits2屬於Vector自定義強制類型 tus = CastTo(tc.TestUnits, "ITestUnits2") # 遍歷所有Test Unit,加入Test Configurations for tu in testunits: tus.Add(tu) # TestConfigs property to access the test configuration self.TestConfigs = [CanoeTestConfiguration(tc)] def Start(self): if not self.Running(): # 如果CANoe未啟動,則啟動CANoe self.Measurement.Start() # 等待CANoe啟動 self.WaitForStart() def Stop(self): if self.Running(): # 如果CANoe已運行,則停止運行CANoe self.Measurement.Stop() # 等待停止運行CANoe self.WaitForStop() def RunTestModules(self): """ starts all test modules and waits for all of them to finish""" # start all test modules for tm in self.TestModules: tm.Start() # wait for test modules to stop # 如果當前CANoe工程中所有test modules在運行中或未激活,則程序等待 while not all([not tm.Enabled or tm.IsDone() for tm in app.TestModules]): DoEvents() def RunTestConfigs(self): """ starts all test configurations and waits for all of them to finish""" # start all test configurations for tc in self.TestConfigs: tc.Start() # wait for test modules to stop # 如果當前CANoe工程中所有test configurations在運行中或未激活,則程序等待 while not all([not tc.Enabled or tc.IsDone() for tc in app.TestConfigs]): DoEvents() # ------------------------------------------------------------------------- # 循環遍歷.tse里Test Modules和子文件 # ------------------------------------------------------------------------- def TraverseTestItem(self, parent, testf): # 遍歷.tse中所有Test Modules for test in parent.TestModules: # 添加到Test Modules集合中 testf(test) # 在.tse文件夾中遍歷子文件 for folder in parent.Folders: # 添加到Test Modules集合中 found = self.TraverseTestItem(folder, testf) class CanoeMeasurementEvents(object): """Handler for CANoe measurement events""" def OnStart(self): CanoeSync.Started = True CanoeSync.Stopped = False print("< measurement started >") def OnStop(self): CanoeSync.Started = False CanoeSync.Stopped = True print("< measurement stopped >") class CanoeTestModule: """Wrapper class for CANoe TestModule object""" def __init__(self, tm): self.tm = tm self.Events = DispatchWithEvents(tm, CanoeTestEvents) self.Name = tm.Name self.IsDone = lambda: self.Events.stopped self.Enabled = tm.Enabled def Start(self): if self.tm.Enabled: self.tm.Start() self.Events.WaitForStart() class CanoeTestConfiguration: """Wrapper class for a CANoe Test Configuration object""" def __init__(self, tc): self.tc = tc self.Name = tc.Name self.Events = DispatchWithEvents(tc, CanoeTestEvents) self.IsDone = lambda: self.Events.stopped self.Enabled = tc.Enabled def Start(self): if self.tc.Enabled: self.tc.Start() self.Events.WaitForStart() class CanoeTestEvents: """Utility class to handle the test events""" def __init__(self): self.started = False self.stopped = False self.WaitForStart = lambda: DoEventsUntil(lambda: self.started) self.WaitForStop = lambda: DoEventsUntil(lambda: self.stopped) def OnStart(self): self.started = True self.stopped = False print("<", self.Name, " started >") def OnStop(self, reason): self.started = False self.stopped = True print("<", self.Name, " stopped >") # ----------------------------------------------------------------------------- # main # ----------------------------------------------------------------------------- app = CanoeSync() # loads the sample configuration app.Load('CANoePro\CANoePro_autotest.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 script # 使用subprocess方法,執行adb命令 subprocess.run("adb shell am start -n com.zinthewind.air-consetting/.MainActivity", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) # 延時1秒 time.sleep(1) subprocess.run("adb shell input tap 778 438", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) time.sleep(1) # 獲取CANoe工程中Environment的值 ACFunctionReqState = app.Environment.GetVariable("Env_ACFunctionReq") print("Env_ACFunctionReq = ",ACFunctionReqState.Value) time.sleep(1) NM_BUS_State = app.Environment.GetVariable("Env_NM_BUS_State") print("Env_NM_BUS_State = ", NM_BUS_State) # bus sleep = 1 # 設置CANoe工程中Environment的值 NM_BUS_State.Value = 1 time.sleep(1) # 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()