pytest(1):pytest的安装与使用

前言

pytest介绍

  1.pytest是比较成熟的一款python测试框架

  2.简单灵活,容易上手,对比unitest框架更丰富。

  3.单元测试和复杂的功能测试,都支持。

  4.同时支持selenium,appium和接口自动化测试。

  5.支持三方插件结合Allure生成测试报告,可以集成到jenkins。

pytest安装

  安装方式: pip install pytest 

  如果安装失败,查看是否是需要升级pip,出现这个错误就是需要升级pip pip install --upgrade pip 

      

  有时网络不好或者不稳定时候会出现升级失败的情况,升级时加上默认超时时间就可以了:

       pip --default-timeout=100 install --upgrade pip 

      

   升级pip之后再次安装pytest

      

  pytest的升级:

     pip install -U pytest 

  查看是否安装成功:

     pytest --version 

      

pytest使用规则

  测试文件:

    1.test_*.py

    2.*_test.py

  用例识别:

    1.Test*类包含的所有test_*的方法,注意测试类中不能带有__init__方法

    2.不在class中的所有的test_*方法

  pytest可以兼容unitest框架写的用例和方法

pytest运行

  创建一个test_a.py文件

# content of test_sample.py
def inc(x):
    return x + 1


def test_answer():
    assert inc(3) == 5

  然后终端输入pytest  -v,回车。pytest会自动去找以test开头的文件和方法,找到之后就会去执行。

platform win32 -- Python 3.6.0, pytest-6.0.1, py-1.9.0, pluggy-0.13.1
rootdir: C:Users10835	est
plugins: allure-pytest-2.8.14, forked-1.1.3, html-1.16.0, metadata-1.9.0, repeat-0.8.0, rerunfailures-9.0, xdist-1.23.2
collected 1 item

test_a.py F                                                                                                                                                          [100%]

================================================================================ FAILURES =================================================================================
_______________________________________________________________________________ test_answer _______________________________________________________________________________

    def test_answer():
>       assert inc(3) == 5
E       assert 4 == 5
E        +  where 4 = inc(3)

test_a.py:7: AssertionError
========================================================================= short test summary info =========================================================================
FAILED test_a.py::test_answer - assert 4 == 5

关注公众号获取更多更新~

原文地址:https://www.cnblogs.com/zengxuejie/p/13652036.html