zoukankan      html  css  js  c++  java
  • pytest运行

    pytest运行

      ·测试搜索

      运行pytest可在终端指定目录和文件,如果不指定,pytest会搜索当前目录及其子目录中以test_开头或以_test结尾的测试函数(遵循pytest的命名规则)

      以pytest方式运行,需要改工程设置默认的运行器,File - setting -Tools - python integrate tools - 项目名 - default test runner - 选择 py.test

    1、用例设计原则
    • 测试文件名以test_**.py或**_test.py
    • 测试类以Test开头,并且不能带有init方法
    • 以test_开头的函数/类方法
    • 所有的包pakege必须要有__init_.py文件

    2、单个测试用例运行(在指定文件后添加 ::test_name)

    • 运行.py模块里面的某个函数:    pytest test_mod.py::test_func
    • 运行.py模块里面,测试类里面的某个方法:  pytest test_mod.py::TestClass::test_method

    3、命令行选项

    # 该目录为测试代码, 目录为C:Usersviruser.v-desktopPycharmProjectsPYTEST_BYY	asks>

    """ 元组具有局限性,不能为元组内部的数据命名, 引入collection.namedtuple该工厂函数构造一个带字段名的元组 Task """ from collections import namedtuple Task = namedtuple('Task', ['summary', 'owner', 'done', 'id']) # Task为元组名称 Task.__new__.__defaults__ = (None, None, False, None) #__new__.__defaults__创建默认的Task对象,不必指定所有属性 def test_defaults(): #定义默认的task t1 = Task() t2 = Task(None, None, False, None) assert t1 == t2 def test_member_access(): t = Task('buy milk', 'brian') assert t.summary == 'buy milk' assert t.owner == 'brian' assert (t.done, t.id) == (False, None) def test_asdict(): # 返回一个字典格式的task t_task = Task('eat', 'tokken', True, 21) t_dict = t_task._asdict() exspected = {'summary': 'eat', 'owner':'tokken', 'done': True, 'id': 21} assert t_dict == exspected def test_replace(): t_before = Task('eat', 'tokken', True, 21) t_after = t_before._replace(id=10, done=True) t_expected = Task('eat', 'tokken', True, 10) assert t_after == t_expected

    可使用pytest--help查看全部选项

    --collect-only选项:该选项可以展示在给定的配置下会运行哪些测试用例

    例:如上图所示 Task.py中有四个test函数

    -k: 使用表达式指定希望运行的测试用例/测试类 (only run test/classes which match the given substring expression)

    $ pytest -k "member" --collect-only  # 可查看所筛选执行用例情况
    $ pytest -k "member"   # 执行test_member_access()用例

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

    -m MARKEXPR     only run tests matching given mark expression.
                 For example: -m 'mark1 and not mark2'.


    marker标记在用例前增加例:  
    @pytest.mark.markname
    def test_replace():
    。。。。。

    pytest -m run_beforetest


    警告处理:You can register custom marks to avoid this warning
    在项目下新建pytest.ini文件,里面如下配置,第二个开始要换行,且要缩进,:
    [pytest]
    markers=markname1
      markname2

    -x :通常运行用例,当某个用例失败时,其他继续运行,对于-x是:遇到失败立即停止(方便在debug时检查问题)

    -x, --exitfirst     exit instantly on first error or failed test.

    pytest -x
    pytest -x --tb=no(--tb=no:关闭错误信息回溯)

    --maxfail =num:允许失败num次后停止运行

    --maxfail=num         exit after first num failures or errors.
    
    pytest --maxfail=2  --tb=no

    -s:允许终端在测试运行时输出某些结果,包括任何符合标准的输出流信息

    对于编写测试用例时,会添加print()检查执情况,使用-s可输出print内容

    --lf  : 仅重新运行上次运行失败的测试(如果没有失败,则全部重新运行)

    --ff:   运行所有用例,但首先运行上次失败的用例

      --lf, --last-failed   rerun only the tests that failed at the last run (or all if none failed)
    
      --ff, --failed-first  run all tests, but run the last failures first.
                            This may re-order tests and thus lead to repeated fixture setup/teardown.

    -v :输出详细信息,每个测试用例占一样,展示每个测试用例的名字和执行结果

    -v, --verbose  increase verbosity.

    -q :简化输出信息,将-q和--tb=line(仅打印异常代码的位置)

     -q, --quiet           decrease verbosity.

    -l : 失败用例在回溯中显示局部变量信息

     -l, --showlocals      show locals in tracebacks (disabled by default).

    --tb = style : 捕获到失败时输出信息的显示方式,(失败信息回溯很有必要,但对多余的信息会感到厌烦,因此可以定义失败信息的输出方式获取想要的信息)

    常用模式:--tb = short、--tb =  line、--tb =  no

    --tb=style            traceback print mode (auto/long/short/line/native/no).

    --duration = N : 显示测试运行中最慢的N个阶段, N=0时:将所有阶段耗时从长到短排序显示

      --durations=N         show N slowest setup/test durations (N=0 for all).

     --version:显示当前的pytest版本及安装目录

      -V, --version         display pytest version and information about plugins.When given twice, also display information about plugins.

     -h( --help )

    (shown according to specified file_or_dir or current dir if not specified;
    显示结果仅取决于指定的文件或目录,未指定则默认使用当前目录和文件
  • 相关阅读:
    MacBook设置终端颜色,补全忽略大小写,设置命令别名alias,设置vim,设置显示git分支
    lvs
    java lock锁住特定对象
    java实现版本比较
    mysql根据时间查询日期的优化
    DIV固定宽度和动态拉伸混合水平排列
    js控制input text字符键入/字符长度限制/字母自动大写
    CSS实现响应式布局(自动拆分几列)
    重命名流程
    div按照屏幕尺寸(设备大小)进行缩放
  • 原文地址:https://www.cnblogs.com/byy521/p/15134924.html
Copyright © 2011-2022 走看看