尝试在机器人框架中创建测试用例。
我们有几个休息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文件是相同的。但它的失败测试案例仍然不相同。这是比较的正确方法,还是我们有任何其他方法在机器人框架中执行此操作。
答案 0 :(得分:3)
为什么不使用现有的库:
示例代码:
*** 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
答案 1 :(得分:2)
执行此操作时的响应是什么:
Should Be Equal As Strings ${result} ${json}
我发现你实现你想要的有点奇怪的方法,如果我是你,我将使用http库或请求库
https://github.com/peritus/robotframework-httplibrary https://github.com/bulkan/robotframework-requests
答案 2 :(得分:1)
Get File
读取文件的内容并返回一个字符串;同时python函数返回一个dict对象。所以看起来你正在将字典与字符串进行比较 - 而且它们根本无法返回相同的数字。
如果将文件输出转换为dict,则检查很可能会通过:
# your other code
${json_file}= Get file output
${json_file}= Evaluate json.loads("""${json_file}""") json # use the json module to transform str->dict
Should Be Equal ${result} ${json_file}
# even better - more verbose logging if values differ, or there are missing keys:
Dictionaries Should Be Equal ${result} ${json_file}
答案 3 :(得分:0)
工作示例
将其放入.robot文件
*** Settings ***
Library jsonlib.py
*** Variables ***
${response_json} {"key1": 33, "key2": "string", "key3": [1,2,3,"sring"], "name": "hans"}
${expected_json} {"key1": 77, "key2": "string", "key3": [1,2,3,"sring"], "name": "OTTO", "age": 33}
*** Test Cases ***
response meets expectation
&{json1}= Evaluate json.loads('''${response_json}''') json
&{json2}= Evaluate json.loads('''${expected_json}''') json
Log Dictionary ${json1}
Log Dictionary ${json2}
# ${type}= evaluate type(${json1}).__name__
# ${type}= evaluate type(&{json2})
response_meets_expectation ${json1} ${json2}
compare json payloads
&{json1}= Evaluate json.loads('''${response_json}''') json
&{json2}= Evaluate json.loads('''${expected_json}''') json
compare_json_payloads ${json1} ${json2}
json payloads should match
&{json1}= Evaluate json.loads('''${response_json}''') json
&{json2}= Evaluate json.loads('''${expected_json}''') json
json_paylodas_should_match ${json1} ${json2}
将其放在.robot文件所在的文件夹中的jsonlib.py中
# -*- coding: utf-8 -*-
import json
import jsondiff
from deepdiff import DeepDiff
# from deepdiff import DeepSearch, grep
from pprint import pprint
from robot.api import logger
class JsonCompareError(Exception):
pass
def response_meets_expectation(response, expectation):
union = jsondiff.patch(response, expectation)
difference = jsondiff.diff(response, union)
if difference:
raise JsonCompareError("Diff found: {}".format(difference))
else:
return True
def compare_json_payloads(response_json, expected_json):
logger.debug("Compare JSON payloads")
diff = DeepDiff(response_json, expected_json, ignore_order=True, verbose_level=2)
logger.debug("Values changed: {}".format(diff['values_changed']))
return diff.to_dict()
def json_paylodas_should_match(response_json, expected_json):
diff = DeepDiff(response_json, expected_json, verbose_level=2)
if diff != {}:
raise JsonCompareError("Payloads do not match! Differences: {}".format(diff))
else:
return True
答案 4 :(得分:0)
使用https://github.com/Accruent/robotframework-zoomba类似于:
Library Zoomba.APILibrary
*** Test Cases ***
Example Test
${headers}= Create Dictionary Content-Type=application/json Accept=application/json
${actual_response}= Call Get Request ${headers} http://httpbin.org/get
Validate Response Contains Expected Response ${actual_response} ${expected_response}
这将逐个密钥验证结果,失败的内容将如下所示:
Key(s) Did Not Match:
------------------
Key: pear
Expected: fish
Actual: bird
------------------
Full List Breakdown:
Expected: [{'apple': 'cat', 'banana': 'dog', 'pear': 'fish'}, {'apple': 'cat', 'banana': 'mice', 'pear': 'bird'}, {'apple': 'dog', 'banana': 'mice', 'pear': 'cat'}]
Actual: [{'apple': 'cat', 'banana': 'dog', 'pear': 'bird'}]
Please see differing value(s)"
此处的文档:https://accruent.github.io/robotframework-zoomba/docs/APILibraryDocumentation.html