Pytest之基本介绍

本文介绍了Pytest,一个易于上手且功能强大的Python测试框架,包括其特点、安装、PyCharm配置以及编写和运行测试用例的规则。还提到了如何生成HTML测试报告和在PyCharm中的集成应用。
摘要由CSDN通过智能技术生成

一、Pytest概念

Pytest 是 Python 的一种单元测试框架,与 Python 自带的 unittest 测试框架类似,但是比 unittest 框架使用起来更简洁,效率更高。

二、Pytest特点

Pytest是一个非常成熟的Python测试框架,主要特点有以下几点:

非常容易上手,入门简单,文档丰富,文档中有很多实例可以参考;

能够支持简单的单元测试和复杂的功能测试;

支持参数化;

执行测试过程中可以将某些测试用例跳过(skip),或者对某些预期失败的case标记成失败;

支持重复执行(rerun)失败的 case;

支持运行由 nose, unittest 编写的测试 case;

可生成html 报告;

方便jenkins持续集成;

可支持执行部分用例;

具有很多第三方插件,并且可以自定义扩展。

三、Pytest安装

① 安装pytest命令:

pip install pytest

 ② 查看pytest版本:

pytest --version

 ③ 安装生成测试结果的HTML报告pytest-html。

pip install pytest-html

这里已经安装过,所以输出信息和第一次安装不一样。

 四、Pycharm配置Pytest

① pycharm依次选择,File->Settings->Tools->Python Integrated Tools。

② 配置用例脚本运行模式。

 ③ 菜单栏,点击Edit Configurations。

④ 依次点击"+" --》 Python tests --》pytest,配置项目路径如下:

 五、Pytest用例运行规则

用Pytest写用例时候,一定要按照下面的规则去写,否则不符合规则的测试用例是不会执行的。

  • 文件名以 test_*.py 文件或*_test.py;

  • 以  test_ 开头的函数;

  • 以  Test 开头的类,不能包含 __init__ 方法;

  •   test_ 开头的类里面的方法;

  • 所有的包(package)必项要有__init__.py 文件。

六、 Pytest简单使用

环境都准备好了,尝试下使用pytest运行用例。

① 新建py文件,写两条测试用例。

import pytest
def test_demo1():
    assert 3 == 3
def test_demo2():
    assert 3 == 5
if __name__ == '__main__':
    pytest.main()

② 运行之后,结果如下:

Testing started at 12:37 ...
C:\Users\96984\Desktop\code\learn_pytest\venv\Scripts\python.exe "C:\ruanjian\pycharm2019.3\PyCharm 2019.3.1\plugins\python\helpers\pycharm\_jb_pytest_runner.py" --path C:/Users/96984/Desktop/code/learn_pytest/demo/demo_pytest.py
Launching pytest with arguments C:/Users/96984/Desktop/code/learn_pytest/demo/demo_pytest.py in C:\Users\96984\Desktop\code\learn_pytest\demo
============================= test session starts =============================
platform win32 -- Python 3.6.8, pytest-5.4.3, py-1.9.0, pluggy-0.13.1 -- C:\Users\96984\Desktop\code\learn_pytest\venv\Scripts\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.6.8', 'Platform': 'Windows-10-10.0.18362-SP0', 'Packages': {'pytest': '5.4.3', 'py': '1.9.0', 'pluggy': '0.13.1'}, 'Plugins': {'html': '2.1.1', 'metadata': '1.10.0'}, 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_77'}
rootdir: C:\Users\96984\Desktop\code\learn_pytest\demo
plugins: html-2.1.1, metadata-1.10.0
collecting ... collected 2 items
demo_pytest.py::test_demo1 PASSED                                        [ 50%]
demo_pytest.py::test_demo2 FAILED                                        [100%]
demo_pytest.py:8 (test_demo2)
def test_demo2():
>       assert 3 == 5
E       AssertionError
demo_pytest.py:10: AssertionError
================================== FAILURES ===================================
_________________________________ test_demo2 __________________________________
    def test_demo2():
>       assert 3 == 5
E       AssertionError
demo_pytest.py:10: AssertionError
=========================== short test summary info ===========================
FAILED demo_pytest.py::test_demo2 - AssertionError
========================= 1 failed, 1 passed in 0.05s =========================
Process finished with exit code 0


 最后我邀请你进入我们的软件测试学习交流群:785128166, 大家可以一起探讨交流软件测试,共同学习软件测试技术、面试等软件测试方方面面,还会有免费直播课,收获更多测试技巧,我们一起进阶Python自动化测试/测试开发,走向高薪之路

感谢每一个认真阅读我文章的人,看着粉丝一路的上涨和关注,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:

​​

这些资料,对于从事【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴我走过了最艰难的路程,希望也能帮助到你!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值