小门板儿

Menu

pytest夹具-fixture之request

一、定义

request 是 pytest 的内置 fixture , 为请求对象提供对请求测试上下文的访问权,并且在fixture被间接参数化的情况下具有可选的“param”属性。

二、request.moudle

fixture 函数可以通过接受 request 对象来反向获取请求中的测试函数、类或模块上下文,

@pytest.fixture(scope="module")
def smtp(request):
    server = getattr(request.module, "smtpserver1", "smtp.qq.com")
    print("fixture 获取到的server :%s" %server)
    smtp = smtplib.SMTP(server, 587, timeout=5)
    yield smtp
    print("完成 %s (%s)" % (smtp, server))
    smtp.close()
 
# content of test_a.py
smtpserver1 = "mail.python.org"
def test_showhelo(smtp):
    print("case showhelo")
输出结果:
D:\pytest\learn01>pytest -s a/test_a.py
========================================================================================== test session starts ===========================================================================================
platform win32 -- Python 3.7.3, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
rootdir: D:\pytest\learn01, configfile: pytest.ini
collected 1 item                                                                                                                                                                                          
a\test_a.py fixture 获取到的server :mail.python.org
case showhelo
.完成 <smtplib.SMTP object at 0x0000000003796940> (mail.python.org)

备注:

getattr语法:getattr(object, name[, default]),获取object对象中name属性的值,object -- 对象;name -- 字符串,对象属性;default -- 默认返回值,如果不提供该参数,在没有对应属性时,将触发 AttributeError。

三、request.param

request就是需要什么东西,用来接受参数,用到@pytest.fixture装饰器,传参就用默认的request参数,user = request.param 这一步是接收传入的参数。

# content of conftest.py
import pytest
​
@pytest.fixture(scope="function")
def login(request):
    user = request.param
    print("登录账户:%s"%user)
    print('ddddddddd')
    return user
​
# content of test_c.py
import pytest
# 测试账号数据
test_data = ["admin1", "admin2"]
@pytest.mark.parametrize("login", test_data, indirect=True)
def test_login(login):
    """登录用例"""
    a = login
    print("测试用例中login的返回值:%s" % a)
    assert a != ""
输出内容:
rootdir: D:\pytest\learn01, configfile: pytest.ini
collected 2 items                                                                                                                                                                                         
​
a\test_c.py 登录账户:admin1
ddddddddd
测试用例中login的返回值:admin1
.登录账户:admin2
ddddddddd
测试用例中login的返回值:admin2
.

indirect=True 参数是为了把login当作一个函数去执行,而不是一个参数

四、request.config

request.config 是获取测试的配置文件参数 conftest 配置参数:首先需要在 conftest.py 添加命令行选项,命令行传入参数”—cmdopt“, 用例如果需要用到从命令行传入的参数,就调用 cmdopt函数:

import pytest
​
def pytest_addoption(parser):
    parser.addoption(
        '--cmdopt', action='store', default='type1', help='myoption: type1 or type2'
    )
​
@pytest.fixture()
def cmdopt(request):
    return request.config.getoption('--cmdopt')
​
#content of test_a.py
import pytest
​
def test_answer(cmdopt):
    if cmdopt == 'type1':
        print('**********')
        print('first')
    elif cmdopt =='type2':
        print('----------')
        print('second')
    assert 0
执行测试命令>:pytest -s a/test_a.py
1、如果不带参数执行,那么传默认的 default=”type1”
输出:
a\test_a.py **********
first
2、带参数执行:pytest -s a/test_a.py  --cmdopt=type2
输出:
a\test_a.py ----------
second
3、命令行传参数有两种写法,有一种分成 2 个参数也可以的,参数和名称用空格隔开 :pytest -s a/test_a.py  --cmdopt  type2
​

pytest_addoption 可以让用户注册一个自定义的命令行参数,方便用户将数据传递给 pytest

五、request的相关成员对象

import pytest
​
@pytest.fixture(autouse=True)
def print_request(request):
    print("\n=======================request start=================================")
    print(request.module)
    print(request.function)
    print(request.cls)
    print(request.fspath)
    print(request.fixturenames)
    print(request.fixturename)
    print(request.scope)
    print("\n=======================request end=================================")
    
#content of test_a.py
import pytest
def test_a(print_request):
    print(print_request)
    assert 1==1
输出内容:
=======================request start=================================
<module 'a.test_a' from 'D:\\pytest\\learn01\\a\\test_a.py'>
<function test_a at 0x00000000038382F0>
None
D:\pytest\learn01\a\test_a.py
['print_request', 'request']
print_request
function
​
=======================request end=================================
None
.

参考文献:https://blog.csdn.net/weixin_30763455/article/details/97358716

https://www.cnblogs.com/yoyoketang/p/14041751.html

— 于 共写了3732个字
— 标签:

评论已关闭。