pytest简单介绍

pytest简单介绍

pytest:Pytest是一个使创建简单及可扩展性测试用例变得非常方便的框架。测试用例清晰、易读而无需大量的繁琐代码。只要几分钟你就可以对你的应用程序或者库展开一个小型的单元测试或者复杂的功能测试。pytest支持第三方插件,灵活性较高。

官方文档https://pypi.org/project/pytest/5.1.0/

python支持版本:Python2.0,python3.0+

pytest功能

  • 通过python编写脚本,简单方便
  • pytest支持调用unittest用例
  • pytest支持参数化
  • pytest支持并发运行
  • 执行特定的用例顺序
  • 利用插件生成html报告

安装: pip install pytest 

查看版本号: pytest --version 

快速上手

这里就直接编写代码了

复制代码
class Test:
    def test_01(self):
        print('这是用例01')
        assert 1 == 1

    def test_02(self):
        print('这是用例02')
        assert 2 == 2

    def test_03(self):
        print('这是用例03')
        assert 1 == 2
复制代码

执行代码可以通过cmd打开文件地址也可以通过pycharm下的Terminal打开进行输入pytest

复制代码
E:\auto_test>pytest
======================================================================== test session starts ========================================================================
platform win32 -- Python 3.7.7, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
rootdir: E:\auto_test
collected 3 items                                                                                                                                                    

test_01.py ..F                                                                                                                                                 [100%]

============================================================================= FAILURES ==============================================================================
___________________________________________________________________________ Test.test_03 ____________________________________________________________________________

self = <test_01.Test object at 0x0000020F9536F788>

    def test_03(self):
        print('这是用例03')
>       assert 1 == 2
E       assert 1 == 2

test_01.py:12: AssertionError
----------------------------------------------------------------------- Captured stdout call ------------------------------------------------------------------------
这是用例03
====================================================================== short test summary info ======================================================================
FAILED test_01.py::Test::test_03 - assert 1 == 2
==================================================================== 1 failed, 2 passed in 0.07s ====================================================================
复制代码

结果解析:

我们可以从上面执行结果看到,执行了多少个用例,有多少个失败的,且运行时间以及报错内容

这里的执行用例规则是通过查找当前目录下的test_*.py或者*_test.py文件,找到后进行查找test开头的类以及test开头的函数进行执行

 

知识总结

  1. pytest执行规则是通过调用目录下文件名为test_*.py文件
  2. pytest执行时类名必须是通过Test开头,函数也必须是test为前缀,且类Test下不能有__init__方法
  3. pytest断言是通过assert进行调用
  4. pytest执行通过cmd进行在当前目录执行

原文链接:

https://www.cnblogs.com/qican/p/13959626.html

posted @ 2022-01-04 20:30  超级宝宝11  阅读(1234)  评论(0编辑  收藏  举报