遇一山,过一山,处处有风景;只要勇敢向前,一路尽是繁花盛开。 | (点击查看→)【测试干货】python/java自动化、持续集成、性能、测开、简历、笔试面试等

pytest简易教程(08):fixture标志传参

 

pytest简易教程汇总,详见https://www.cnblogs.com/uncleyong/p/17982846

特点

1. 采用pytest.mark.xxx(参数)标志所需要的参数,然后在fixture中可以做一些逻辑处理

2. fixture采用request获取参数

3. 传参的个数可以是多个,类型可以为简单类型或者复杂对象

 

示例

简单类型

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/

import pytest

@pytest.fixture
def fun(request):
    marker = request.node.get_closest_marker("mydata")
    if marker is None:
        data = None
    else:
        data = marker.args[0] + 1
    return data

@pytest.mark.mydata(1)
def test_data(fun):
    print("fun={}".format(fun))

if __name__ == '__main__':
    pytest.main(['-vs'])

  

结果:

  

复杂类型

也可以是其它类型,比如列表

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/

import pytest

@pytest.fixture
def fun(request):
    marker = request.node.get_closest_marker("mydata")
    if marker is None:
        data = None
    else:
        data = marker.args[0]
        data[-1]=666
    return data

@pytest.mark.mydata([1,2,3])
def test_data(fun):
    print("fun={}".format(fun))

if __name__ == '__main__':
    pytest.main(['-vs'])

  

结果:

 

可以传多个

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/

import pytest

@pytest.fixture
def fun(request):
    marker = request.node.get_closest_marker("mydata")
    marker2 = request.node.get_closest_marker("mydata2")
    if marker is None:
        data = None
    else:
        data = marker.args[0]
        data[-1]=666
    if marker2 is None:
        data2 = None
    else:
        data2 = marker2.args[0] + 1
    return data,data2

@pytest.mark.mydata([1,2,3])
@pytest.mark.mydata2(1)
def test_data(fun):
    print("fun={}".format(fun))

if __name__ == '__main__':
    pytest.main(['-vs'])

  

结果:

 

补充:命令行执行,需要在pytest.ini文件中加配置:

[pytest]
markers =
    mydata
    mydata2

 

结果:

 

posted @ 2024-02-23 20:59  全栈测试笔记  阅读(276)  评论(0编辑  收藏  举报
浏览器标题切换
浏览器标题切换end