如何列出.robot文件中没有测试用例的所有关键字

2024-06-26 01:32:47 发布

您现在位置:Python中文网/ 问答频道 /正文

问题陈述: 我有一个.robot文件,其中包含很多关键字。这是一个更高级别的robot文件,不包含任何测试用例。 我想列出所有的关键字名称

到目前为止我试过什么

from robot.parsing.model import TestData
suite = TestData(parent=None,source="Track2_Keywords.robot")

这是错误的

raise NoTestsFound('File has no tests or tasks.') robot.parsing.populators.NoTestsFound: File has no tests or tasks.

我也试过:

from robot.parsing.model import KeywordTable
suite = KeywordTable("Track2_Keywords.robot")
for item in suite:
...     print (item.name)

但它是空的


Tags: 文件nofromimportmodelrobot关键字file
2条回答

首先,您需要使用ResourceFile模型,而不是TestDataKeywordTable。其次,必须调用populate方法以使关键字可见。实际上是populate方法读取文件并导入关键字

from robot.parsing.model import ResourceFile

rf = ResourceFile("Track2_Keywords.robot")
rf.populate()
for kw in rf.keywords:
    print(kw.name)

我不明白你的目标是什么。如果文件不包含测试,则它是一个资源文件。在这种情况下,您可以添加一个虚拟测试用例,例如:

*** Test Cases ***
Dummy Test
    No Operation

这样,您的代码就不应该抱怨缺少测试

如果您想在测试套件中查找未使用的关键字,可以在RIDE(https://github.com/robotframework/RIDE/)中使用该现有功能

相关问题 更多 >