1,基础配置和搭建与unitest相同,代码实现方式不同
测试文件以test_开头(以_test结尾也可以)
测试类以Test开头,并且不能带有 init 方法
测试函数以test_开头
断言使用基本的assert即可
2,直接在pycharm编辑器中,导入包pytest即可
注意:pycharm 生成/安装requirements文件,方便其他人拉取项目后一键安装包
pycharm 生成requirements文件
pip freeze > requirements.txt
pycharm 安装requirements文件
pip install (-r) requirements.txt
测试用例
最后执行pytest,代码为
pytest.main([‘-s’, ‘-q’, ‘用例路径’,‘–alluredir’,‘测试报告路径’])
获取到允许结果
最后再调用allure将报告可视化展示
说明:allure安装配置
下载地址:
https://repo.maven.apache.org/maven2/io/qameta/allure/allure-commandline/2.13.2/
下载后解压,点击运行一下allure.bat,然后将allure的bin目录配置到环境变量
运行cmd,看是否安装成功allure --version
安装成功后,在pycharm的项目下,运行命令(方法一)
1,先进入项目路径 cd D://ces/report
2,进入后,输入命令 allure generate ./xml -o ./html --clean (生成html文件并清空之前文件)
./xml 是项目运行的结果路径 ./html 是生成的新文件的路径 --clean是清空之前生成文件
生成后,发现生成了html文件
输入命令allure open report/html
自动在浏览器打开了统计文件
成功后,方法二,直接写一个py文件运行所有,不用一个个单独运行
pytest.main([‘-s’, ‘-q’, ‘D://zhujie/tests’, ‘–alluredir’, ‘D://zhujie/report/res’,‘–clean-alluredir’]) # 将D://zhujie/tests目录文件测试用例运行并生成新目录到D://zhujie/report/res下保存
os.system(‘allure generate D://zhujie/report/res -o D://zhujie/report/html --clean’) #将测试结果转化为html测试报告文件
os.system(‘allure open report/html’) #在默认浏览器打开测试报告