zoukankan      html  css  js  c++  java
  • Pytest之收集用例及命令行参数

    Pytest概念

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

     

    Pytest特点

     

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

     

    • 非常容易上手,入门简单,文档丰富,文档中有很多实例可以参考;
    • 能够支持简单的单元测试和复杂的功能测试;
    • 支持参数化;
    • 执行测试过程中可以将某些测试用例跳过(skip),或者对某些预期失败的case标记成失败;
    • 支持重复执行(rerun)失败的 case;
    • 支持运行由 nose, unittest 编写的测试 case;
    • 可生成html 报告;
    • 方便jenkins持续集成;
    • 可支持执行部分用例;
    • 具有很多第三方插件,并且可以自定义扩展。

     

     

    Pytest安装

     

    ① 安装pytest命令:

    pip install pytest

    image.png

    image.png

    ② 查看pytest版本:

    pytest --version

    image.png

    image.png

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

    pip install pytest-html

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

    image.png

    image.png

    image.png

     

     

    Pycharm配置Pytest

     

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

     

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

    image.png

    image.png

    ③ 菜单栏,点击Edit Configurations。

    image.png

     

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

    image.png

    image.png

    image.png

     

     

    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()

     

    ② 运行之后,结果如下:在上一篇Pytest系列文章:Pytest之基本介绍,主要介绍Pytest特点、安装配置及简单运行。

    以下主要介绍:Pytest的用例收集规则及命令行参数详解。

     

     

    用例收集

    1 用例收集规则

    • 从一个或者多个目录开始查找,你可以在命令行指定文件或者目录,如果未指定那么从当前目录开始收集用例;
    • 在该目录和所有子目录下递归查找测试模块;
    • 测试模块是指文件名为test_*.py或者*_test.py的文件;
    • 在测试模块中查找以test_开头的函数;
    • 查找名字以Test开头的类。其中首先筛选掉包含__init__()函数的类,再查找类中以test_开头的类方法。

     

     

    2 运行收集的用例

    现有项目结构如下:

    image

     

    具体代码:

    login/test_login.py

    class TestLogin:
        def test_demo01(self):
            a = 1
            assert a == 1
        def add(self):
            b = 1
            c = 2
            assert b + c == 3

     

    pay/test_pay.py

    def test_pay():
        name = "vivi"
        assert "v" in name

     

    test_demo.py

    def test_demo02():
        print("这是demo02")
        a = 1
        assert a == 1

     

     

     

    3 运行测试用例

    切换到项目路径,在命令行输入:pytest -v

    输出结果为:

    image

    根据用例收集规则,TestLogin类中add方法不是以test开头,所以pytest不会收集。

     

     

    命令行参数

    1 查看命令行参数

    命令行模式下,输入:pytest --help

    说明:出现详细的命令行参数。

    image

     

     

     

    2 命令行参数详解

     

    ① pytest -v

    说明:可以输出用例更加详细的执行信息,比如用例所在的文件及用例名称等。

    image

     

    ② pytest -s

    说明:输出用例中的调式信息,比如print的打印信息等。

    test_demo.py

    def test_demo02():
        print("这是demo02")
        a = 1
        assert a == 1

     

    命令行输入:pytest -s test_demo.py

    image

     

     

    pytest -m

    说明:用于标记测试并分组,执行特定的测试用例。

    这个在后续打标记文章详细解释,此处暂不介绍。

     

     

    pytest -k

    说明:可以通过表达式运行指定的测试用例。

    比如使用命令:pytest -k "test_demo01 or test_demo02",就会指定运行test_demo01和test_demo02两条用例。

    image

     

     

    pytest -q

    说明:简化输出信息。

    image

     

    pytest -x

    说明:遇到错误或者用例不通过,则退出执行。

    手动将login/test_login.py中的用例改成不通过。

    image

     

    输入命令:pytest -v -x

    image

    收集用例3个,但第二个test_demo01用例执行失败,就会全局停止执行,所以第三个用例将不会执行。

     

    pytest --lf

    说明:--last-failed 只重新运行上次运行失败的用例(或如果没有失败的话会全部跑)。

    命令行输入: pytest -v --lf

    image

    通过运行结果看出,本次用例只是运行了上次运行错误的测试用例test_demo01。

     

    pytest --ff

    说明:--failed-first,先执行完上次失败的测试后,再执行上次正常的测试用例。

    输入命令:pytest -v --ff

    image

    通过运行结果看出,本次运行,先运行了上次失败的用例,然后执行正常的测试用例。

     

    pytest --maxfail=num

    说明:用例运行时,允许的最大失败次数,超过则立即停止执行。

    输入命令:pytest -v --maxfail=2

    image

     

    ⑩ 运行指定用例

    模块、类、函数及方法之间用::进行分割。

     

    我们先看下之前运行的结果图。

    image

     

    比如想运行TestLogin类下的测试用例。

    使用命令:pytest -v login/test_login.py::TestLogin

    image

     

     

    更多系列文章,可以关注微信公众号:ITester软件测试小栈


     

  • 相关阅读:
    Working with macro signatures
    Reset and Clear Recent Items and Frequent Places in Windows 10
    git分支演示
    The current .NET SDK does not support targeting .NET Core 2.1. Either target .NET Core 2.0 or lower, or use a version of the .NET SDK that supports .NET Core 2.1.
    Build website project by roslyn through devenv.com
    Configure environment variables for different tools in jenkins
    NUnit Console Command Line
    Code Coverage and Unit Test in SonarQube
    头脑王者 物理化学生物
    头脑王者 常识,饮食
  • 原文地址:https://www.cnblogs.com/vivia/p/14469995.html
Copyright © 2011-2022 走看看