Python 在Robot框架中是否有一种方法可以记录keywork是否为True?
我知道RF中没有开关语句。我确实有50个if关键字(我使用这些关键字是因为不存在开关)。 我的日志文件很长,因为几乎每50条if语句都会被记录(即使是那些不正确的语句)。 我想知道是否有办法只记录真实的陈述 下面是我的代码的编写方式(有50个类似的关键字):Python 在Robot框架中是否有一种方法可以记录keywork是否为True?,python,logging,automated-tests,testng,robotframework,Python,Logging,Automated Tests,Testng,Robotframework,我知道RF中没有开关语句。我确实有50个if关键字(我使用这些关键字是因为不存在开关)。 我的日志文件很长,因为几乎每50条if语句都会被记录(即使是那些不正确的语句)。 我想知道是否有办法只记录真实的陈述 下面是我的代码的编写方式(有50个类似的关键字): 谢谢您的帮助:)我不知道框架中有这样的功能,但我可以想出一个替代方案。你可以写一个小的,这是由用户指南为这种情况 一般来说,不建议在测试用例中,甚至在用户关键字中使用条件逻辑,因为这会使它们难以理解和维护。相反,这种逻辑应该放在测试库中,
谢谢您的帮助:)我不知道框架中有这样的功能,但我可以想出一个替代方案。你可以写一个小的,这是由用户指南为这种情况 一般来说,不建议在测试用例中,甚至在用户关键字中使用条件逻辑,因为这会使它们难以理解和维护。相反,这种逻辑应该放在测试库中,在那里可以使用自然编程语言结构实现。然而,某些条件逻辑有时是有用的,即使Robot框架没有实际的if/else构造,也有几种方法可以获得相同的效果 下面是一个让您开始学习的示例 库代码(lib.py): test.robot(执行
robot test.robot
)中的用法,Python和robot文件应位于同一目录中。
此外,如果库必须位于另一个位置,则可以使用--pythonpath
参数,或者使用库的路径更新pythonpath env变量。):
这是您只能看到已执行分支的输出
您可能正在寻找
--removekeywords
和--flatteKeywords
命令行选项
有关更多详细信息,请查看
您的代码表明,所有这些条件都在\
for循环的较旧语法for
循环下循环。所以通过使用
robot——删除testuitefilename的关键字。robot
将生成如下屏幕截图所示的输出。在大多数情况下,通过的步骤并不需要,我认为这足以满足您的要求
FOR
-此模式从FOR循环中删除所有传递的迭代,最后一个除外
另一种可能性是以其他方式处理此问题,而不是寻找不在日志中记录假条件关键字的选项。比如说-
- 从代码中我可以看到
这种情况很常见。因此,与其全面检查,不如检查一次'${DealId}'!='“无”和“${ScenarioId}”!=”“无”
- 使用
&
或列表
中的字典
检查
变量值是否存在于集合中,而不是循环。并使用关键字获取该值${type}
- 请先检查此条件。
,然后使用变量作为关键字语法的一部分来调用特定关键字“${DealId}”!=”“无”和“${ScenarioId}”!=”“无”
*Test cases
Run Keyword If '${DealId}' != 'None' and '${ScenarioId}' !='None' Execute the type of Keyword SiteSelection
*Keywords
Execute the type of Keyword
[Arguments] ${type}
${Type_list}= Create List ImportExportParams BulkApplyCheapest SiteSelection SiteSelectionFile
... SiteSelectionFile2 SiteSelectionMultiple
${Status} ${index} Run Keyword And Ignore Error Get Index From List ${Type_list} ${type}
${Keyword_type} Get From List ${Type_list} ${index}
log ${Status} ${index}
Run Keyword Call_API_${Keyword_type}
输出
我认为,如果这些条目只包含它们内部的内容,那么它们不会删除
Run关键字。此外,如果
记录的不仅仅是条件被评估为False的情况,那么这些将删除每个Run关键字。虽然我可能错了,但您能提供一个示例,其中--removekeywords
或--flatteKeywords
被参数化吗?:)@BenceKaulics还没试过这个。我会试试看我是否能接近它。但这可能会变成一种解决办法,所以我分享了。即使使用--removekeywords all
我也可以在日志中看到所有运行关键字If
及其参数,只有它们下面的所有内容都被删除,并且使用--removekeywords选项删除了关键字数据。
可见。所以这与OP的图片中的结果是一样的。@BenceKaulics用我的发现更新了答案。几乎删除了If
和FOR
FOR循环和重构的。您好,谢谢您的建议。只要我在本地环境中工作,我就可以创建一个库。但我们是一个在服务器上工作的团队,我只能使用已经制作好的库,我的代码就是这样versatile@LouGyde所以基本上,你不允许在你的机器人文件旁边放一个小Python文件?这个库可以是你想要的通用库,我想,我的例子有点具体,因为它只是一个想法。@LouGyde我已经更新了答案,这样robot文件将直接引用Python库,因此无需使用--pythonpath
或更新pythonpath。@LouGyde是的,有一种方法,您可以在我的答案中检查它BuiltIn().run_关键字('Log Many','arg1-case1','arg2-case1')
您可以将关键字的名称作为字符串与其任何参数一起传递,并使用BuiltIn()运行它。run_关键字()
关键字,就像我使用Log Many
,Log
,无操作
和Log To Console
一样。
from robot.libraries.BuiltIn import BuiltIn
def switch(type):
type = int(type)
if type == 1:
BuiltIn().run_keyword('Log Many', 'arg1 - case1', 'arg2 - case1') # call any keyword like it was called from Robot Framework
elif type == 11:
BuiltIn().fail('Fail as it was 11') # or throw exception, that will fail the test execution as well
if type == 2:
BuiltIn().run_keyword('Log', 'arg1 - case2') # call any keyword like it was called from Robot Framework
elif type == 22:
BuiltIn().fail('Fail as it was 22') # or throw exception, that will fail the test execution as well
if type == 3:
BuiltIn().run_keyword('No operation') # call any keyword like it was called from Robot Framework
elif type == 33:
BuiltIn().fail('Fail as it was 33') # or throw exception, that will fail the test execution as well
if type == 4:
BuiltIn().run_keyword('Log To Console', 'arg1 - case4') # call any keyword like it was called from Robot Framework
elif type == 44:
BuiltIn().fail('Fail as it was 44') # or throw exception, that will fail the test execution as well
*** Settings ***
Library ${CURDIR}/lib.py
*** Test Cases ***
A test 1
switch 1
A test 2
switch 2
A test 3
switch 3
A test 4
switch 4
A test 5
switch 11
A test 6
switch 22
A test 7
switch 33
A test 8
switch 44
A test 9
switch 5
*Test cases
Run Keyword If '${DealId}' != 'None' and '${ScenarioId}' !='None' Execute the type of Keyword SiteSelection
*Keywords
Execute the type of Keyword
[Arguments] ${type}
${Type_list}= Create List ImportExportParams BulkApplyCheapest SiteSelection SiteSelectionFile
... SiteSelectionFile2 SiteSelectionMultiple
${Status} ${index} Run Keyword And Ignore Error Get Index From List ${Type_list} ${type}
${Keyword_type} Get From List ${Type_list} ${index}
log ${Status} ${index}
Run Keyword Call_API_${Keyword_type}