前提:需要安装
pytest
和
pytest-html(
生成
html
测试报告)
pip install pytest
和
pip install pytest-html
![](https://img-blog.csdnimg.cn/e2f8d8c1994248ae92627ff4aeda14d6.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAbTBfNjEwNTkzMTc=,size_20,color_FFFFFF,t_70,g_se,x_16)
1:命名规则
Pytest
单元测试中的类名和方法名必须是以
test
开头
,
执行中只能找到
test
开头的类和方法,比
unittest 更加严谨
unittest
:
Setup>> setupclass , teardown >> teardownclass
(课堂作业)
Pytest: setup, setup_class
和
teardown, teardown_class
函数
(
和
unittest
执行效果一样
)
运行于测试方法的始末,即
:
运行一次测试函数会运行一次
setup
和
teardown
运行于测试方法的始末
,
但是不管有多少测试函数都只执行一次
setup_class
和
teardown_class
2:Pytest
生成自带的
html
测试报告
前提条件:需要下载
pytest-html
模块
(python
自带的生成测试报告模块
)
pip install pytest-html
![](https://img-blog.csdnimg.cn/1521d5a38dec4d36adf559349cd2d33b.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAbTBfNjEwNTkzMTc=,size_20,color_FFFFFF,t_70,g_se,x_16)
案例一
pytest.main("
模块
.py")
【运行指定模块下,运行所有
test
开头的类和测试用例】
pytest
.
main
([
"--html=./report.html"
,
"
模块
.py"
])