前面了解了pytest中的fixture的配置内容以及conftest.py中的应用,既然fixture可以代替setup和teardown,怎么在不同的场景下进行使用运行呢?比如我只想要启动浏览器一次呢?如果每个用例按照前面的都加入fixture那么每条用例都会运行,其实fixture中有参数可以进行配置,配置后可以在不同的场景下进行使用,这里就要引入新的知识fixture的作用范围。
fixture作用范围
fixture中的scope参数可以进行配置,我们可以从源码中看到scope的默认参数为function。下面也介绍了scope中的其他参数:“class”,“module”,“session”
def fixture( # noqa: F811
fixture_function: Optional[_FixtureFunction] = None,
*,
scope: "Union[_Scope, Callable[[str, Config], _Scope]]" = "function",
params: Optional[Iterable[object]] = None,
autouse: bool = False,
ids: Optional[
Union[
Iterable[Union[None, str, float, int, bool]],
Callable[[Any], Optional[object]],
]
] = None,
name: Optional[str] = None
) -> Union[FixtureFunctionMarker, _FixtureFunction]:
"""用于标识夹具工厂功能的装饰器。
该装饰器可以使用,也可以不使用参数,来定义
夹具的功能。
fixture函数的名称稍后可以被引用以导致its
在运行测试之前调用:测试模块或类可以使用
``pytest.mark.usefixtures(fixturename)`` marker.
测试函数可以直接使用fixture名称作为其中的输入参数
从fixture函数返回的fixture实例的情况
注入。
:param scope:
The scope for which this fixture is shared; one of ``"function"``
(default), ``"class"``, ``"module"``, ``"package"`` or ``"session"``.
function
参数scope默认才是就是function,主要应用单个测试函数中,yield前面的代码在测试用例运行前执行,后面的代码在测试用例运行之后运行。未添加到的测试函数,不进行执行
# test_01.py
import pytest
@pytest.fixture(scope='function')
def login():
print('输入用户名,密码,完成登录!')
yield
print('关闭浏览器!')
class Test_01:
def test_01(self, login):
print('需要用到登录功能')
print('----用例01---')
def test_02(self):
print('不需要登录功能')
print('----用例02---')
def test_03(self, login):
print('需要用到登录功能')
print('----用例03---')
if __name__ == '__main__':
pytest.main(['-s', 'test__01.py'])
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsISPrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdsATOfd3bkFGazxCMx8VesATMfhHLlN3XnxCMwEzX0xiRGZkRGZ0Xy9GbvNGLpZTY1EmMZVDUSFTU4VFRR9Fd4VGdsYTMfVmepNHLrJXYtJXZ0F2dvwVZnFWbp1zczV2YvJHctM3cv1Ce-cmbw5iYihTZ5QWZxkDZ2Y2M0ATOyEDO5EGMmNWYhBDZjN2N58CX0IzLcRDMxIDMy8CXn9Gbi9CXzV2Zh1WavwVbvNmLvR3YxUjL3M3Lc9CX6MHc0RHaiojIsJye.png)
module
scope值为“module”表示为模块级别的fixture每个模块之运行1次,无论模块中多少个测试用例,都只运行一次。
# test_01.py
import pytest
@pytest.fixture(scope='module')
def login():
print('输入用户名,密码,完成登录!')
yield
print('关闭浏览器!')
class Test_01:
def test_01(self, login):
print('需要用到登录功能')
print('----用例01---')
def test_02(self):
print('不需要登录功能')
print('----用例02---')
def test_03(self, login):
print('需要用到登录功能')
print('----用例03---')
if __name__ == '__main__':
pytest.main(['-s', 'test__01.py'])
class
scope的值为“class”表示类机会的fixture中每个测试函数都只执行一次。无论模块中多少个用例,都只执行一次。
# test_01.py
import pytest
@pytest.fixture(scope='class')
def login():
print('输入用户名,密码,完成登录!')
yield
print('关闭浏览器!')
class Test_01:
def test_01(self, login):
print('需要用到登录功能')
print('----用例01---')
def test_02(self):
print('不需要登录功能')
print('----用例02---')
def test_03(self, login):
print('需要用到登录功能')
print('----用例03---')
if __name__ == '__main__':
pytest.main(['-s', 'test__01.py'])
session
scope的值为“session”表示会话级别的fixture。每次会话都会只执行一次。每个添加的用例只会执行1次fixture
# test__01.py
import pytest
@pytest.fixture(scope='session')
def login():
print('输入用户名,密码,完成登录!')
yield
print('关闭浏览器!')
class Test_01:
def test_01(self, login):
print('需要用到登录功能')
print('----用例01---')
def test_02(self):
print('不需要登录功能')
print('----用例02---')
def test_03(self, login):
print('需要用到登录功能')
print('----用例03---')
if __name__ == '__main__':
pytest.main(['-s', 'test__01.py'])
执行顺序
了解了fixture中scope参数的各个使用范围,那么如果同时使用,这些执行顺序是什么样子的呢?实践证明下
# test__01.py
import pytest
@pytest.fixture(scope='session')
def fix_session():
print('这是属于fixture中的会话级别')
yield
print('session')
@pytest.fixture(scope='class')
def fix_class():
print('这是属于fixture中的类级别')
yield
print('session')
@pytest.fixture(scope='module')
def fix_module():
print('这是属于fixture中的模块级别')
yield
print('module')
@pytest.fixture()
def fix_function():
print('这是属于fixture中的函数级别')
yield
print('function')
class Test_01:
def test_01(self, fix_function, fix_class,fix_module,fix_session):
print('----用例01---')
if __name__ == '__main__':
pytest.main(['-s', 'test__01.py'])
通过上面实践发现执行顺序:session>>module>>class>>function