python单元测试

pytest

fixture

fixture基本介绍
pytest输出log信息
对log格式进行修饰

import pytest

@pytest.fixture(scope="session" , autouse = True , params=[1, 2, 3])
def preSet(request):
    return request.param # 这里的request.para就是传入的参数。这里简化了自己手写for循环的过程

def test_not_2(preSet):
    print('test_data: %s' % preSet)
    assert preSet!= 2

输出

$~ pytest -v -s test_fixture_param.py
============================= test session starts =============================
platform win32 -- Python 2.7.13, pytest-3.0.6, py-1.4.32, pluggy-0.4.0 
cachedir: .cache
metadata: {'Python': '2.7.13', 'Platform': 'Windows-7-6.1.7601-SP1', 'Packages': {'py': '1.4.32', 'pytest': '3.0.6', 'pluggy': '0.4.0'}, 'JAVA_HOME': 'C:\\Program Files (x86)\\Java\\jd
k1.7.0_01', 'Plugins': {'html': '1.14.2', 'metadata': '1.3.0'}}
rootdir: C:\Users\yatyang\PycharmProjects\pytest_example, inifile:
plugins: metadata-1.3.0, html-1.14.2
collected 3 items 

test_fixture_param.py::test_not_2[1] test_data: 1
PASSED
test_fixture_param.py::test_not_2[2] test_data: 2
FAILED
test_fixture_param.py::test_not_2[3] test_data: 3
PASSED

================================== FAILURES ===================================
________________________________ test_not_2[2] ________________________________

test_data = 2

    def test_not_2(test_data):
        print('test_data: %s' % test_data)
>       assert test_data != 2
E       assert 2 != 2

test_fixture_param.py:9: AssertionError
===================== 1 failed, 2 passed in 0.24 seconds ======================
  • pytest默认捕获所有的输出,保存直到所有的测试用例都执行结束,并且只输出那些失败的测试用例的信息,对于成功的测试用例,没有print的信息显示。
    -s 也就是–capture-no 可以输出所有信息

  • attention : 如果-vm写在一起也可以,但是v要在m之前

  • 不仅可以实现在测试函数之前调用,还可以用yield机制实现在测试函数之后调用。(这样似乎就不能和参数传递的功能结合在一起使用了?还没测

测试用例参数化

ref [tl;dr

  • pytest.fixture()
  • @pytest.mark.parametrize
  • _.pytest_generate_tests

测试命令行

pytest.ini

ref up给了很多实用经验,对应的文档看这里

  • allure

插件 & hook [ tl;dr

ref对应的文档总结

doctest

简单的小测试可以用这个( I prefer hhh

代码

import pytest
def parseHostList( hostIP ):
    """parse host list str into a fixed format of lists

    Arguments:
        hostIP {str} -- str whose format is like "118.229.0-15.0-255"

    Raises:
        ValueError: [can only read in 118.229.0.0/16 or 166.111.0.0/16 ]

    Returns:
        [list] -- [list of host list]

    Examples:
        >>> parseHostList("118.229.0-15.0-255")[1]
        '118.229.1.0/24'
        >>> parseHostList("118.229.0-15.0-255")[15]
        '118.229.15.0/24'
        >>> parseHostList("166.111.0.0/16")[255]
        '166.111.255.0/24'
    """
    if( hostIP == "118.229.0-15.0-255" ):  # parse into 15 tasks 
        return [ f"118.229.{x}.0/24" for x in range(16) ]

    elif( hostIP == "166.111.0.0/16" ): # parse into 256 tasks
        return [ f"166.111.{x}.0/24" for x in range(256) ]

    else:
        raise ValueError("hostlist not right")

虽然测试样例给的不是很规范…凑活看吧…

运行

  1. 直接用doctest模块

    python -m doctest -v multiThread.py
    

    -v就会把成功的也显示出来,一般我不加

  2. 结合pytest模块

    pytest --doctest-modules multiThread.py
    

    如果不加具体文件,就会把当前目录进行递归查找,加了就只会测这个
    –doctest-modules 之类的也可以写进pytest.ini文件

doctest单独写一个文件 XD

ref

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值