最近在学习社区大牛@debugtalk的 HttpRunner接口测试框架,主要是结合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能指点一下。