安装pytest

在pycharm左下角Terminal下面安装

  1. pip install pytest

image.pngimage.png
image.png

  1. def inc(x):
  2. return x+1
  3. def test_answer():
  4. assert inc(3) == 5

运行

image.png
添加新的运行配置 选择【pytest】
image.png
选择目标文件
image.png

测试用例

  1. 测试方法名以 test_ 开始,在使用pytest执行过程中,可以自动识别为测试用例执行。 ```python import requests

    1.主题_主题首页

    def test_topic_home(): data = {
    1. "page": 1,
    2. "tab": "ask",
    3. "limit": 10,
    4. "mdrender": False
    } r = requests.get(‘http://49.233.108.117:3000/api/v1/topics‘, params=data) #发送get请求,请求数据使用params参数 print(r.status_code) #状态码 assert r.status_code == 200 # 对状态码进行验证 print(r.json()) #json格式 json_data = r.json() print(len(json_data[‘data’])) assert len(json_data[‘data’]) == 10 data = json_data[‘data’] for x in data:
    1. print(x['tab'])
    2. assert x['tab'] == "ask"
  1. 添加测试用例。
  2. ```python
  3. import requests
  4. #1.主题_主题首页
  5. def test_topic_home():
  6. data = {
  7. "page": 1,
  8. "tab": "ask",
  9. "limit": 10,
  10. "mdrender": False
  11. }
  12. r = requests.get('http://49.233.108.117:3000/api/v1/topics', params=data) #发送get请求,请求数据使用params参数
  13. print(r.status_code) #状态码
  14. assert r.status_code == 200 # 对状态码进行验证
  15. print(r.json()) #json格式
  16. json_data = r.json()
  17. print(len(json_data['data']))
  18. assert len(json_data['data']) == 10
  19. data = json_data['data']
  20. for x in data:
  21. print(x['tab'])
  22. assert x['tab'] == "ask"
  23. #3.主题_新建主题
  24. def test_topic_new():
  25. post_data = {
  26. "accesstoken": "2599536f-728d-46b4-88dd-9ec022586914",
  27. "title": "李进-新建主题-标题",
  28. "tab": "ask",
  29. "content": "李进-新建主题-内容"
  30. }
  31. r1 = requests.post('http://49.233.108.117:3000/api/v1/topics', data=post_data) #post请求,请求body数据使用data参数
  32. print(r1.status_code)
  33. print(r1.json())
  34. print(r1.json()['topic_id'])
  35. topic1_id = r1.json()['topic_id']
  36. r2 = requests.post('http://49.233.108.117:3000/api/v1/topics', data=post_data) #post请求,请求body数据使用data参数
  37. print(r2.status_code)
  38. print(r2.json())
  39. print(r2.json()['topic_id'])
  40. topic2_id = r2.json()['topic_id']
  41. assert not topic1_id == topic2_id
  42. #2.主题_主题详情
  43. def test_topic_detail():
  44. post_data = {
  45. "accesstoken": "2599536f-728d-46b4-88dd-9ec022586914",
  46. "title": "李进-新建主题-标题",
  47. "tab": "ask",
  48. "content": "李进-新建主题-内容"
  49. }
  50. r1 = requests.post('http://49.233.108.117:3000/api/v1/topics', data=post_data) # post请求,请求body数据使用data参数
  51. topic_id = r1.json()['topic_id']
  52. data = {
  53. "mdrender": "True",
  54. "accesstoken": "2599536f-728d-46b4-88dd-9ec022586914"
  55. }
  56. r = requests.get(f'http://49.233.108.117:3000/api/v1/topic/{topic_id}', params=data)
  57. print(r.status_code)
  58. assert r.status_code == 200
  59. print(r.json())
  60. json_data = r.json()
  61. print(len(json_data['data']))
  62. print(json_data['data']['tab'])
  63. assert json_data['data']['tab'] == post_data['tab'] # 对tab进行验证
  64. assert json_data['data']['title'] == post_data['title'] #对title 进行验证
  65. assert post_data['content'] in json_data['data']['content'] # 对content进行验证

将函数中的一些变量信息抽离出来,用参数的方式来做。

  1. import requests
  2. base_url = "http://49.233.108.117:3000"
  3. user_token = "2599536f-728d-46b4-88dd-9ec022586914"
  4. post_data = {
  5. "accesstoken": user_token,
  6. "title": "李进-新建主题-标题",
  7. "tab": "ask",
  8. "content": "李进-新建主题-内容"
  9. }
  10. def create_topic():
  11. r = requests.post(f'{base_url}/api/v1/topics', data=post_data) # post请求,请求body数据使用data参数
  12. return r
  13. #1.主题_主题首页
  14. def test_topic_home():
  15. data = {
  16. "page": 1,
  17. "tab": "ask",
  18. "limit": 10,
  19. "mdrender": False
  20. }
  21. r = requests.get(f'{base_url}/api/v1/topics', params=data) #发送get请求,请求数据使用params参数
  22. print(r.status_code) #状态码
  23. assert r.status_code == 200 # 对状态码进行验证
  24. print(r.json()) #json格式
  25. json_data = r.json()
  26. print(len(json_data['data']))
  27. assert len(json_data['data']) == 10
  28. data = json_data['data']
  29. for x in data:
  30. print(x['tab'])
  31. assert x['tab'] == "ask"
  32. #3.主题_新建主题
  33. def test_topic_new():
  34. r1 = create_topic()
  35. topic1_id = r1.json()['topic_id']
  36. r2 = create_topic() #post请求,请求body数据使用data参数
  37. topic2_id = r2.json()['topic_id']
  38. assert not topic1_id == topic2_id
  39. #2.主题_主题详情
  40. def test_topic_detail():
  41. r1 = create_topic()
  42. topic_id = r1.json()['topic_id']
  43. data = {
  44. "mdrender": "True",
  45. "accesstoken": user_token
  46. }
  47. r = requests.get(f'{base_url}/api/v1/topic/{topic_id}', params=data)
  48. print(r.status_code)
  49. assert r.status_code == 200
  50. print(r.json())
  51. json_data = r.json()
  52. print(len(json_data['data']))
  53. print(json_data['data']['tab'])
  54. assert json_data['data']['tab'] == post_data['tab'] # 对tab进行验证
  55. assert json_data['data']['title'] == post_data['title'] #对title 进行验证
  56. assert post_data['content'] in json_data['data']['content'] # 对content进行验证

测试报告

  1. 添加测试报告 模块

    1. pip install pytest
  2. 添加测试报告选项

image.png