• pytest.main() 学习笔记


    
    
    
    
    import pytest

    pytest.main(["-n 3"]) # 3个进程并发执行,需要装 pytest-xdist 库
    pytest.main(["--collect-only"]) # 仅仅展示所有测试用例,不跑用例
    pytest.main(["--durations=1"]) # 显示耗时最多的1个用例
    pytest.main(["-k", "test"]) # -k 表示跑*test*的用例
    pytest.main(["-s"]) # 允许终端运行时输出某些结果,例如print
    pytest.main(["-q"]) # --quiet 简化输出信息
    pytest.main(["-v"]) # --verbose 增加-v查看详细信息
    """
    在pytest中,测试函数可能返回多种结果,不只是通过或失败。如下:
    PASSED(.):测试通过
    FAILED(F):测试失败
    SKIPED(s):测试未被执行,指定测试跳过执行
    xfail(x):预期测试失败,并且确实失败
    XPASS(X):预期测试失败,但实际上运行通过,不符合预期
    ERROR(E):测试用例之外的代码触发的异常
    不使用-v后,就用的括号中字符表示结果
    """
    pytest.main(["-m", "run_first"])
    """
    使用-m对用例进行标记,用例需注释@pytest.mark.xxx,将xxx作为参数传入
    使用-m "mark1 and mark2"可以同时选中带有这两个标记的所有测试用例。
    使用-m "mark1 and not mark2"选中带哟与mark1的测试用例,而过滤掉带有mark2的测试用例
    使用-m "mark1 or mark2"则选中带有mark1或者mark2的所有测试用例
    """
    pytest.main(["--maxfail=2"]) # --maxfail=n 设定最多失败 n 次即停止
    pytest.main(["--lf"]) # --last-first 只执行失败用例
    pytest.main(["--ff"]) # --failed-first 失败的用例首先执行,但是正常的用例也会执行
    pytest.main(["-l"]) # --showlocals 打印失败用例的变量值
    pytest.main(["-x"]) # -x 遇到错误即停止 效果等同于 --maxfail=1
    pytest.main(["--tb=short"])
    """
    tb即为 traceback缩写
    --tb=style,选择失败回溯信息的展示风格
    no: 不展示回溯信息
    short: 仅输出assert一行以及系统判定内容(不显示上下文)
    long: 展示全部信息
    line: 只是用一行输出显示所有的信息错误,展示异常代码位置
    auto: 只展示第一个和最后一个错误
    native: 只展示python标准库信息,不展示额外信息
    """
    pytest.main(["-h"]) # help 弹出pytest的help信息


    pytest.main(["--html=report.html"]) # 需要装 pytest-html
    pytest.main(["--reruns 3"]) # 需要装 pytest-rerunfailures 用例失败,再重试3次
    pytest.main(["--cov", "--cov-report=html"]) # 需要装pytest-cov 展示测试覆盖率
     
    
    

    几个单横杠的,支持一起输入,比如:

    pytest.main(["-vqslxk test"])

    注意 test 是 -k 的参数,所以k要放在最后

  • 相关阅读:
    Notes of Daily Scrum Meeting(12.18)
    Notes of Daily Scrum Meeting(12.17)
    Notes of Daily Scrum Meeting(12.16)
    Notes of Daily Scrum Meeting(12.8)
    Notes of Daily Scrum Meeting(12.5)
    Notes of Daily Scrum Meeting(12.3)
    Notes of Daily Scrum Meeting(11.12)
    Linux中profile、bashrc、bash_profile之间的区别和联系
    Linux GCC编译
    mysql 5.7.16 远程连接
  • 原文地址:https://www.cnblogs.com/july401/p/14102719.html
Copyright © 2020-2023  润新知