接口推荐

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

运行接口源码

image.png

浏览器访问效果

image.png

单元测试

  1. # 单个类
  2. import unittest
  3. import requests
  4. class TestHello(unittest.TestCase):
  5. def setUp(self):
  6. self.base_url = "http://127.0.0.1:5000"
  7. def test_case(self):
  8. url = "/user/tom"
  9. r = requests.get(self.base_url + url)
  10. result = r.json()
  11. self.assertEqual(result["code"], 10200)
  12. self.assertEqual(result["message"], "hello, tom")
  13. if __name__ == '__main__':
  14. unittest.main()
  1. # 多个类
  2. import unittest
  3. import requests
  4. class TestHello(unittest.TestCase):
  5. def setUp(self):
  6. self.base_url = "http://127.0.0.1:5000"
  7. def test_case(self):
  8. url = "/user/tom"
  9. r = requests.get(self.base_url + url)
  10. result = r.json()
  11. self.assertEqual(result["code"], 10200)
  12. self.assertEqual(result["message"], "hello, tom")
  13. class TestLogin(unittest.TestCase):
  14. def setUp(self):
  15. self.base_url = "http://127.0.0.1:5000"
  16. def test_login_success(self):
  17. """登录成功"""
  18. payload = {'username': 'admin', "password": "a123456"}
  19. r = requests.post(self.base_url + "/login", data=payload)
  20. result = r.json()
  21. print(result)
  22. self.assertEqual(result["code"], 10200)
  23. self.assertEqual(result["message"], "login success")
  24. def test_login_null(self):
  25. """用户名密码为空"""
  26. payload = {'username': '', "password": ""}
  27. r = requests.post(self.base_url + "/login", data=payload)
  28. result = r.json()
  29. print(result)
  30. self.assertEqual(result["code"], 10103)
  31. self.assertEqual(result["message"], "username or passwrord is null")
  32. def test_login_error(self):
  33. """用户名密码为空"""
  34. payload = {'username': 'error', "password": "error123"}
  35. r = requests.post(self.base_url + "/login", data=payload)
  36. result = r.json()
  37. print(result)
  38. self.assertEqual(result["code"], 10104)
  39. self.assertEqual(result["message"], "username or password error")
  40. if __name__ == '__main__':
  41. unittest.main()

image.png

httpbin访问demo

  1. import requests
  2. # 请求方法post, 参数格式:from-data
  3. # url = "http://httpbin.org/post"
  4. # r = requests.post(url, data={'key':'value'})
  5. # print(r.status_code) #http状态 404,200, 302
  6. # print(r.json())
  7. # url = "http://httpbin.org/post"
  8. # r = requests.post(url, data={'key':'value'})
  9. # print(r.status_code) #http状态 404,200, 302
  10. # print(r.json())
  11. # 请求方法:get
  12. # r = requests.get('https://api.github.com/events')
  13. # print(r.status_code)
  14. # print(r.json())
  15. # 请求方法:get, 带参数params
  16. # payload = {'key1': 'value1', 'key2': 'value2'}
  17. # r = requests.get("http://httpbin.org/get", params=payload)
  18. # print(r.status_code)
  19. # print(r.json())
  20. # 请求方法:post, 参数格式:json
  21. # url = 'https://api.github.com/some/endpoint'
  22. # payload = {'some': 'data'}
  23. # r = requests.post(url, json=payload)
  24. # print(r.status_code)
  25. # print(r.json())
  26. # 带header请求头的接口调用
  27. url = 'http://httpbin.org/post'
  28. abc = {'user-agent': 'my-app/0.0.1'}
  29. r = requests.post(url, headers=abc)
  30. print(r.status_code)
  31. print(r.json())
  32. # 带auth信息的接口
  33. url = 'http://httpbin.org/post'
  34. r = requests.post(url, auth=("admin", "123"))
  35. print(r.status_code)
  36. print(r.json())