简体   繁体   中英

Running Test Modules in CANoe Using Python

I developed a Python based CANOE Automation Code, wherein I launch CANOE, Open the Configuration and load the Test Specification and run it.

Now i want to wait till the Test Module execution is completed to note the Verdict. But not sure on how to do it. Any help would be appreciated.

"""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)

You have all pieces in place. I think the only problem is a misunderstanding of the way global variables work in python.

You declare testComplete in the global scope of your python file. Inside of TestModuleEvents.OnStop you declare another variable named testComplete . This instance is completely unrelated to the variable in the global scope.

Change your OnStop -handler (and the others as well) to something like this:

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

This will import the global variable into your scope and set that one to True instead of creating a new one.

After doing that, change your while loop to:

while not testComplete:
    time.sleep(1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM