尝试在机器人框架中创建测试用例。
我们有几个 rest api 返回给我们 Json 作为结果。为了调用这种情况,我们在 rest2.py 中使用了以下代码
def restJSON():
r = requests.get("http://httpbin.org/get")
# print "Code" , r.status_code
# print "Text " , r.text
return r.json()
我们将 json 存储在文件输出中。我们已经编写了机器人测试用例来评估 json 比较,如下所示
*** Settings ***
Library rest2.py
Library OperatingSystem
*** Test Cases ***
Example that calls a python keyword
${result}= restJSON
${json}= Get file output
Should be equal ${result} ${json}
但是当我们运行 pybot 测试用例时,我们收到错误消息,指出两个 json 不相同。
--------------------------------
pybot testSuite.txt
--------------------------------
==============================================================================
testSuite
==============================================================================
Example that calls a python keyword | FAIL |
{u'origin': u'10.252.30.94, 69.241.25.16', u'headers': {u'Via': u'1.1 localhost (squid/3.1.14)', u'Accept-Encoding': u'gzip, deflate, compress', u'Accept': u'*/*', u'User-Agent': u'python-requests/2.2.1 CPython/2.7.6 Linux/3.16.0-30-generic', u'Host': u'httpbin.org', u'Cache-Control': u'max-age=259200'}, u'args': {}, u'url': u'http://httpbin.org/get'} != {u'origin': u'10.252.30.94, 69.241.25.16', u'headers': {u'Via': u'1.1 localhost (squid/3.1.14)', u'Accept-Encoding': u'gzip, deflate, compress', u'Accept': u'*/*', u'User-Agent': u'python-requests/2.2.1 CPython/2.7.6 Linux/3.16.0-30-generic', u'Host': u'httpbin.org', u'Cache-Control': u'max-age=259200'}, u'args': {}, u'url': u'http://httpbin.org/get'}
------------------------------------------------------------------------------
testSuite | FAIL |
1 critical test, 0 passed, 1 failed
1 test total, 0 passed, 1 failed
==============================================================================
json 文件相同。但它的失败测试用例仍然说两者不一样。这是比较的正确方法吗,或者我们是否有任何其他方法可以在机器人框架中进行比较。
最佳答案
为什么不使用已经存在的库:
示例代码:
*** Settings ***
Library RequestsLibrary
Library Collections
Library XML use_lxml=True
Force Tags REST
*** Variables ***
${SERVICE_ROOT} http://ip.jsontest.com/
${SERVICE_NAME} testing
*** Keywords ***
json_property_should_equal
[Arguments] ${json} ${property} ${value_expected}
${value_found} = Get From Dictionary ${json} ${property}
${error_message} = Catenate SEPARATOR= Expected value for property " ${property} " was " ${value_expected} " but found " ${value_found} "
Should Be Equal As Strings ${value_found} ${value_expected} ${error_message} values=false
*** Test Cases ***
Example REST JSON
Create session ${SERVICE_NAME} ${SERVICE_ROOT}
${headers}= Create Dictionary Content-Type=application/json Accept=application/json
${result}= Get Request ${SERVICE_NAME} ${SERVICE_ROOT} headers=${headers}
Should Be Equal ${result.status_code} ${200}
Log ${result.content}
${json}= To Json ${result.content}
${pp}= To Json ${result.content} pretty_print=True
Log ${pp}
json_property_should_equal ${json} ip [Your_IP]
记得安装所有需要的库:
pip install robotframework-requests
pip install requests
关于python - 比较机器人框架中函数的 Json 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36916304/