其他分享
首页 > 其他分享> > pytest入门到放弃7--fixture之 autouse 参数

pytest入门到放弃7--fixture之 autouse 参数

作者:互联网

1、源码解释如下:
:arg autouse: if True, the fixture func is activated for all tests that can see it. If False (the default) then an explicit reference is needed to activate the fixture.
# autouse=True 时,自动使用 fixture
#
autouse=False 时,则调用函数名来实现 fixture

2、调用 fixture 的三种方式
3、autouse=True 自动使用 fixture(在conftest中编辑,作用范围为在同一级目录中全部自动调用):
# File  : conftest.py
# IDE   : PyCharm

import pytest

@pytest.fixture(autouse=True)
def automaticallyUs():
    print('\n实例化webdriver')
    yield
    print('\n关闭webdriver')
在同一级目录中新建 .py 文件:
# File  : test_demo_9.py
# IDE   : PyCharm

def test_1():
    print('\n打开百度...')

def test_2():
    print('\n登录系统...')
运行脚本:
E:\personal\python38\python.exe E:/personal/GitWorkSpace/pytest_basic/main.py

实例化webdriver

打开百度...
.
关闭webdriver

实例化webdriver

登录系统...
.
关闭webdriver

2 passed in 0.07s

 

标签:webdriver,...,--,fixture,autouse,pytest,True
来源: https://www.cnblogs.com/xiaohuboke/p/13528520.html