016、fixture之 autouse 参数

时间:2021-08-07
本文章向大家介绍016、fixture之 autouse 参数,主要包括016、fixture之 autouse 参数使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1、fixture之 autouse 参数

  概述:

  a、autouse 参数,默认为False,需要使用时手动调用 , 如果设置为True,相关层级所有的用例都会自动使用这个fixture ;

  b、fixture 的 autouse=True 测试函数自动调用,不需要传函数名 ,这样也就无法获取到  fixture 的返回值 ,一般不怎么用

  

a、autouse 参数,默认为False,需要使用时手动调用 , 如果设置为True,相关层级所有的用例都会自动使用这个fixture ;

  示例结构层级如下:

  conftest.py  代码如下:

import pytest


# 设置fixture,scope='class' ,autouse 的默认参数是:False,
# 某个类需要调用时使用 @pytest.mark.usefixtures('open_browser')
@pytest.fixture(scope='class')
def open_browser():
    print('打开浏览器')


# 设置fixture,scope='function' ,autouse 的默认参数是:False
# 某个函数需要调用时,把 open_web 函数名传给某个函数即可,比如:def test_aa_func_1(open_web)
@pytest.fixture(scope='function')
def open_web():
    print('打开网站')


# 设置fixture,scope='function' ,autouse 设置为 True,所有的函数、方法都会默认调用login
@pytest.fixture(scope='function', autouse=True)
def login():
    print('开始登录')
View Code

  test_aa.py 代码如下:

import pytest


# 当 autouse没有为True时,需要手动调用;
# 手动调用 open_web,fixture ,
def test_aa_func_1(open_web):
    a = 'hello'
    b = 'world'
    print('\n============= test_aa_func_1 ==============')
    assert a in 'hello world'


def test_aa_func_2():
    a = 'hello'
    b = 'world'
    print('\n============= test_aa_func_2 ==============')
    assert a in 'hello world'


# 手动调用 类 fixture;
@pytest.mark.usefixtures('open_browser')
class TestAA():

    def test_aa_1(self):
        a = 'hello'
        b = 'world'
        print('\n============= test_aa_1 ==============')
        assert a in 'hello world'

    def test_aa_2(self):
        a = 'hello'
        b = 'world'
        print('\n============= test_aa_2 ==============')
        assert a in 'hello world'


class TestAATemp():

    def test_aa_temp_1(self, open_web):
        a = 'hello'
        b = 'world'
        print('\n============= test_aa_temp_1 ==============')
        assert a in 'hello world'

    def test_aa_temp_2(self):
        a = 'hello'
        b = 'world'
        print('\n============= test_aa_temp_2 ==============')
        assert a in 'hello world'
View Code

  运行结果如下:

(venv) D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day11\aa>pytest -sv
======================================================================== test session starts ========================================================================
platform win32 -- Python 3.8.6, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day11\venv\Scripts\python.exe
cachedir: .pytest_cache
rootdir: D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day11\aa
collected 6 items                                                                                                                                                    

test_aa.py::test_aa_func_1 开始登录
打开网站

============= test_aa_func_1 ==============
PASSED
test_aa.py::test_aa_func_2 开始登录

============= test_aa_func_2 ==============
PASSED
test_aa.py::TestAA::test_aa_1 打开浏览器
开始登录

============= test_aa_1 ==============
PASSED
test_aa.py::TestAA::test_aa_2 开始登录

============= test_aa_2 ==============
PASSED
test_aa.py::TestAATemp::test_aa_temp_1 开始登录
打开网站

============= test_aa_temp_1 ==============
PASSED
test_aa.py::TestAATemp::test_aa_temp_2 开始登录

============= test_aa_temp_2 ==============
PASSED

========================================================================= 6 passed in 0.05s =========================================================================

(venv) D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day11\aa>
View Code

b、fixture 的 autouse=True 测试函数自动调用,不需要传函数名 ,这样也就无法获取到  fixture 的返回值 ,一般不怎么用 ;

  autouse=True 示例代码如下:

import pytest


@pytest.fixture(scope='function', autouse=True)
def login():
    print('\n登录成功')
    return 'login success'

# login ,fixture 中的 autouse 已经设置为 True,
# 测试函数自动调用,不需要传函数名 ,这样也就无法获取到 login 的返回值了;
def test_login_1():
    print('\n========== test_login_1 =========')


def test_login_2():
    print('\n========== test_login_2 =========')
View Code

  执行结果如下:

D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day11\venv\Scripts\python.exe "C:\SkyWorkSpace\WorkTools\PyCharm\PyCharm_Community_Edition_202003\PyCharm Community Edition 2020.3\plugins\python-ce\helpers\pycharm\_jb_pytest_runner.py" --path D:/SkyWorkSpace/WorkSpace/Pytest/Temp/day11/dd/test_dd.py
Testing started at 10:06 ...
Launching pytest with arguments D:/SkyWorkSpace/WorkSpace/Pytest/Temp/day11/dd/test_dd.py in D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day11\dd

============================= test session starts =============================
platform win32 -- Python 3.8.6, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day11\venv\Scripts\python.exe
cachedir: .pytest_cache
rootdir: D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day11\dd
collecting ... collected 2 items

test_dd.py::test_login_1 
登录成功
PASSED                                          [ 50%]
========== test_login_1 =========

test_dd.py::test_login_2 
登录成功
PASSED                                          [100%]
========== test_login_2 =========


============================== 2 passed in 0.01s ==============================

Process finished with exit code 0
View Code

  autouse=Fasle ,有返回值,示例代码如下:

import pytest


# autouse默认为False
@pytest.fixture(scope='function')
def login():
    print('\n登录成功')
    return 'login success'


# login ,fixture 中的 autouse默认为False,
# 测试函数调用 login 可得到返回值
def test_login_1(login):
    print('\n返回值为:{0}'.format(login))
    print('\n========== test_login_1 =========')


# login ,fixture 中的 autouse默认为False,
# 测试函数没有调用 login
def test_login_2():
    print('\n========== test_login_2 =========')
View Code

  执行结果如下:

D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day11\venv\Scripts\python.exe "C:\SkyWorkSpace\WorkTools\PyCharm\PyCharm_Community_Edition_202003\PyCharm Community Edition 2020.3\plugins\python-ce\helpers\pycharm\_jb_pytest_runner.py" --path D:/SkyWorkSpace/WorkSpace/Pytest/Temp/day11/dd/test_dd.py
Testing started at 10:17 ...
Launching pytest with arguments D:/SkyWorkSpace/WorkSpace/Pytest/Temp/day11/dd/test_dd.py in D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day11\dd

============================= test session starts =============================
platform win32 -- Python 3.8.6, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day11\venv\Scripts\python.exe
cachedir: .pytest_cache
rootdir: D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day11\dd
collecting ... collected 2 items

test_dd.py::test_login_1 
登录成功
PASSED                                          [ 50%]
返回值为:login success

========== test_login_1 =========

test_dd.py::test_login_2 PASSED                                          [100%]
========== test_login_2 =========


============================== 2 passed in 0.01s ==============================

Process finished with exit code 0
View Code

原文地址:https://www.cnblogs.com/qq-2780619724/p/15111175.html