CANoe:如何使用CANoe COM接口从Python的XML测试模块中选择和启动测试用例?
CANoe:如何使用CANoe COM接口从Python的XML测试模块中选择和启动测试用例?,python,windows,com,canoe,Python,Windows,Com,Canoe,目前我可以: 开始使用独木舟 加载独木舟配置文件 加载测试设置文件 def load_test_setup(self, canoe_test_setup_file: str = None) -> None: logger.info( f'Loading CANoe test setup file <{canoe_test_setup_file}>.') if self.measurement.Running: logger.i
目前我可以:
- 开始使用独木舟
- 加载独木舟配置文件
- 加载测试设置文件
def load_test_setup(self, canoe_test_setup_file: str = None) -> None: logger.info( f'Loading CANoe test setup file <{canoe_test_setup_file}>.') if self.measurement.Running: logger.info( f'Simulation is currently running, so new test setup could \ not be loaded!') return self.test_setup.TestEnvironments.Add(canoe_test_setup_file) test_environment = self.test_setup.TestEnvironments.Item(1) logger.info(f'Loaded test environment is <{test_environment.Name}>.')
def load\u test\u setup(self,canoe\u test\u setup\u file:str=None)->None: logger.info( f“加载独木舟测试设置文件”。) 如果自测量运行: logger.info( f'Simulation目前正在运行,因此新的测试设置可能\ 无法加载!) 返回 self.test\u setup.TestEnvironments.Add(canoe\u test\u setup\u文件) 测试环境=自身。测试设置。测试环境。项目(1) info(f'Loaded test environment is')
如何访问加载了测试设置(tse)文件的XML测试模块并选择要执行的测试?
继续您所做的工作 TestEnvironment包含TestModules。每个TestModule都包含一个TestSequence,该序列依次包含测试用例 请记住,您不能单独测试用例,而只能测试模块。但是您可以在执行之前使用COM-API启用和禁用单个测试用例 (在我的头顶上输入这个,可能无法100%工作)
您必须记住,TestSequence还可以包含其他TestSequence(即TestGroup)。这取决于TestModule的设置方式。如果是这样,您必须在循环中处理这些问题,并在搜索您感兴趣的测试用例时进入这些测试组。继续您所做的工作 TestEnvironment包含TestModules。每个TestModule都包含一个TestSequence,该序列依次包含测试用例 请记住,您不能单独测试用例,而只能测试模块。但是您可以在执行之前使用COM-API启用和禁用单个测试用例 (在我的头顶上输入这个,可能无法100%工作)
您必须记住,TestSequence还可以包含其他TestSequence(即TestGroup)。这取决于TestModule的设置方式。如果是这样的话,您必须在循环中处理这个问题,并在搜索您感兴趣的测试用例时进入这些测试组。代码段中的最后一行最有可能导致这个问题。 我已经尝试解决这个问题很长一段时间了,终于找到了解决方案 当您执行行
self.test\u setup.TestEnvironments.Item(1)
Win32 COM创建类型为TestSetupItem
的对象,该对象不具有访问测试用例所需的属性或方法。相反,我们希望访问集合类型为TestSetupFolders
或TestModules
的对象。win32com创建TestSetupItem
类型的对象,即使我在测试环境中有一个单独的XML测试模块(称为AutomationTestSeq)
我发现了三种可能的解决方案
win32com.client.DispatchWithEvents
或win32com.client.gencache.EnsureDispatch
生成一组描述CANoe对象模型的python文件
如果您以前使用过这两种方法之一,TestEnvironments.Item(1)
将始终返回TestSetupItem
,而不是更合适的类型对象
要删除缓存,需要删除C:\Users\{username}\AppData\Local\Temp\gen\u py\{python version}
文件夹
当然,每次这样做都不太实际
canoe=win32com.client.dynamic.Dispatch(“canoe.Application”)
从现在起,您使用canoe
创建的任何对象都将被动态调度
强制动态分派比每次手动清除缓存文件夹更容易。这总是给我带来好的结果。但这样做不会让您对对象有任何洞察。您将无法看到对象的可接受属性和方法
TestSetupItem
键入TestSetupFolders
或TestModules
win32.CastTo(test_env,“ITestEnvironment2”)
。这将确保您按照CANoe技术参考使用推荐的对象层次结构
请注意,您还必须将TestSequenceItem
类型转换为TestCase
,才能访问测试用例裁决并启用/禁用测试用例
下面是一个不错的示例脚本
"""Execute XML Test Cases without a pass verdict"""
import sys
from time import sleep
import win32com.client as win32
CANoe = win32.DispatchWithEvents("CANoe.Application")
CANoe.Open("canoe.cfg")
test_env = CANoe.Configuration.TestSetup.TestEnvironments.Item('Test Environment')
# Cast required since test_env is originally of type <ITestEnvironment>
test_env = win32.CastTo(test_env, "ITestEnvironment2")
# Get the XML TestModule (type <TSTestModule>) in the test setup
test_module = test_env.TestModules.Item('AutomationTestSeq')
# {.Sequence} property returns a collection of <TestCases> or <TestGroup>
# or <TestSequenceItem> which is more generic
seq = test_module.Sequence
for i in range(1, seq.Count+1):
# Cast from <ITestSequenceItem> to <ITestCase> to access {.Verdict}
# and the {.Enabled} property
tc = win32.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")
CANoe.Measurement.Start()
sleep(5) # Sleep because measurement start is not instantaneous
test_module.Start()
sleep(1)
“在没有通过判定的情况下执行XML测试用例”
导入系统
从时间上导入睡眠
将win32com.client作为win32导入
CANoe=win32.DispatchWithEvents(“CANoe.Application”)
CANoe.Open(“CANoe.cfg”)
test_env=CANoe.Configuration.TestSetup.TestEnvironments.Item('测试环境')
#由于test_env最初为类型,因此需要强制转换
test_env=win32.CastTo(test_env,“ITestEnvironment2”)
#在测试设置中获取XML TestModule(类型)
测试模块=测试环境测试模块项('AutomationTestSeq')
#{.Sequence}属性返回或的集合
#或者哪个更通用
seq=测试模块顺序
对于范围内的i(1,序号计数+1):
#从强制转换到访问{.Verdict}
#以及{.Enabled}属性
tc=win32.CastTo(序号第(i)项,“ITestCase”)
如果tc.裁决!=1:#判决一通过
tc.Enabled=True
打印(f“使用判决{tc.verdict}启用测试用例{tc.Ident}”)
其他:
tc.Enabled=False
打印(f“禁用测试用例{tc.Ident},因为它已经通过”)
独木舟。测量。开始()
睡眠(5)#睡眠,因为测量开始不是瞬时的
测试模块启动()
睡眠(1)
"""Execute XML Test Cases without a pass verdict"""
import sys
from time import sleep
import win32com.client as win32
CANoe = win32.DispatchWithEvents("CANoe.Application")
CANoe.Open("canoe.cfg")
test_env = CANoe.Configuration.TestSetup.TestEnvironments.Item('Test Environment')
# Cast required since test_env is originally of type <ITestEnvironment>
test_env = win32.CastTo(test_env, "ITestEnvironment2")
# Get the XML TestModule (type <TSTestModule>) in the test setup
test_module = test_env.TestModules.Item('AutomationTestSeq')
# {.Sequence} property returns a collection of <TestCases> or <TestGroup>
# or <TestSequenceItem> which is more generic
seq = test_module.Sequence
for i in range(1, seq.Count+1):
# Cast from <ITestSequenceItem> to <ITestCase> to access {.Verdict}
# and the {.Enabled} property
tc = win32.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")
CANoe.Measurement.Start()
sleep(5) # Sleep because measurement start is not instantaneous
test_module.Start()
sleep(1)