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

    最近看了许多Pytest的文档,发现有些已经是很早以前的版本了。沿着学习的道路,我记录下我学习Pytest的过程

    安装

    pip install pytest

    安装很简单,如果第一次安装用上述pip安装,如果已经安装了使用

    pip install -U pytest

    -U参数具有更新作用

    我的使用环境

    platform win32 -- Python 3.8.10, pytest-6.2.4, py-1.10.0, pluggy-0.13.1

    创建第一个测试

    用四行代码创建一个简单的测试函数:

    # content of test_sample.py
    def func(x):
        return x + 1
    
    
    def test_answer():
        assert func(3) == 5

    执行

    $ pytest
    =========================== test session starts ============================
    platform linux -- Python 3.x.y, pytest-6.x.y, py-1.x.y, pluggy-0.x.y
    cachedir: $PYTHON_PREFIX/.pytest_cache
    rootdir: $REGENDOC_TMPDIR
    collected 1 item
    
    test_sample.py F                                                     [100%]
    
    ================================= FAILURES =================================
    _______________________________ test_answer ________________________________
    
        def test_answer():
    >       assert func(3) == 5
    E       assert 4 == 5
    E        +  where 4 = func(3)
    
    test_sample.py:6: AssertionError
    ========================= short test summary info ==========================
    FAILED test_sample.py::test_answer - assert 4 == 5
    ============================ 1 failed in 0.12s =============================

    这里是在cmd中执行的,也可以在pycharm的Terminal执行(如果你安装了虚拟环境)

    这个 [100%] 指运行所有测试用例的总体进度。完成后,pytest会显示一个失败报告,因为 func(3) 不返 5 。

    pytest可以搜索指定目录下包括子目录的test_*.py和*_test.py脚本。注意:这里是“.py”文件

    断言

    pytest 允许您使用标准的python assert 用于验证Python测试中的期望和值

    # content of test_assert1.py
    def f():
        return 3
    
    
    def test_function():
        assert f() == 4

    断言函数返回某个值。如果此断言失败,您将看到函数调用的返回值:

    $ pytest test_assert1.py
    =========================== test session starts ============================
    platform linux -- Python 3.x.y, pytest-6.x.y, py-1.x.y, pluggy-0.x.y
    cachedir: $PYTHON_PREFIX/.pytest_cache
    rootdir: $REGENDOC_TMPDIR
    collected 1 item
    
    test_assert1.py F                                                    [100%]
    
    ================================= FAILURES =================================
    ______________________________ test_function _______________________________
    
        def test_function():
    >       assert f() == 4
    E       assert 3 == 4
    E        +  where 3 = f()
    
    test_assert1.py:6: AssertionError
    ========================= short test summary info ==========================
    FAILED test_assert1.py::test_function - assert 3 == 4
    ============================ 1 failed in 0.12s =============================

    pytest 支持显示最常见的子表达式的值,包括调用、属性、比较以及二进制和一元运算符。

    def test_ne():
        a = 7
        assert a % 2 == 0, "value was odd, should be even"

    pytest运行结果

    ==================================================================================== FAILURES =====================================================================================
    _____________________________________________________________________________________ test_ne _____________________________________________________________________________________
    
        def test_ne():
            a = 7
    >       assert a % 2 == 0, "value was odd, should be even"
    E       AssertionError: value was odd, should be even
    E       assert (7 % 2) == 0
    
    2_test.py:10: AssertionError

    如图你只是知道7%2==0失败,但7%2的值你不知道。

    改为:

    def test_ne():
        a = 7
        value = a % 2
        assert value == 0, "value was odd, should be even"

    这样会得出:

    _____________________________________________________________________________________ test_ne _____________________________________________________________________________________
    
        def test_ne():
            a = 7
            value = a % 2
    >       assert value == 0, "value was odd, should be even"
    E       AssertionError: value was odd, should be even
    E       assert 1 == 0
    
    2_test.py:11: AssertionError

    这样就会知道value的值,会帮助我们发现问题

    关于预期异常的断言

    import pytest
    
    
    def test_zero_division():
        with pytest.raises(ZeroDivisionError):
            1 / 0

    如果您需要访问实际的异常信息,可以使用:

    def test_recursion_depth():
        with pytest.raises(RuntimeError) as excinfo:
    
            def f():
                f()
    
            f()
        assert "maximum recursion" in str(excinfo.value)

    在一个类中分组多个测试

    # content of test_class_demo.py
    class TestClassDemoInstance:
        value = 0
    
        def test_one(self):
            self.value = 1
            assert self.value == 1
    
        def test_two(self):
            assert self.value == 1

    测试结果:

    $ pytest -k TestClassDemoInstance -q
    .F                                                                   [100%]
    ================================= FAILURES =================================
    ______________________ TestClassDemoInstance.test_two ______________________
    
    self = <test_class_demo.TestClassDemoInstance object at 0xdeadbeef>
    
        def test_two(self):
    >       assert self.value == 1
    E       assert 0 == 1
    E        +  where 0 = <test_class_demo.TestClassDemoInstance object at 0xdeadbeef>.value
    
    test_class_demo.py:9: AssertionError
    ========================= short test summary info ==========================
    FAILED test_class_demo.py::TestClassDemoInstance::test_two - assert 0 == 1
    1 failed, 1 passed in 0.04s

    可见test_one测试成功,test_two测试失败。在这里需注意:在类内对测试进行分组时,每个测试都有一个唯一的类实例。也就是说test_one并没有改变test_two测试实例中value的值。

    以前-好记性不如烂笔头 现在-好记性不如烂键盘
  • 相关阅读:
    我的北漂之路 北漂如饮水,冷暖自知
    初学者路径规划 | 人生苦短我用Python
    web开发快速提高工作效率的一些资源
    工作中如何快速成长和学习?
    把手账打印成书 把回忆装订成册
    算法训练 K好数 (DP)
    算法训练 最大的算式(DP)
    算法训练 数字三角形(DP)
    算法训练 Balloons in a Box (枚举,模拟)
    算法训练 Pollution Solution(计算几何)
  • 原文地址:https://www.cnblogs.com/gexbooks/p/15006820.html
Copyright © 2011-2022 走看看