尝试在robot框架中创建测试用例。在
我们有几个restapi返回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存储在一个文件输出中。我们编写了robot测试用例来评估json比较,如下所示
^{pr2}$但是,当我们运行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文件是相同的。但它的失败测试案例表明两者并不相同。这是正确的比较方法吗,还是我们有其他方法可以在robot框架中进行比较。在
当你这样做时,会有什么反应:
我发现你实现你想要的东西的方法有点奇怪,如果我是你,我会使用http库或请求库
https://github.com/peritus/robotframework-httplibraryhttps://github.com/bulkan/robotframework-requests
Get File
读取文件内容并返回字符串;同时python函数返回dict对象。所以看起来你在比较dict和字符串-它们根本不能返回equal。在如果将文件输出转换为dict,则检查很可能通过:
为什么不使用已经存在的库:
示例代码:
记住安装所有需要的库:
^{pr2}$相关问题 更多 >
编程相关推荐