pytest Fixture 作用范围
【指定fixture作用范围】
fixture 包含了一个scope的可选参数,也就是作用范围,用于控制fixture执行配置和销毁逻辑,scope有四个参数【function、class、module、session(默认的是function)其实还有一个package】
源码部分的参数介绍:
1 def fixture( 2 callable_or_scope=None, 3 *args, 4 scope="function", 5 params=None, 6 autouse=False, 7 ids=None, 8 name=None 9 ): 10 """Decorator to mark a fixture factory function. 11 12 This decorator can be used, with or without parameters, to define a 13 fixture function. 14 15 The name of the fixture function can later be referenced to cause its 16 invocation ahead of running tests: test 17 modules or classes can use the ``pytest.mark.usefixtures(fixturename)`` 18 marker. 19 20 Test functions can directly use fixture names as input 21 arguments in which case the fixture instance returned from the fixture 22 function will be injected. 23 24 Fixtures can provide their values to test functions using ``return`` or ``yield`` 25 statements. When using ``yield`` the code block after the ``yield`` statement is executed 26 as teardown code regardless of the test outcome, and must yield exactly once. 27 28 :arg scope: the scope for which this fixture is shared, one of 29 ``"function"`` (default), ``"class"``, ``"module"``, 30 ``"package"`` or ``"session"`` (``"package"`` is considered **experimental** 31 at this time). 32 33 This parameter may also be a callable which receives ``(fixture_name, config)`` 34 as parameters, and must return a ``str`` with one of the values mentioned above. 35 36 See :ref:`dynamic scope` in the docs for more information. 37 38 :arg params: an optional list of parameters which will cause multiple 39 invocations of the fixture function and all of the tests 40 using it. 41 The current parameter is available in ``request.param``. 42 43 :arg autouse: if True, the fixture func is activated for all tests that 44 can see it. If False (the default) then an explicit 45 reference is needed to activate the fixture. 46 47 :arg ids: list of string ids each corresponding to the params 48 so that they are part of the test id. If no ids are provided 49 they will be generated automatically from the params. 50 51 :arg name: the name of the fixture. This defaults to the name of the 52 decorated function. If a fixture is used in the same module in 53 which it is defined, the function name of the fixture will be 54 shadowed by the function arg that requests the fixture; one way 55 to resolve this is to name the decorated function 56 ``fixture_<fixturename>`` and then use 57 ``@pytest.fixture(name='<fixturename>')``.
> scope='function'
> 函数级别的fixture每个测试函数只需要运行一次,测试用例运行前运行一次,测试用例运行后运行一次,它是scope的默认参数。> scope='class'
> 类级别的fixture每个测试类只需要运行一次,无论测试类中有多少个类方法都可以共享这个fixture。> scope='module'
> 模块级的fixture每个模块只运行一次,无论模块中有多少个测试函数、测试类、测试类方法或是其他的fixture都可以共享这个fixture。> scope='session'
> 会话级别的fixture每次会话只需要运行一次,一次pytest会话中的所有测试函数、测试类、测试类方法都可以共享这个fixture。> scope='package'
> 包级别的fixture实际和session的功能一致。
import pytest @pytest.fixture(scope='function') def function_scope(): print("@pytest.fixture(scope='function')") @pytest.fixture(scope='class') def class_scope(): print("@pytest.fixture(scope='class')") @pytest.fixture(scope='module') def module_scope(): print("@pytest.fixture(scope='module')") @pytest.fixture(scope='package') def package_scope(): print("@pytest.fixture(scope='package')") @pytest.fixture(scope='session') def session_scope(): print("@pytest.fixture(scope='session')")
fixture也是可以互相继承的,但要遵循规则小继承大,function->class->mdule->session
import pytest @pytest.fixture(scope='module') def module_scope(session_scope): print("@pytest.fixture(scope='module')") @pytest.fixture(scope='class') def class_scope(module_scope): print("@pytest.fixture(scope='class')") @pytest.fixture(scope='function') def function_scope(class_scope): print("@pytest.fixture(scope='function')")
以上是fixture中scope作用范围的简述,小小总结可能会解决你的问题,也可能解决不了你的问题,但还是希望对您有所帮助,感谢阅读,欢迎来扰!未完待续.......
一直都在努力变好中,希望您也是,加油!