pytest学习记录01

pytest.main():main中传入不同的指令用以执行指定测试用例
    -s: 显示程序中的print/logging输出
    -v: 丰富信息模式, 输出更详细的用例执行信息
    -q: 安静模式, 不输出环境信息
    -k:关键字匹配,用and区分:匹配范围(文件名、类名、函数名)
    -x, --exitfirst, exit instantly on first error or failed test
    -h 帮助 https://blog.csdn.net/weixin_44006041/article/details/107934174
    -m tag 运行包含指定标签的所有用例
    test_reg.py运行指定模块中的所有用例

py.test 执行用例命令行参数
        https://www.cnblogs.com/linuxchao/p/linuxchao-pytest-2.html
    --collect-only:罗列出所有当前目录下所有的测试模块,测试类及测试函数
    --tb=style:屏蔽测试用例执行输出的回溯信息,可以简化用例失败时的输出信息。style可以是  (auto/long/short/line/native/no)
            https://www.cnblogs.com/yoyoketang/p/13601121.html
                --tb=auto 有多个用例失败的时候,只打印第一个和最后一个用例的回溯信息
                --tb=long 输出最详细的回溯信息
                --tb=short 输入assert的一行和系统判断内容
                --tb=line 使用一行显示错误信息
                --tb=native 只输出python标准库的回溯信息
                --tb=no 不显示回溯信息
    --lf:当一次用例执行完成后,如果其中存在失败的测试用例,那么我们可以使用此命令重新运行失败的测试用例
    --ff:如果上次测试用例出现失败的用例,当使用--ff后,失败的测试用例会首先执行,剩余的用例也会再次执行一次
    py.test test_mod.py   # 运行这个文件下的所有测试
    py.test somepath      # 运行这个路径下的所有测试文件
    py.test -k stringexpr # 只测试与 stringexpr 匹配的测试
    py.test test_mod.py::test_func  # 测试指定测试文件下的测试函数
    py.test test_mod.py::TestClass::test_method  # 测试指定测试文件下的指定测试类的测试方法
    py.test --pyargs pkg   # 测试 pkg 文件夹下所有的测试

@pytest.fixture(scope='module',params=None,autouse=False,ids=None,name=None))
    fixture的scope参数:'function','module','class','session',默认为function
    function:每个test都运行,默认是function的scope
    class:每个class的所有test只运行一次
    module:每个module的所有test只运行一次
    session:每个session只运行一次

    setup和teardown操作
        setup,在测试函数或类之前执行,完成准备工作,例如数据库链接、测试数据、打开文件等
        teardown,在测试函数或类之后执行,完成收尾工作,例如断开数据库链接、回收内存资源等
        备注:也可以通过在fixture函数中通过yield实现setup和teardown功能

pytest相关库:
    3.2. 测试顺序随机
    pip install pytest-randomly

    3.3. 分布式测试
    pip install pytest-xdist

    3.4. 出错立即返回
    pip install pytest-instafail

    Pytest的插件
    pytest-rerunfailures 用例重新执行
    pytest-html 生成html报告
    pytest-timeout 限制超时时间
    pytest-parallel 多线程使用,常用配置命令如下:
        –workers (optional) *:多进程运行需要加此参数, *是进程数。默认为1。
        –tests-per-worker (optional) *:多线程运行, *是每个worker运行的最大并发线程数。默认为1

    3.5. 计算pytest覆盖率,支持输出多种格式的测试报告
    pip install pytest-cov
        Console参数介绍
        --cov=[path], measure coverage for filesystem path (multi-allowed), 指定被测试对象,用于计算测试覆盖率
        --cov-report=type, type of report to generate: term, term-missing, annotate, html, xml (multi-allowed), 测试报告的类型
        --cov-config=path, config file for coverage, default: .coveragerc, coverage配置文件
        --no-cov-on-fail, do not report coverage if test run fails, default: False,如果测试失败,不生成测试报告
        --cov-fail-under=MIN, Fail if the total coverage is less than MIN. 如果测试覆盖率低于MIN,则认为失败

pytest-->Hook :confest.py
    1) pytest_collection_modifyitems  [动态mark]
        https://blog.csdn.net/waitan2018/article/details/104334932
        Hook 方法之 pytest_collection_modifyitems:
        是在用例收集完毕之后被调用,可以用来调整测试用例执行顺序;
            session:会话对象;
            config:配置对象;
            items:用例对象列表;
        这三个参数分别有不同的作用,都可以拿来单独使用,修改用例执行顺序主要是使用 items 参数!

        存储位置:# conftest.py
            def pytest_collection_modifyitems(items):
                print('pytest 收集到的所有测试用例:\n', items)
                for item in items:
                    print('---' * 10)
                    print('用例名:', item.name)
                    print('用例节点:', item.nodeid)
            if __name__ == '__main__':
                pytest.main(['-s'])
        ****结果:
             pytest 收集到的所有测试用例:
             [, , ]
            ------------------------------
            用例名: test_A_001
            用例节点: pytestDemo/pytest_hook_demo/test_Z.py::TestDemoA::test_A_001

       2) mark用法:配置用用例是否运行
            pytest.mark.filterwarnings
            pytest.mark.parametrize
            pytest.mark.skip
            pytest.mark.skipif
            pytest.mark.usefixtures
            pytest.mark.xfail
            Custom marks
        https://zhuanlan.zhihu.com/p/139081988
        https://docs.pytest.org/en/stable/example/markers.html#mark-examples
        https://docs.pytest.org/en/stable/example/markers.html#adding-a-custom-marker-from-a-plugin

        3) Pytest前置和后置
        setup_class/teardown_class Pytest所有用例的前置,所有用例之前只执行一次!
        setup/teardown Pytest每个用例前置

    2)pytest_runtest_makereport :修改pytest-html报告

Pytest收集测试用例的规则
    https://www.cnblogs.com/linuxchao/p/linuxchao-pytest-2.html
    1)从一个或者多个目录开始查找,你可以在命令行指定文件或者目录,如果未指定那么从当前目录开始收集用例
    2)在该目录和所有子目录下递归查找测试模块
    3)测试模块是指文件名为test_*.py或者*_test.py的文件
    4)在测试模块中查找以test_开头的函数
    5)查找名字以Test开头的类。其中首先筛选掉包含__init__()函数的类,再查找类中以Test_开头的类方法

参考:
    python的测试工具大全
    https://wiki.python.org/moin/PythonTestingToolsTaxonomy

    python主流的测试工具横向比较
    http://docs.python-guide.org/en/latest/writing/tests/
    http://pythontesting.net/test-podcast/

    python单元测试框架pytest简介
    https://blog.csdn.net/liuchunming033/article/details/46501653

    https://blog.csdn.net/qq_42610167/article/details/101204066
    pytest的详细介绍,感觉架构比较全面,细节要自己查找

    https://cloud.tencent.com/developer/article/1088383
    一个简单的pytest 接口测试用例 可直接执行

    https://www.jianshu.com/p/5f671aca2b5a
    自动化测试代码框架,自动管理等

    http://www.testtalking.com/#/data/data
    测试接口包

Original: https://blog.csdn.net/gtestcandle/article/details/115673271
Author: gtestcandle
Title: pytest学习记录01

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/776308/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球