Scope of fixture in pytest

1. Definition

There is a parameter scope in the fixture of pytest, and its scope has five, namely: function, class, module, and session

function: Called before each method starts, method level

class: will be called once before each class starts, class level

module: Called once before each module (py file) starts, module level

session: a session (multiple modules) will be called once before starting, package level

二. scope="function"

Define a test_function_scope.py, write the following code and run it:

import pytest 

@ pytest.fixture (scope = " function " )
 def function_scope ():
     print ( " run before each method starts " ) 
    msg = " test function_scope " 
    return msg 


def test_function_scope_1 (function_scope):
     assert function_scope == " test function_scope " 


def test_function_scope_2 (function_scope): assert function_scope == " test function_scope " 


#Run
    
result ============================= test session starts ================== =========== platform win32 -Python 3.7.3, pytest-5.1.2, py-1.8.0, pluggy-0.12.0- D: \ program \ Python37 \ python.exe cachedir : .pytest_cache rootdir: E: \ virtual_workshop \ pytest - demo \ test_demo collecting ... collected 2 items test_function_scope.py::test_function_scope_1 run once before each method starts [ 50% ] test_function_scope.py::test_function_scope_2 each method starts Before running PASSED [ 100% ] ============================== 2 passed in 0.03s ======= =======================

三. scope="class"

Define a test_class_scope.py, write the following code and run it:

import pytest

@pytest.fixture(scope="class")
def class_scope():
    print("每个类开始之前运行一次")
    msg = "测试class_scope"
    return msg


class TestClassScope1():
    def test_class_scope_1(self, class_scope):
        assert class_scope == "测试class_scope"


class TestClassScope2():
    def test_class_scope_2(self, class_scope):
        assert class_scope == "Test class_scope " 


# operation results 
============================= test session starts ============ ================= 
platform win32 -Python 3.7.3, pytest-5.1.2, py-1.8.0, pluggy-0.12.0- D: \ program \ Python37 \ python.exe 
cachedir: .pytest_cache 
rootdir: E: \ virtual_workshop \ pytest - demo \ test_demo 
collecting ... collected 2 items 

test_class_scope.py::TestClassScope1::test_class_scope_1 Run 
PASSED [ 50% ] 
test_class_scope before each class starts .py :: TestClassScope2 :: test_class_scope_2 Run 
PASSED once before each class [ 100% ]

 ============================== 2 passedin 0.03s ==============================

四. scope="module"

Define a test_module_scope.py, write the following code and run it:

import pytest

@pytest.fixture(scope="module")
def module_scope():
    print("每个模块开始之前运行一次")
    msg = "测试module_scope"
    return msg


class TestClassScope1():
    def test_module_scope_1(self, module_scope):
        assert module_scope == "测试module_scope"


class TestClassScope2():
    def test_module_scope_2(self, module_scope):
        assert module_scope == " Test module_scope " 



# operation results 
============================= test session starts =========== ================== 
platform win32 -Python 3.7.3, pytest-5.1.2, py-1.8.0, pluggy-0.12.0- D: \ program \ Python37 \ python.exe 
cachedir: .pytest_cache 
rootdir: E: \ virtual_workshop \ pytest - demo \ test_demo2 
collecting ... collected 2 items 

test_module_scope.py::TestClassScope1::test_module_scope_1 Run 
PASSED once before each module starts [ 50% ] 
test_module_scope.py::TestClassScope2::test_module_scope_2 PASSED [ 100% ]

 ============================== 2 passed in 0.03s ==============================

五. scope="session"

Define a conftest.py under the test_demo3 package, the code is as follows:

import pytest 

@ pytest.fixture (scope = " session " )
 def session_scope ():
     print ( " Call once before the start of a session (multiple modules) " ) 
    msg = " Call once before the start of a session (multiple modules) " 
    return msg

Then define two modules, one is test_session_scope1.py and one is test_session_scope2.py

#test_session_scope1.py
def test_session_scope1(session_scope):
    assert session_scope == "测试session_scope"


#test_session_scope2.py
def test_session_scope2(session_scope):
    assert session_scope == "测试session_scope"


#运行结果
============================= test session starts =============================
platform win32 -- Python 3.7.3, pytest-5.1.2, py-1.8.0, pluggy-0.12.0 -- D:\program\Python37\python.exe
cachedir: .pytest_cache
rootdir: E:\virtual_workshop\pytest-demo\test_demo3
collecting ... collected2 items 

test_session_scope1.py::test_session_scope1 Run 
PASSED once before a session (multiple modules) [ 50% ] 
test_session_scope2.py::test_session_scope2 PASSED [ 100% ]

 =============== =============== 2 passed in 0.03s ==============================

6. The instantiation order

session> module> class> function. A good example is from this blog :

import pytest

order = []

@pytest.fixture(scope="session")
def s1():
    order.append("s1")


@pytest.fixture(scope="module")
def m1():
    order.append("m1")


@pytest.fixture
def f1(f3, a1):
    # 先实例化f3, 再实例化a1, 最后实例化f1
    order.append("f1")
    assert f3 == 123


@pytest.fixture
def f3 (): 
    order.append ( " f3 " ) 
    a = 123
     yield a 


@ pytest.fixture 
def a1 (): 
    order.append ( " a1 " ) 


@ pytest.fixture 
def f2 (): 
    order.append ( " f2 " ) 


def test_order (f1, m1, f2, s1):
     # m1, s1 are after f1, but because the scope is large, it will preferentially instantiate 
    assert order == [ " s1 " , " m1 " , " f3 " , "a1", "f1", "f2"]

 

Guess you like

Origin www.cnblogs.com/my_captain/p/12714119.html