zoukankan      html  css  js  c++  java
  • Pytest学习(二)

    第一次(或n次)故障后停止

    在第一(n)次失败后停止测试过程:

    pytest -x           # stop after first failure
    pytest --maxfail=2  # stop after two failures
    

    指定测试/选择测试

    pytest支持几种方法来运行和从命令行中选择测试。

    在模块中运行测试

    pytest test_mod.py
    

    在目录中运行测试

    pytest testing/
    

    按关键字表达式运行测试

    pytest -k "MyClass and not method"
    

    这将运行包含与给定名称匹配的名称的测试 字符串表达式 (不区分大小写),它可以包括使用文件名、类名和函数名作为变量的Python运算符。上面的例子将运行 TestMyClass.test_something 但不是 TestMyClass.test_method_simple .

    标记函数

    Pytest 查找测试策略

    默认情况下,pytest 会递归查找当前目录下所有以 test 开始或结尾的 Python 脚本,并执行文件内的所有以 test 开始或结束的函数和方法。

    对于下面脚本:

    # test_no_mark.py
    
    def test_func1():
        assert 1 == 1
    
    def test_func2():
        assert 1 != 1
    

    直接执行测试脚本会同时执行所有测试函数:

    $ pytest tests/test-function/test_no_mark.py
    ============================= test session starts =============================
    platform win32 -- Python 3.6.4, pytest-3.6.1, py-1.5.2, pluggy-0.6.0
    rootdir: F:self-repolearning-pytest, inifile:
    collected 2 items
    
    tests	est-function	est_no_mark.py .F                                   [100%]
    
    ================================== FAILURES ===================================
    _________________________________ test_func2 __________________________________
    
        def test_func2():
    >       assert 1 != 1
    E       assert 1 != 1
    
    tests	est-function	est_no_mark.py:6: AssertionError
    ===================== 1 failed, 1 passed in 0.07 seconds ======================
    

    标记测试函数

    由于某种原因(如 test_func2 的功能尚未开发完成),我们只想执行指定的测试函数。在 pytest 中有几种方式可以解决:

    第一种,显式指定函数名,通过 :: 标记。

    $ pytest tests/test-function/test_no_mark.py::test_func1
    ============================= test session starts =============================
    platform win32 -- Python 3.6.4, pytest-3.6.1, py-1.5.2, pluggy-0.6.0
    rootdir: F:self-repolearning-pytest, inifile:
    collected 1 item
    
    tests	est-function	est_no_mark.py .                                    [100%]
    
    ========================== 1 passed in 0.02 seconds ===========================
    

    第二种,使用模糊匹配,使用 -k 选项标识。

    $ pytest -k func1 tests/test-function/test_no_mark.py
    ============================= test session starts =============================
    platform win32 -- Python 3.6.4, pytest-3.6.1, py-1.5.2, pluggy-0.6.0
    rootdir: F:self-repolearning-pytest, inifile:
    collected 2 items / 1 deselected
    
    tests	est-function	est_no_mark.py .                                    [100%]
    
    =================== 1 passed, 1 deselected in 0.03 seconds ====================
    

    注解

    以上两种方法,第一种一次只能指定一个测试函数,当要进行批量测试时无能为力;第二种方法可以批量操作,但需要所有测试的函数名包含相同的模式,也不方便。

    第三种,使用 pytest.mark 在函数上进行标记。

    带标记的测试函数如:

    # test_with_mark.py
    
    @pytest.mark.finished
    def test_func1():
        assert 1 == 1
    
    @pytest.mark.unfinished
    def test_func2():
        assert 1 != 1
    

    测试时使用 -m 选择标记的测试函数:

    $ pytest -m finished tests/test-function/test_with_mark.py
    ============================= test session starts =============================
    platform win32 -- Python 3.6.4, pytest-3.6.1, py-1.5.2, pluggy-0.6.0
    rootdir: F:self-repolearning-pytest, inifile:
    collected 2 items / 1 deselected
    
    tests	est-function	est_with_mark.py .                                  [100%]
    
    =================== 1 passed, 1 deselected in 0.10 seconds ====================
    

    使用 mark,我们可以给每个函数打上不同的标记,测试时指定就可以允许所有被标记的函数。

    注解

    一个函数可以打多个标记;多个函数也可以打相同的标记。

    运行测试时使用 -m 选项可以加上逻辑,如:

    $ pytest -m "finished and commit"
    
    $ pytest -m "finished and not merged"

    跳过测试

    上一节提到 pytest 使用标记过滤测试函数,所以对于那些尚未开发完成的测试,最好的处理方式就是略过而不执行测试。

    按正向的思路,我们只要通过标记指定要测试的就可以解决这个问题;但有时候的处境是我们能进行反向的操作才是最好的解决途径,即通过标记指定要跳过的测试。

    Pytest 使用特定的标记 pytest.mark.skip 完美的解决了这个问题。

    # test_skip.py
    
    @pytest.mark.skip(reason='out-of-date api')
    def test_connect():
        pass
    

    执行结果可以看到该测试已被忽略:

    $ pytest tests/test-function/test_skip.py
    ============================= test session starts =============================
    platform win32 -- Python 3.6.4, pytest-3.6.1, py-1.5.2, pluggy-0.6.0
    rootdir: F:self-repolearning-pytest, inifile:
    collected 1 item
    
    tests	est-function	est_skip.py s                                       [100%]
    
    ========================== 1 skipped in 0.13 seconds ==========================
    

    注解

    pytest 使用 s 表示测试被跳过(SKIPPED)。

    Pytest 还支持使用 pytest.mark.skipif 为测试函数指定被忽略的条件。

    @pytest.mark.skipif(conn.__version__ < '0.2.0',
                        reason='not supported until v0.2.0')
    def test_api():
        pass


    以前-好记性不如烂笔头 现在-好记性不如烂键盘
  • 相关阅读:
    Access使用记录
    html js 表单提交前检测数据
    asp.net mvc5 action多个参数
    asp.net mvc Areas 母版页动态获取数据进行渲染
    asp.net mvc 自定义全局过滤器 验证用户是否登录
    .net 报错汇总——持续更新
    数据库基础
    Python协程
    Python 线程
    Python 队列
  • 原文地址:https://www.cnblogs.com/gexbooks/p/15007671.html
Copyright © 2011-2022 走看看