pip3 install pytest
pip3 install pytest-allure-adaptor
查看安装的 pytest 版本:
~ $ pytest --version
This is pytest version 5.1.2, imported from /usr/local/lib/python3.7/site-packages/pytest.py
setuptools registered plugins:
pytest-rerunfailures-4.2 at /usr/local/lib/python3.7/site-packages/pytest_rerunfailures.py
pytest-cov-2.6.0 at /usr/local/lib/python3.7/site-packages/pytest_cov/plugin.py
allure-pytest-2.8.4 at /usr/local/lib/python3.7/site-packages/allure_pytest/plugin.py
pytest-html-1.19.0 at /usr/local/lib/python3.7/site-packages/pytest_html/plugin.py
pytest-metadata-1.7.0 at /usr/local/lib/python3.7/site-packages/pytest_metadata/plugin.py
pytest -s -q --alluredir ./allure-reports/
其中 allure_reports 为临时存放上述命令生成的 json 文件目录。
allure generate ./allure-reports/ -o ./reports/ --clean
其中 reports 为我们最终生成的 html 报告目录,–clean 目的是先清空测试报告目录,再生成新的测试报告。最后,我们用浏览器打开里面的 index.html 即可看到最终的报告结果。
上面的两步也可以通过管道命令合并命令一键生成 html 报告:
pytest -s -q --alluredir ./allure-reports/ | allure generate ./allure-reports/ -o ./reports/ --clean
如果不想要两个目录,也可以把 allure-reports 目录替换为 reports 目录:
pytest -s -q --alluredir ./reports/ | allure generate ./reports/ -o ./reports/ --clean
上述的 pytest 命令是没有实时输出日志记录的,在实际项目中,我们需要输出实时日志,包括时间、文件名、行号等,可以用-o 设置相关参数,整个命令如下:
pytest -s -q --alluredir ./allure-reports/ -vv -o log_cli=true -o log_cli_level=INFO --log-date-format="%Y-%m-%d %H:%M:%S" --log-format="%(filename)s:%(lineno)s %(asctime)s %(levelname)s %(message)s"
执行结果如下:
测试代码如下:
# -*- coding: utf-8 -*-
# @file: test_requests.py.py
# @author: xulinzhou
# @date : 2019/12/19
import requests
import json
import allure
import pytest
# # /json/home/homePage的headers
# top_headers = {
# "Accept": "*/*",
# "User-Agent": "AirMonitoring/3.0.6 (iPhone; iOS 11.4.1; Scale/2.00)",
# "Accept-Language": "zh-Hans-CN;q=1, en-CN;q=0.9, zh-Hant-CN;q=0.8, zh-Hant-HK;q=0.7",
# "Content-Type": "application/json",
# "Content-Length": "383",
# "Host": "epapi.moji.com",
# "Accept-Encoding": "gzip",
# "Connection": "keep-alive"
# }
# /json/home/homePage的data
top_data = {
"common": {
"cityid": "110000",
"app_version": "45030006",
"device": "iPhone9,1",
"apnsisopen": "0",
"platform": "iPhone",
"uid": 2154359387456724365,
"language": "CN",
"identifier": "95DD96CD-A3BD-48C4-A507-E63FBBDED29C",
"token": "<545a468e 92bed312 6fe0add2 999d3a52 b9d4422a 867060c2 2d676371 b0ec5ef9>"
},
"params": {
"longitude": "116.2991775173611",
"cityId": "110000",
"latitude": "40.05391682942708"
}
}
# # /json/epa/cityRank的headers
# city_rank_headers = {
# "Accept": "*/*",
# "User-Agent": "AirMonitoring/3.0.6 (iPhone; iOS 11.4.1; Scale/2.00)",
# "Accept-Language": "zh-Hans-CN;q=1, en-CN;q=0.9, zh-Hant-CN;q=0.8, zh-Hant-HK;q=0.7",
# "Content-Type": "application/json",
# "Content-Length": "383",
# "Host": "epapi.moji.com",
# "Accept-Encoding": "gzip",
# "Connection": "keep-alive"
# }
# /json/epa/cityRank的data
city_rank_data = {
"common": {
"cityid": "110000",
"app_version": "45030006",
"device": "iPhone9,1",
"apnsisopen": "0",
"platform": "iPhone",
"uid": 2154359387456724365,
"language": "CN",
"identifier": "95DD96CD-A3BD-48C4-A507-E63FBBDED29C",
"token": "<545a468e 92bed312 6fe0add2 999d3a52 b9d4422a 867060c2 2d676371 b0ec5ef9>"
},
"params": {
"type": "1",
"dataTime": "1567267200000"
}
}
# 【空气质量发布】app的首页请求
top_url = "http://epapi.moji.com/json/home/homePage"
# 【空气质量发布】app的分析tab-月度排名
city_rank_url = "http://epapi.moji.com/json/epa/cityRank"
@allure.feature("首页")
def test_top():
# 方法1:采用json:dict
# print(type(top_data))
s = requests.post(top_url, json=top_data)
print(s)
# # 方法二:通过json.dumps把data:dict转换为data:str
# print(type(json.dumps(top_data)))
# s = requests.post(top_url, headers=top_headers, data=json.dumps(top_data))
# print(s)
s = json.loads(s.text)
print("Response:" + str(s))
assert s["code"] == 0 # 校验接口返回是否正常
@allure.feature("分析tab-月度排名")
def test_city_rank():
# 方法1:采用json:dict
# print(type(city_rank_data))
s = requests.post(city_rank_url, json=city_rank_data)
print(s)
s = json.loads(s.text)
print("Response:" + str(s))
assert s["code"] == 0 # 校验接口返回是否正常
if __name__ == "__main__":
pytest.main()
# run:
# 1、pytest -s -q --alluredir ./allure-reports/
# 2、allure generate ./allure-reports/ -o ./reports/ --clean
# 3、open ./reports/index.html
# or: pytest -s -q --alluredir ./reports/ | allure generate ./reports/ -o ./reports/ --clean