簡體   English   中英

使用 Python 在 CANoe 中運行測試模塊

[英]Running Test Modules in CANoe Using Python

我開發了一個基於 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)

你已經准備好了所有的東西。 我認為唯一的問題是對全局變量在 python 中的工作方式的誤解。

您在 python 文件的全局 scope 中聲明testComplete TestModuleEvents.OnStop中,您聲明了另一個名為testComplete的變量。 此實例與全局 scope 中的變量完全無關。

將您的OnStop -處理程序(以及其他處理程序)更改為以下內容:

    def OnStop(self, value):
        global testComplete
        print("Test Module Stopped")
        testComplete = True

這會將全局變量導入您的 scope 並將該變量設置為True而不是創建新變量。

之后,將您的 while 循環更改為:

while not testComplete:
    time.sleep(1)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM