接口测试 HttpRunner Hook 测试鉴权类型接口失败。

Sutune · 2018年08月04日 · 最后由 faywang666 回复于 2018年10月26日 · 3142 次阅读

问题描述

最近在学习社区大牛@debugtalkHttpRunner接口测试框架,主要是结合Httpbin的接口进行实践。
在对接口basic auth进行鉴权测试时,始终失败。

我的用例如下:
test_httpbin.yml

- config:
    name: httpbin api test
    request:
        base_url: http://www.httpbin.org/


- test:
    name: test basicauth
    request:
        url: /basic_auth/51zxw/8888
        method: GET
    validate:
        - eq: [status_code,200]
        - eq: [content.authenticated,true]
        - eq: [user,51zxw]
    setup_hooks:
        - ${hook_print(start test auth)}
        - ${set_hook_basic_auth($request)}

debugtalk.py 的 hook 方法定义如下:

from requests.auth import HTTPBasicAuth
def set_hook_basic_auth(request):
      request['auth']=HTTPBasicAuth('51zxw','8888')

def hook_print(msg):
    '''打印提示'''
    print(msg)

但是运行一直授权不成功,日志如下:

C:\Users\Shuqing>hrun D:\api_test\HttpRunner_test\test_httpbin.yml
test basicauth
start test auth
INFO     GET /basic_auth/51zxw/8888
ERROR    404 Client Error: NOT FOUND for url: http://www.httpbin.org/basic_auth/51zxw/8888
INFO     start to validate.
ERROR    validate: status_code equals 200(int)  ==> fail
404(int) equals 200(int)
ERROR    Failed to extract attribute from response body! => content.authenticated
response body: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>404 Not Found</title>
<h1>Not Found</h1>
<p>The requested URL was not found on the server.  If you entered the URL manually please check your spelling and try again.</p>


ERROR    request:
headers: {}
auth: <requests.auth.HTTPBasicAuth object at 0x000001EDE9F1B5C0>

ERROR    response:
status_code: 404
headers: {'Content-Type': 'text/html', 'Date': 'Sat, 04 Aug 2018 00:06:12 GMT', 'Connection': 'keep-alive', 'Content-Length': '233', 'Via': '1.1 vegur', 'Access-Control-Allow-Origin': '*', 'Server': 'gunicorn/19.9.0', 'Access-Control-Allow-Credentials': 'true'}
body: '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">\n<title>404 Not Found</title>\n<h1>Not Found</h1>\n<p>The requested URL was not found on the server.  If you entered the URL manually please check your spelling and try again.</p>\n'

F

======================================================================
FAIL: runTest (httprunner.task.TestCase)
test basicauth
----------------------------------------------------------------------
Traceback (most recent call last):
  File "c:\python35\lib\site-packages\httprunner\task.py", line 27, in runTest
    self.test_runner.run_test(self.testcase_dict)
httprunner.exceptions.ExtractFailure: Failed to extract attribute from response body! => content.authenticated
response body: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>404 Not Found</title>
<h1>Not Found</h1>
<p>The requested URL was not found on the server.  If you entered the URL manually please check your spelling and try again.</p>



During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\python35\lib\site-packages\httprunner\task.py", line 29, in runTest
    self.fail(repr(ex))
AssertionError: ExtractFailure('Failed to extract attribute from response body! => content.authenticated\nresponse body: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">\n<title>404 Not Found</title>\n<h1>Not Found</h1>\n<p>The requested URL was not found on the server.  If you entered the URL manually please check your spelling and try again.</p>\n\n',)

----------------------------------------------------------------------
Ran 1 test in 0.930s

FAILED (failures=1)
INFO     Start to render Html report ...
INFO     Generated Html report: C:\Users\Shuqing\reports\1533341170.html

另外我这边使用request来进行测试是 OK 的

import requests
from requests.auth import HTTPBasicAuth

url='http://httpbin.org/basic-auth/51zxw/8888'
r=requests.get(url,auth=HTTPBasicAuth('51zxw','8888'))
print(r.text)

运行结果

C:\Python35\python.exe D:/api_test/requests_api_test/test.py
{
  "authenticated": true, 
  "user": "51zxw"
}


Process finished with exit code 0

不知道是怎么回事,希望@debugtalk能指点一下。

共收到 4 条回复 时间 点赞

也希望使用过 httpRunner 的小伙伴一起讨论交流一下。

5楼 已删除
仅楼主可见
4楼 已删除

你把 request 请求的 url 和 httprunner hook 请求的 url 分别打印出来看看呢?可能俩不一样吧

老师,你是不是在 “我要自学网” 上课的那个舒清老师哦。selenium+python 和 appium 那个?

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册