html基础单元测试,Pytest 单元测试框架入门

1、pytest 是 python 的第三方单元测试框架,比自带 unittest 更简洁和高效

2、安装 pytest

pip install pytest

3、验证 pytest 是否安装成功

pip show pytest

4、使用 pytest 执行测试需要遵行的规则

搜索根目录:默认从当前目录中搜集测试用例,即在哪个目录下运行pytest命令,则从哪个目录当中搜索

符合命名规则 test_*.py 或者 *_test.py 的文件

文件中识别用例的规则:

以test_开头的函数名

以Test开头的测试类(没有__init__函数)当中,以test_开头的函数

断言必须使用 assert,pytest 中没有自带的断言方法

5、pytest 执行方式

指定文件/模块

pytest –v filename(最高级别信息 — verbose)

pytest -s filename(输出打印)

pytest -v -s filename

pytest -q filename(静默输出,不会打印用例输出)

指定一个或多个目录

pytest dir01  # dir01 为用例目录

pytest dir01 dir02

指定运行py文件中的某一个类

pytest test_demo1.py::Test02

test_demo1.py 为文件名称

Test02 为类名称

指定运行某个类中的某一个方法

pytest test_demo1.py::Test01::test_01

test_demo1.py 为文件名称

Test01 为类名称

test_01 为方法名称

6、实例介绍一

dos 窗口中执行用例

#test_demo1.py

deftest_01():print("深圳多测师")deftest_02():print("广州多测师")deftest_03():print("上海多测师")

执行方法一:pytest -q test_demo1.py

ef275d71446597ac41456fde88c03efc.png

执行方法二:pytest -s test_demo1.py

e59de707b4a0785a71592e09d31ecc39.png

执行方法三:pytest -v test_demo1.py

3a3d9ca928cd731520bb9196093b45db.png

执行方法四:pytest -v -s test_demo1.py

4542869f1728b5c61881fcb8cf9a5e97.png

7、实例介绍二

demo1

demo1 发现结果中没有用例的执行打印结果

#test_demo.py

importpytestdeftest_01():print("深圳多测师")deftest_02():print("广州多测师")deftest_03():print("上海多测师")if __name__ == '__main__':

pytest.main()#结果如下

============================= test session starts =============================platform win32-- Python 3.6.0, pytest-5.4.2, py-1.8.1, pluggy-0.13.1rootdir: D:work_docCodeFilepracticepytest_demo

plugins: html-2.1.1, metadata-1.9.0

collected3items

test_demo1.py ... [100%]============================== 3 passed in 0.05s ==============================Process finished with exit code 0

demo2

demo2 中在 main() 中加入 ["-s"],结果中就可以展示打印结果

importpytestdeftest_01():print("深圳多测师")deftest_02():print("广州多测师")deftest_03():print("上海多测师")if __name__ == '__main__':

pytest.main(["-s"]) #在 main() 中加入 ["-s"]

#结果如下

============================= test session starts =============================platform win32-- Python 3.6.0, pytest-5.4.2, py-1.8.1, pluggy-0.13.1rootdir: D:work_docCodeFilepracticepytest_demo

plugins: html-2.1.1, metadata-1.9.0

collected3items

test_demo1.py 深圳多测师

.广州多测师

.上海多测师

.============================== 3 passed in 0.02s ==============================Process finished with exit code 0

demo3

运行指定的用例文件

#test_demo1.py

deftest_01():print("深圳多测师")deftest_02():print("广州多测师")deftest_03():print("上海多测师")

#run_all.py

importpytestif __name__ == '__main__':

pytest.main(["D:/test_demo1.py"])#结果如下

============================= test session starts =============================platform win32-- Python 3.6.0, pytest-5.4.2, py-1.8.1, pluggy-0.13.1rootdir: D:

plugins: html-2.1.1, metadata-1.9.0

collected3items

test_demo1.py ... [100%]============================== 3 passed in 0.01s ==============================Process finished with exit code 0

demo4

运行指定目录下的用例文件

#demo1/test_demo1.py  demo1 目录下的 test_demo1.py 文件

deftest_01():print("深圳多测师")deftest_02():print("广州多测师")deftest_03():print("上海多测师")

#demo2/test_demo2.py  demo2 目录下的 test_demo2.py 文件

deftest_04():print("杭州多测师")deftest_05():print("北京多测师")deftest_06():print("重庆多测师")

importpytestif __name__ == '__main__':

pytest.main(["D:/demo1","D:/demo2"]) #指定 D:/demo1 和 D:/demo2 目录

#结果如下

============================= test session starts =============================platform win32-- Python 3.6.0, pytest-5.4.2, py-1.8.1, pluggy-0.13.1rootdir: D:

plugins: html-2.1.1, metadata-1.9.0

collected6items

demo1est_demo1.py ... [50%]

demo2est_demo2.py ... [100%]============================== 6 passed in 0.04s ==============================Process finished with exit code 0

8、pytest-html 生成测试报告

安装 pytest-html

pip install pytest-html

cmd 下执行用例且生成报告

1f6b42895d73a84f0f700864e5c736c2.png

代码中生成测试报告

importpytestdeftest_01():print("深圳多测师")deftest_02():print("广州多测师")deftest_03():print("上海多测师")if __name__ == '__main__':

pytest.main(["-q","test_demo1.py","--html=report.html"]) #--html=report.html 生成测试报告,也可以自定义报告路径如:--html=d://report.html

6c8af1397ad2543f10f0ba9e017f21b8.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值