zoukankan      html  css  js  c++  java
  • 一、Pytest 框架入门

    (使用规范、命令选项、测试来源、环境变量)

    1.   使用规范:
    文件名以test_*.py文件和*_test.py
    以test_开头的函数
    以Test开头的类
    以test_开头的方法
    所有的包pakege必须要有__init__.py文件
    2.   执行用例规则:
    1.执行某个目录下所有的用例
    pytest 目录名/
    2.执行某一个py文件下用例
    pytest 脚本名称.py
    3.-k 按关键字匹配
    pytest -k "MyClass and not method"
    4.按节点运行

    每个收集的测试都分配了一个唯一的nodeid,它由模块文件名和后跟说明符组成来自参数化的类名,函数名和参数,由:: characters分隔。

    运行.py模块里面的某个函数

    pytest test_mod.py::test_func

    运行.py模块里面,测试类里面的某个方法

    pytest test_mod.py::TestClass::test_method
    5.标记表达式
    pytest -m slow

    将运行用@pytest.mark.slow装饰器修饰的所有测试。后面章节会讲自定义标记mark的功能

    -x 遇到错误时停止测试
    pytest -x test_class.py
    --maxfail=num

    当用例错误个数达到指定数量时,停止测试

    pytest --maxfail=3
    3.在python代码中使用pytest执行测试
    1.    直接调用
    Pytest.main()
    2.    传参调用
    Pytest.main("-s", "mytest.py")
    4.常用的命令行选项详解
    1. --collect-only选项

    使用--collect-only选项可以展示在给定的配置下哪些测试用例会被运行。此选项可以让你在执行测试前,非常方便地查看选中的用例是否符合预期。

    (venv) D:PyLibCodemaoApiTest>pytest --collect-only
    ============================================================================================= test session starts ==============================================================================================
    platform win32 -- Python 3.6.5rc1, pytest-5.0.1, py-1.8.0, pluggy-0.12.0
    rootdir: D:PyLibCodemaoApiTest
    plugins: allure-pytest-2.7.1, forked-1.0.2, html-1.21.1, metadata-1.8.0, xdist-1.29.0
    collected 12 items                                                                                                                                                                                              
    <Package D:PyLibCodemaoApiTest	ests>
      <Module test_ban_and_recover_login.py>
        <Function test_ban_login_one>
        <Function test_ban_and_recover_login_two>
      <Module test_ban_and_recover_publish.py>
        <Function test_ban_one>
        <Function test_ban_two>
      <Module test_ban_comment.py>
        <Function test_ban_one>
        <Function test_ban_two>
        <Function test_ban_three>
        <Function test_ban_four>
      <Module test_delete_comments.py>
        <Function test_delete_comments_one>
      <Module test_delete_works.py>
        <Function test_delete_works_one>
      <Module test_recover_comment.py>
        <Function test_recover_one>
        <Function test_recover_two>
    
    
    
    
    ========================================================================================= no tests ran in 0.27 seconds ========================================================================================
    2.-k选项

    -k选项允许你使用表达式指定希望运行的用例。如果某测试名是唯一的,或者多个测试名的前缀或后缀相同,那么可以使用表达式来快速定位。假设希望选中有关publish和comment的用例,那么

    (venv) D:PyLibCodemaoApiTest>pytest -k "comment or publish" --collect-only
    ============================================================================================= test session starts ==============================================================================================
    platform win32 -- Python 3.6.5rc1, pytest-5.0.1, py-1.8.0, pluggy-0.12.0
    rootdir: D:PyLibCodemaoApiTest
    plugins: allure-pytest-2.7.1, forked-1.0.2, html-1.21.1, metadata-1.8.0, xdist-1.29.0
    collected 12 items / 3 deselected / 9 selected                                                                                                                                                                  
    <Package D:PyLibCodemaoApiTest	ests>
      <Module test_ban_and_recover_publish.py>
        <Function test_ban_one>
        <Function test_ban_two>
      <Module test_ban_comment.py>
        <Function test_ban_one>
        <Function test_ban_two>
        <Function test_ban_three>
        <Function test_ban_four>
      <Module test_delete_comments.py>
        <Function test_delete_comments_one>
      <Module test_recover_comment.py>
        <Function test_recover_one>
        <Function test_recover_two>
    
    
    ========================================================================================= 3 deselected in 0.12 seconds =========================================================================================
    (venv) D:PyLibCodemaoApiTest>pytest -k "one and not ban" --collect-only
    ============================================================================================= test session starts ==============================================================================================
    platform win32 -- Python 3.6.5rc1, pytest-5.0.1, py-1.8.0, pluggy-0.12.0
    rootdir: D:PyLibCodemaoApiTest
    plugins: allure-pytest-2.7.1, forked-1.0.2, html-1.21.1, metadata-1.8.0, xdist-1.29.0
    collected 12 items / 9 deselected / 3 selected                                                                                                                                                                  
    <Package D:PyLibCodemaoApiTest	ests>
      <Module test_delete_comments.py>
        <Function test_delete_comments_one>
      <Module test_delete_works.py>
        <Function test_delete_works_one>
      <Module test_recover_comment.py>
        <Function test_recover_one>
    
    
    ========================================================================================= 9 deselected in 0.12 seconds =========================================================================================
    3.-m选项

    标记(marker)用于标记测试并分组,以便快速选中并运行。

    (venv) D:PyLibCodemaoApiTest>pytest -m smoke
    ============================================================================================= test session starts ==============================================================================================
    platform win32 -- Python 3.6.5rc1, pytest-5.0.1, py-1.8.0, pluggy-0.12.0
    rootdir: D:PyLibCodemaoApiTest, inifile: pytest.ini
    plugins: allure-pytest-2.7.1, forked-1.0.2, html-1.21.1, metadata-1.8.0, xdist-1.29.0
    collected 12 items / 11 deselected / 1 selected                                                                                                                                                                 
    
    
    tests	est_recover_comment.py .                                                                                                                                                                           [100%]
    
    
    =================================================================================== 1 passed, 11 deselected in 13.47 seconds ==================================================================================
    4.-x选项

    首次失败后停止执行

    5.-v(--verbose)选项
    (venv) D:PyLibNemoDataFactory	ests>pytest -v test_add.py
    ============================================================================================= test session starts ==============================================================================================
    platform win32 -- Python 3.6.5rc1, pytest-5.0.1, py-1.8.0, pluggy-0.12.0 -- d:pylib
    emodatafactoryvenvscriptspython.exe
    cachedir: .pytest_cache
    metadata: {'Python': '3.6.5rc1', 'Platform': 'Windows-10-10.0.17763-SP0', 'Packages': {'pytest': '5.0.1', 'py': '1.8.0', 'pluggy': '0.12.0'}, 'Plugins': {'allure-pytest': '2.7.1', 'html': '1.21.1', 'metadata':
     '1.8.0'}, 'JAVA_HOME': 'D:\software\Java'}
    rootdir: D:PyLibNemoDataFactory	ests
    plugins: allure-pytest-2.7.1, html-1.21.1, metadata-1.8.0
    collected 24 items                                                                                                                                                                                              
    
    
    test_add.py::test_add_bcm[{'name': 测试所有声音播放}] PASSED                                                                                                                                              [  4%]
    test_add.py::test_add_bcm[{'name': 重构后得声音模块}] PASSED                                                                                                                                              [  8%]
    test_add.py::test_add_bcm[{'name': 测试说你好}] PASSED                                                                                                                                                    [ 12%]
    test_add.py::test_add_bcm[{'name': 测试侦测颜色}] PASSED                                                                                                                                                  [ 16%]
    test_add.py::test_add_bcm[{'name': 测试侦测屏幕边缘}] PASSED                                                                                                                                              [ 20%]
    test_add.py::test_add_bcm[{'name': 测试计时器}] PASSED                                                                                                                                  #                  [ 25%]
    test_add.py::test_add_bcm[{'name': 测试手指}] PASSED                                                                                                                                                      [ 29%]
    test_add.py::test_add_bcm[{'name': 测试画笔}] PASSED                                                                                                                                                      [ 33%]
    test_add.py::test_add_bcm[{'name': 测试变量运算}] PASSED                                                                                                                                                  [ 37%]
    test_add.py::test_add_bcm[{'name': 音乐播放器嘟嘟}] PASSED                                                                                                                                                [ 41%]
    test_add.py::test_add_bcm[{'name': 追击飞电鼠}] PASSED                                                                                                                                                    [ 45%]
    test_add.py::test_add_bcm[{'name': 5000积木数}] PASSED                                                                                                                                                    [ 50%]
    test_add.py::test_add_bcm[{'name': 测试克隆类积木}] PASSED                                                                                                                                                [ 54%]
    test_add.py::test_add_bcm[{'name': 测试控制类积木}] PASSED                                                                                                                                                [ 58%]
    test_add.py::test_add_bcm[{'name': 测试动作类积木}] PASSED                                                                                                                                                [ 62%]
    test_add.py::test_add_bcm[{'name': 编程猫快跑}] PASSED                                                                                                                                                    [ 66%]
    test_add.py::test_add_bcm[{'name': 测试侦测类}] PASSED                                                                                                                                                    [ 70%]
    test_add.py::test_add_bcm[{'name': 测试水平和重力}] PASSED                                                                                                                                                [ 75%]
    test_add.py::test_add_bcm[{'name': 外观积木汇总}] PASSED                                                                                                                                                  [ 79%]
    test_add.py::test_add_bcm[{'name': 测试当角色被点击}] PASSED                                                                                                                                              [ 83%]
    test_add.py::test_add_bcm[{'name': 测试外观类}] PASSED                                                                                                                                                    [ 87%]
    test_add.py::test_add_bcm[{'name': 测试x分量}] PASSED                                                                                                                                                     [ 91%]
    test_add.py::test_add_bcm[{'name': 测试当积木}] PASSED                                                                                                                                                    [ 95%]
    test_add.py::test_add_bcm[{'name': (创作页内运行)测试复制积木能否克隆}] PASSED                                                                                                                            [100%]
    
    
    ========================================================================================== 24 passed in 32.01 seconds =========================================================================================

    6.-q(--quiet)选项

    与-v相反,会简化输出信息。

    (venv) D:PyLibNemoDataFactory	ests>pytest -q test_add.py
    ........................                                                                                                                                                                                  [100%]
    24 passed in 31.47 seconds
    7.-s选项

    允许终端在测试时输出任何符合标准输出流信息(例如print语句)

    8.-l(--showlocals)选项

    使用-l选项,失败的测试用例由于被堆栈追踪,所以局部变量及其值都会显示出来。

    (venv) D:PyLibNemoDataFactory	ests>pytest -l test_add.py
    ============================================================================================= test session starts ==============================================================================================
    platform win32 -- Python 3.6.5rc1, pytest-5.0.1, py-1.8.0, pluggy-0.12.0
    rootdir: D:PyLibNemoDataFactory	ests
    plugins: allure-pytest-2.7.1, html-1.21.1, metadata-1.8.0
    collected 24 items                                                                                                                                                                                              
    
    
    test_add.py ........................                                                                                                                                                                      [100%]
    
    
    ========================================================================================== 24 passed in 30.74 seconds =========================================================================================
    9.--tb=style选项

    --tb=no屏蔽全部回溯信息

    --tb=line可以告诉我们错误的位置

    --tb=short显示的回溯信息比上面两种模式更详细

    --tb=long输出最为详尽的回溯信息

    --tb=auto默认值,如果有多个测试用例失败,仅打印第一个和最后一个用例的回溯信息(格式与long模式的一致)

    --tb=native只输出python标准库的回溯信息,不显示额外信息

    10.--duratioins=N选项

    --durations=N会显示最慢的N个阶段(包括每个测试用例的call、setup、teardown),耗时越长越靠前。如果使用--durations=0,则会将所以阶段按耗时从长到短排序后显示。

    (venv) D:PyLibCodemaoApiTest>pytest tests --durations=0
    ============================================================================================= test session starts ==============================================================================================
    platform win32 -- Python 3.6.5rc1, pytest-5.0.1, py-1.8.0, pluggy-0.12.0
    rootdir: D:PyLibCodemaoApiTest, inifile: pytest.ini
    plugins: allure-pytest-2.7.1, forked-1.0.2, html-1.21.1, metadata-1.8.0, xdist-1.29.0
    collected 12 items                                                                                                                                                                                              
    
    
    tests	est_ban_and_recover_login.py ..                                                                                                                                                                    [ 16%]
    tests	est_ban_and_recover_publish.py ..                                                                                                                                                                  [ 33%]
    tests	est_ban_comment.py ....                                                                                                                                                                            [ 66%]
    tests	est_delete_comments.py .                                                                                                                                                                           [ 75%]
    tests	est_delete_works.py .                                                                                                                                                                              [ 83%]
    tests	est_recover_comment.py ..                                                                                                                                                                          [100%]
    
    
    ============================================================================================ slowest test durations ============================================================================================
    37.84s call     tests/test_delete_comments.py::test_delete_comments_one
    15.91s call     tests/test_delete_works.py::test_delete_works_one
    12.94s call     tests/test_recover_comment.py::test_recover_one
    9.03s call     tests/test_ban_and_recover_login.py::test_ban_and_recover_login_two
    8.56s call     tests/test_recover_comment.py::test_recover_two
    6.10s call     tests/test_ban_comment.py::test_ban_two
    6.05s call     tests/test_ban_comment.py::test_ban_four
    5.21s teardown tests/test_ban_and_recover_login.py::test_ban_and_recover_login_two
    3.92s call     tests/test_ban_and_recover_publish.py::test_ban_two
    3.75s setup    tests/test_ban_and_recover_login.py::test_ban_and_recover_login_two
    3.32s setup    tests/test_ban_and_recover_login.py::test_ban_login_one
    2.74s setup    tests/test_ban_comment.py::test_ban_two
    2.71s setup    tests/test_recover_comment.py::test_recover_one
    2.62s setup    tests/test_delete_works.py::test_delete_works_one
    2.50s setup    tests/test_ban_comment.py::test_ban_four
    2.46s teardown tests/test_recover_comment.py::test_recover_one
    2.45s setup    tests/test_ban_comment.py::test_ban_three
    2.44s setup    tests/test_delete_comments.py::test_delete_comments_one
    2.42s setup    tests/test_ban_comment.py::test_ban_one
    2.41s teardown tests/test_ban_comment.py::test_ban_one
    2.37s setup    tests/test_recover_comment.py::test_recover_two
    2.31s teardown tests/test_ban_and_recover_publish.py::test_ban_two
    2.30s teardown tests/test_delete_comments.py::test_delete_comments_one
    2.29s teardown tests/test_ban_comment.py::test_ban_two
    2.28s teardown tests/test_delete_works.py::test_delete_works_one
    2.25s teardown tests/test_ban_and_recover_login.py::test_ban_login_one
    2.24s teardown tests/test_recover_comment.py::test_recover_two
    2.22s teardown tests/test_ban_comment.py::test_ban_three
    2.20s teardown tests/test_ban_comment.py::test_ban_four
    2.03s teardown tests/test_ban_and_recover_publish.py::test_ban_one
    1.54s call     tests/test_ban_and_recover_login.py::test_ban_login_one
    1.52s setup    tests/test_ban_and_recover_publish.py::test_ban_two
    1.47s setup    tests/test_ban_and_recover_publish.py::test_ban_one
    1.31s call     tests/test_ban_and_recover_publish.py::test_ban_one
    0.70s call     tests/test_ban_comment.py::test_ban_one
    0.66s call     tests/test_ban_comment.py::test_ban_three
    ========================================================================================= 12 passed in 165.19 seconds =========================================================================================
    11.帮助选项

    pytest --version 查看pytest版本

    pytest --fixtures 查看内置参数

    pytest -h| --help 帮助文档

  • 相关阅读:
    OpenCV 2.4.9
    开机黑屏 仅仅显示鼠标 电脑黑屏 仅仅有鼠标 移动 [已成功解决]
    吐槽一下CSDN的封停审查机制
    【课程分享】Oracle数据库系统project师
    Html的空格显示
    iOS UIWebView 访问https 绕过证书验证的方法
    Java实现 蓝桥杯VIP 算法训练 整除问题
    Java实现 蓝桥杯VIP 算法训练 数位分离
    Java实现 蓝桥杯VIP 算法训练 薪水计算
    Java实现 蓝桥杯VIP 算法训练 完数
  • 原文地址:https://www.cnblogs.com/hlsam/p/13152689.html
Copyright © 2011-2022 走看看