pytest+报告插件
1、安装,我使用的Mac本
方式一:到官网(pytest官网)下载pytest源码
进入源码根目录,执行命令:python setup install
安装出现权限问题,Mac/Linux 添加sudo 运行 sudo python setup install
windows使用管理员运行cmd,执行python setup install
方式二:直接使用命令安装
mac/linux 执行命令:pip install -U pytest -U可以理解为upgrade ,升级到最新版本
windows 管理员运行cmd,执行 pip install -U pytest
pytest验证是否安装成功:
在终端执行命令:pytest --version
2、pytest配置文件
在项目根目录下创建pytest.ini文件
[pytest]
#空格分隔,可添加多个参数,-所有参数均为插件包参数
addopts = -s --alluredir report
#测试用例脚本文件所在目录
testpaths = ./testcases
#测试搜索的文件名
python_files = test_*.py
#测试搜索的测试类名
python_classes = Test*
#测试搜索的测试函数名
test_functions = test_*
3、当pytest执行测试函数之前需要有其他操作时,可在对应测试类中添加setup/setup_class函数,也可以自定义函数,使用fixture装饰器装饰
def setup(self): print('test login start!!') self.driver = init_driver() self.login_page = Login_Page(self.driver) def setup_class(self): print('test login start!!') self.driver = init_driver() self.login_page = Login_Page(self.driver) # 测试用例执行之前需要初始化信息,需使用fixture工具 @pytest.fixture(autouse=True) def pre_run(self): print("--------") self.driver = init_driver() self.login_page = Login_Page(self.driver)
4、测试报告之Html
使用插件pytest-html
安装方式:
安装包:python setup.py install
命令:pip3 install pytest-html
使用方式:在配置文件中添加参数
修改
[pytest]
#空格分隔,可添加多个参数,-所有参数均为插件包参数
addopts = -s --html=[path]/report.html
5、测试报告之 allure
插件:pytest-allure-adaptor
安装:pip3 install pytest-allure-adaptor
使用方式,在配置文件中添加数
将--html=[path]/report.html 修改为 --alluredir report
可能出现的错误:module 'pytest' has no attribute 'allure'
解决办法:
执行命令:
pip uninstall pytest-allure-adaptor
pip install allure-pytest