(首先猜测,对机器人框架一无所知,但即将深入研究):您的机器人脚本正在$output 中寻找FAIL。您的输出包含 string not found cleanup is in progress (可能具有不同的格式,因为您已打印两次但未指定不打印换行符)。您的输出中没有 FAIL。也许你打算拥有
Run Keyword if 'string not found' in $output
在您的机器人脚本中,或
print("FAIL: string not found")
在您的验证码中?
更新:现在我已经花了一些时间在文档上(它们不会让学习做一些简单的事情变得容易),我可以说 Bence Kaulics 的答案大部分是正确的,可能取决于你的版本跑步。对我来说,由于try 没有except,他的答案中显示的测试以IndentationError 失败。这就是我所拥有的工作(例如,由于预期的原因而失败,主要是):
.机器人文件:
*** Settings ***
Documentation TC2
Library exception.py
*** Test Cases ***
TEST CASE TWO
validation
异常.py:
def validation():
try:
if 'loopback is up, line protocol is up' in open('1.txt').read():
result = 'string found'
else:
result = 'string not found'
raise RuntimeError(result)
except Exception as e:
result = 'string not found'
raise RuntimeError(result)
finally:
print(result)
cleanup()
def cleanup():
print("cleanup is in progress")
通过以上,我得到了输出:
(robottestframework) ~/s/l/robottestframework> robot -L TRACE keyword_driven.robot
==============================================================================
Keyword Driven :: TC2
==============================================================================
TEST CASE TWO | FAIL |
string not found
------------------------------------------------------------------------------
Keyword Driven :: TC2 | FAIL |
1 critical test, 0 passed, 1 failed
1 test total, 0 passed, 1 failed
==============================================================================
请注意,这会改变您正在测试的语义:您正在运行从库导入的关键字,而不是检查 stdout 的值,如果引发异常则失败。 IMO,这是比检查标准输出更好的方法。但是,如果您真正需要做的是检查 stdout 的值,您将需要更多类似的东西:
.机器人文件:
*** Settings ***
Documentation TC2
Library Process
Suite Teardown Terminate All Processes kill=True
*** Test Cases ***
TEST CASE TWO
${result} = Run Process python exception.py
should not contain ${result.stdout} string not found
exception.py 文件:
def validation():
result = ''
try:
if 'loopback is up, line protocol is up' in open('1.txt').read():
result = 'string found'
else:
result = 'string not found'
raise RuntimeError(result)
except Exception as e:
result = 'string not found'
finally:
print(result)
cleanup()
return result
def cleanup():
print("cleanup is in progress")
validation()
通过这些更改,我得到:
(robottestframework) ~/s/l/robottestframework> robot -L TRACE keyword_driven.robot
==============================================================================
Keyword Driven :: TC2
==============================================================================
TEST CASE TWO | FAIL |
'string not found
cleanup is in progress' contains 'string not found'
------------------------------------------------------------------------------
Keyword Driven :: TC2 | FAIL |
1 critical test, 0 passed, 1 failed
1 test total, 0 passed, 1 failed
==============================================================================