【Pytest】fixture

usage

Similar to the usage of setup and teardown, you can use any name and use it more flexibly.

The operation before yield is equivalent to setup, and the operation before yield is equivalent to teardown.

@pytest.fixture
def fixture_name():
    setup操作 
    yield 返回值
    teardown操作

scope

Use scope to control the scope of fixture, the default is function level.

function function level or method level
class class level
module module level
package package level
session session level

@pytest.fixture(scope="class")
def a():
    print("start")
    yield
    print("end")

class TestA:
    def test_a(self, a):
        assert True

Three calling methods

1. Directly pass in the fixture function name in the function or method.

@pytest.fixture()
def a():
    print("start")
    yield
    print("end")

class TestA:
    def test_a(self, a):
        assert True

2. Use the decorator @pytest.mark.usefixtures()

@pytest.fixture()
def a():
    print("start")
    yield
    print("end")

@pytest.mark.usefixtures("a")
class TestA:
    def test_a(self):
        assert True

3. Use multiple fixture methods

If a method or function wants to call multiple fixtures at the same time, you can use @pytest.mark.usefixture() to superimpose. Pay attention to the stacking order, put the ones executed first on the bottom layer, and the ones executed later on the upper layer.

@pytest.fixture()
def login():
    print("完成登录操作")
    token = "123456789"
    username = "zhangsan"
    yield token, username  #yield用法相当于return
    print("完成登出操作")

@pytest.fixture()
def a():
    print("start")
    yield
    print("end")

@pytest.mark.usefixtures("login")
@pytest.mark.usefixtures("a") #优先执行
class TestA:
    def test_a(self):
        assert True

4. The difference between passing directly into the function and using the decorator @pytest.mark.usefixtures().

When there is a return value after yield, the return value cannot be obtained using @pytest.mark.usefixtures(). Must be passed directly into the function.

@pytest.fixture(scope="function")
def login():
    print("完成登录操作")
    token = "123456789"
    username = "zhangsan"
    yield token, username  #yield用法相当于return
    print("完成登出操作")

def test_a(login):
    token, username = login
    print(token, username)

5. You can also use autouse automatically. The default value is False. When it is changed to True, all test functions and methods will automatically use this fixture function without calling it.

@pytest.fixture(scope="class", autouse=True)
def a():
    print("start")
    yield
    print("end")

class TestA:
    def test_a(self):
        assert True

Use of conftest

Write methods in the conftest.py configuration to achieve data sharing, no import is required, and it can be shared across files. 

Put the defined fixture method into conftest.py and place it in the public directory. The test code can directly call the fixture method in conftest.p without putting it in the same test file. In multi-person collaboration, it is possible to implement a shared method.

Guess you like

Origin blog.csdn.net/Yocczy/article/details/129289810