如何让Robot Framework失败测试用例重复执行?登录多轮测试需求
解决重复执行错误登录测试用例的方法
方法一:用For循环直接在测试用例里重复执行
在单个测试用例中使用FOR循环遍历1到10的次数,每次执行登录命令并输出对应结果:
*** Test Cases *** Repeat Failed Login Attempts FOR ${attempt} IN RANGE 1 11 ${result}= Run special command -k --url https://${url} auth login ${username} -p${password} # 输出指定格式的错误信息 Log HTTP error 401 Unauthorized, Attempt ${attempt} INFO # 可选:断言返回结果包含401 Unauthorized,确保错误符合预期 Should Contain ${result} 401 Unauthorized END
注意:提前在*** Variables ***区块或测试套件中定义好${url}、${username}、${password}变量;同时确认Run special command关键字能正确返回命令执行的输出内容,参数格式(比如-p${password}是否需要空格)要匹配目标命令的要求。
方法二:使用Test Template(测试模板)
如果需要更规整的用例结构,用测试模板封装重复执行的步骤:
*** Test Cases *** Failed Login Attempt 1 Execute Failed Login 1 Failed Login Attempt 2 Execute Failed Login 2 ... Failed Login Attempt 10 Execute Failed Login 10 *** Keywords *** Execute Failed Login [Arguments] ${attempt_num} ${result}= Run special command -k --url https://${url} auth login ${username} -p${password} Log HTTP error 401 Unauthorized, Attempt ${attempt_num} INFO Should Contain ${result} 401 Unauthorized
也可以结合FOR循环简化模板调用(Robot Framework 3.1及以上版本支持):
*** Test Cases *** Repeat Failed Logins With Template FOR ${attempt} IN RANGE 1 11 Execute Failed Login ${attempt} END *** Keywords *** Execute Failed Login [Arguments] ${attempt_num} ${result}= Run special command -k --url https://${url} auth login ${username} -p${password} Log HTTP error 401 Unauthorized, Attempt ${attempt_num} INFO Should Contain ${result} 401 Unauthorized
你之前遇到的问题原因
- 复制用例后后续代码无法运行:Robot Framework不允许测试用例名称重复,重名会导致用例执行异常。
Run special command执行失败:大概率是变量未正确定义,或者命令参数格式不匹配目标工具的要求,可以先单独执行一次命令验证参数正确性,再放到循环逻辑中。
内容的提问来源于stack exchange,提问作者user5539903
相关产品推荐
相关产品推荐

