需要安装pytest和pytest-html(生成html测试报告)
pip install pytest 和 pip install pytest-html一、命名规则
Pytest单元测试中的类名和方法名必须是以test开头,执行中只能找到test开头的类和方法,比unittest更加严谨
运行于测试方法的始末,即:运行一次测试函数会运行一次setup和teardown
运行于测试方法的始末,但是不管有多少测试函数都只执行一次setup_class和 teardown_class
前提条件:需要下载pytest-html模块(python自带的生成测试报告模块)
pip install pytest-html
2.1 方式一
- 格式
pytest.main("模块.py")【运行指定模块下,运行所有test开头的类和测试用例】pytest.main(["--html=./report.html","模块.py"])
- 代码:
pytest.main(["--html=../report1.html", "test_01.py"])
Pytest调用语句
pytst.main(['-x','--html=./report.html','t12est000.py'])
扩充:跳过
使用@pytest.mark.skip()跳过该用例(函数)
三、Pytest的运行方式
四、文件读取 4.1 读取csv文件先创建文件,然后读取
4.2 读取xml文件
五、Allure
首先配置allure的环境变量
验证allure是否配置成功
- 其次要安装allure
pip install allure-pytest
allure-pytest是Pytest的一个插件,通过它我们可以生成Allure所需要的用于生成测试报告的数据
案例
实现用户登录功能,场景为登录成功和登录失败
Pytest和allure效果展示



