gpt4 book ai didi

python - Robot 框架中是否有一种方法可以仅在存在 True 时才记录 if 关键字?

转载 作者:行者123 更新时间:2023-12-04 08:58:12 26 4
gpt4 key购买 nike

我知道 RF 中没有 switch 语句。我确实有 50 个 if 关键字(我使用它是因为不存在开关)。
我的日志文件很长,因为每 50 条 if 语句都会记录一次(即使是那些不正确的语句)。
我想知道是否有办法只记录正确的陈述?
这是我的代码的编写方式(有 50 个这样的关键字):

# Access Apply ImportExportParams
\ Run Keyword If '${Type}' == 'ImportExportParams' and '${DealId}' != 'None' and '${ScenarioId}' != 'None' Call_API_ImportExportParams ${DealId} ${ScenarioId} ${ProductId}
# Access bulk apply cheapest quote
\ Run Keyword If '${Type}' == 'BulkApplyCheapest' and '${DealId}' != 'None' and '${ScenarioId}' != 'None' Call_API_BulkApplyCheapest ${DealId} ${ScenarioId} ${ProductId}
# SiteSelection
\ Run Keyword If '${Type}' == 'SiteSelection' and '${DealId}' != 'None' and '${ScenarioId}' != 'None' Call_API_SiteSelection ${ProductId} ${DealId} ${ScenarioId} ${Name}
# SiteSelectionFile
\ Run Keyword If '${Type}' == 'SiteSelectionFile' and '${DealId}' != 'None' and '${ScenarioId}' != 'None' Call_API_SiteSelectionFile ${ProductId} ${DealId} ${ScenarioId}
\ Run Keyword If '${Type}' == 'SiteSelectionFile2' and '${DealId}' != 'None' and '${ScenarioId}' != 'None' Call_API_SiteSelectionFile2 ${ProductId} ${DealId} ${ScenarioId}
# SiteSelectionMultiple
\ Run Keyword If '${Type}' == 'SiteSelectionMultiple' and '${DealId}' != 'None' and '${ScenarioId}' != 'None' Call_API_SiteSelectionMultiple ${ProductId} ${DealId} ${ScenarioId}
here is a screenshot of a part of my actual log
感谢您的帮助 :)

最佳答案

我不知道框架中有这样的功能,但我可以想到一个替代方案。你可以写一个小library这是 recommended由用户指南针对此类情况。

In general, it is not recommended to have conditional logic in test cases, or even in user keywords, because it can make them hard to understand and maintain. Instead, this kind of logic should be in test libraries, where it can be implemented using natural programming language constructs. However, some conditional logic can be useful at times, and even though Robot Framework does not have an actual if/else construct, there are several ways to get the same effect.


这是一个让您入门的示例。
库代码(lib.py):
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
test.robot 中的用法(执行 robot test.robot ,Python 和机器人文件应该在同一个目录下。
enter image description here
还有 --pythonpath如果库必须位于另一个位置,则可以使用参数或者可以使用库的路径更新 PYTHONPATH 环境变量。):
*** 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
这是您只能看到已执行分支的输出。
enter image description here

关于python - Robot 框架中是否有一种方法可以仅在存在 True 时才记录 if 关键字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63702650/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com