接口推荐
http://httpbin.org/ 知名API接口(有在线版)
learning-API-test 国人写的用于测试领域的接口(无在线版,需要自己部署)
learning-API-test
下载接口源码
https://github.com/defnngj/learning-API-test
安装依赖库
pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple
运行接口源码
浏览器访问效果
单元测试
# 单个类
import unittest
import requests
class TestHello(unittest.TestCase):
def setUp(self):
self.base_url = "http://127.0.0.1:5000"
def test_case(self):
url = "/user/tom"
r = requests.get(self.base_url + url)
result = r.json()
self.assertEqual(result["code"], 10200)
self.assertEqual(result["message"], "hello, tom")
if __name__ == '__main__':
unittest.main()
# 多个类
import unittest
import requests
class TestHello(unittest.TestCase):
def setUp(self):
self.base_url = "http://127.0.0.1:5000"
def test_case(self):
url = "/user/tom"
r = requests.get(self.base_url + url)
result = r.json()
self.assertEqual(result["code"], 10200)
self.assertEqual(result["message"], "hello, tom")
class TestLogin(unittest.TestCase):
def setUp(self):
self.base_url = "http://127.0.0.1:5000"
def test_login_success(self):
"""登录成功"""
payload = {'username': 'admin', "password": "a123456"}
r = requests.post(self.base_url + "/login", data=payload)
result = r.json()
print(result)
self.assertEqual(result["code"], 10200)
self.assertEqual(result["message"], "login success")
def test_login_null(self):
"""用户名密码为空"""
payload = {'username': '', "password": ""}
r = requests.post(self.base_url + "/login", data=payload)
result = r.json()
print(result)
self.assertEqual(result["code"], 10103)
self.assertEqual(result["message"], "username or passwrord is null")
def test_login_error(self):
"""用户名密码为空"""
payload = {'username': 'error', "password": "error123"}
r = requests.post(self.base_url + "/login", data=payload)
result = r.json()
print(result)
self.assertEqual(result["code"], 10104)
self.assertEqual(result["message"], "username or password error")
if __name__ == '__main__':
unittest.main()
httpbin访问demo
import requests
# 请求方法post, 参数格式:from-data
# url = "http://httpbin.org/post"
# r = requests.post(url, data={'key':'value'})
# print(r.status_code) #http状态 404,200, 302
# print(r.json())
# url = "http://httpbin.org/post"
# r = requests.post(url, data={'key':'value'})
# print(r.status_code) #http状态 404,200, 302
# print(r.json())
# 请求方法:get
# r = requests.get('https://api.github.com/events')
# print(r.status_code)
# print(r.json())
# 请求方法:get, 带参数params
# payload = {'key1': 'value1', 'key2': 'value2'}
# r = requests.get("http://httpbin.org/get", params=payload)
# print(r.status_code)
# print(r.json())
# 请求方法:post, 参数格式:json
# url = 'https://api.github.com/some/endpoint'
# payload = {'some': 'data'}
# r = requests.post(url, json=payload)
# print(r.status_code)
# print(r.json())
# 带header请求头的接口调用
url = 'http://httpbin.org/post'
abc = {'user-agent': 'my-app/0.0.1'}
r = requests.post(url, headers=abc)
print(r.status_code)
print(r.json())
# 带auth信息的接口
url = 'http://httpbin.org/post'
r = requests.post(url, auth=("admin", "123"))
print(r.status_code)
print(r.json())