安装pytest
在pycharm左下角Terminal下面安装
pip install pytest
def inc(x):
return x+1
def test_answer():
assert inc(3) == 5
运行
添加新的运行配置 选择【pytest】
选择目标文件
测试用例
- 测试方法名以
test_
开始,在使用pytest执行过程中,可以自动识别为测试用例执行。 ```python import requests1.主题_主题首页
def test_topic_home(): data = {
} 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:"page": 1,
"tab": "ask",
"limit": 10,
"mdrender": False
print(x['tab'])
assert x['tab'] == "ask"
添加测试用例。
```python
import requests
#1.主题_主题首页
def test_topic_home():
data = {
"page": 1,
"tab": "ask",
"limit": 10,
"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:
print(x['tab'])
assert x['tab'] == "ask"
#3.主题_新建主题
def test_topic_new():
post_data = {
"accesstoken": "2599536f-728d-46b4-88dd-9ec022586914",
"title": "李进-新建主题-标题",
"tab": "ask",
"content": "李进-新建主题-内容"
}
r1 = requests.post('http://49.233.108.117:3000/api/v1/topics', data=post_data) #post请求,请求body数据使用data参数
print(r1.status_code)
print(r1.json())
print(r1.json()['topic_id'])
topic1_id = r1.json()['topic_id']
r2 = requests.post('http://49.233.108.117:3000/api/v1/topics', data=post_data) #post请求,请求body数据使用data参数
print(r2.status_code)
print(r2.json())
print(r2.json()['topic_id'])
topic2_id = r2.json()['topic_id']
assert not topic1_id == topic2_id
#2.主题_主题详情
def test_topic_detail():
post_data = {
"accesstoken": "2599536f-728d-46b4-88dd-9ec022586914",
"title": "李进-新建主题-标题",
"tab": "ask",
"content": "李进-新建主题-内容"
}
r1 = requests.post('http://49.233.108.117:3000/api/v1/topics', data=post_data) # post请求,请求body数据使用data参数
topic_id = r1.json()['topic_id']
data = {
"mdrender": "True",
"accesstoken": "2599536f-728d-46b4-88dd-9ec022586914"
}
r = requests.get(f'http://49.233.108.117:3000/api/v1/topic/{topic_id}', params=data)
print(r.status_code)
assert r.status_code == 200
print(r.json())
json_data = r.json()
print(len(json_data['data']))
print(json_data['data']['tab'])
assert json_data['data']['tab'] == post_data['tab'] # 对tab进行验证
assert json_data['data']['title'] == post_data['title'] #对title 进行验证
assert post_data['content'] in json_data['data']['content'] # 对content进行验证
将函数中的一些变量信息抽离出来,用参数的方式来做。
import requests
base_url = "http://49.233.108.117:3000"
user_token = "2599536f-728d-46b4-88dd-9ec022586914"
post_data = {
"accesstoken": user_token,
"title": "李进-新建主题-标题",
"tab": "ask",
"content": "李进-新建主题-内容"
}
def create_topic():
r = requests.post(f'{base_url}/api/v1/topics', data=post_data) # post请求,请求body数据使用data参数
return r
#1.主题_主题首页
def test_topic_home():
data = {
"page": 1,
"tab": "ask",
"limit": 10,
"mdrender": False
}
r = requests.get(f'{base_url}/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:
print(x['tab'])
assert x['tab'] == "ask"
#3.主题_新建主题
def test_topic_new():
r1 = create_topic()
topic1_id = r1.json()['topic_id']
r2 = create_topic() #post请求,请求body数据使用data参数
topic2_id = r2.json()['topic_id']
assert not topic1_id == topic2_id
#2.主题_主题详情
def test_topic_detail():
r1 = create_topic()
topic_id = r1.json()['topic_id']
data = {
"mdrender": "True",
"accesstoken": user_token
}
r = requests.get(f'{base_url}/api/v1/topic/{topic_id}', params=data)
print(r.status_code)
assert r.status_code == 200
print(r.json())
json_data = r.json()
print(len(json_data['data']))
print(json_data['data']['tab'])
assert json_data['data']['tab'] == post_data['tab'] # 对tab进行验证
assert json_data['data']['title'] == post_data['title'] #对title 进行验证
assert post_data['content'] in json_data['data']['content'] # 对content进行验证
测试报告
添加测试报告 模块
pip install pytest
添加测试报告选项