pytest test framework (2) --- fixture introduction

table of Contents

One, the definition of the fixture function

Second, the call of the fixture function

Three, the return of the fixture function

Fourth, the use of conftest.py

Five, use cases


One, the definition of the fixture function

Defining a function as a fixture is very simple, just add "@pytest.fixture" before the function declaration, and the parameters are as follows:

     fixture(scope="function", params=None, autouse=False, ids=None, name=None)

     scope : There are four level parameters "function" (default), "class", "module" or "session":

         ☆ Multiple session files can be called once, and can be called across .py files

         ☆ Module is called once for each .py file

         ☆ class is called once for each class

         ☆ function every function or method will be called

     params : an optional parameter list, the default is None. When it is not None, the fixture will be called and executed once for each value in params, just like executing a for loop to traverse the value in params once;

     autouse : When the default is False, you can choose the other two ways to call the fixture. When set to True, all test cases in a session will automatically call this fixture;

     ids : A list of each string id, each string corresponds to params so that they are part of the test ID. If no ID is provided, they will be automatically generated from params;

     name : the name of the fixture, representing the name of the decorated function.

 

Second, the call of the fixture function

After the fixture function is defined, if you want to call it in a test case, there are three ways to call it:

    ☆ Direct call;

    ☆ Use fixture call, add "@pytest.mark.usefixtures("name")" before the test case;

    ☆ Use the fixture's autouse parameter to call, set the autose parameter to True, and all use cases under the session will automatically call it.

 

Three, the return of the fixture function

The fixture can return a value, a tuple, a list or a dictionary.

 

Fourth, the use of conftest.py

If multiple use cases need to call the same function. We can write fixtures into the conftest.py configuration file to realize data sharing, and at the same time facilitate the unified management of these common functions.

      The configuration of conftest.py needs to pay attention to the following points:

      ☆ The name of the conftest.py configuration script is fixed and cannot be changed;

      ☆ conftest.py and the running use case should be in the same directory, and there should be an __init__.py file;

      ☆ No need to import conftest.py, the use case will be automatically searched.

 

Five, use cases

conftest.py and test_fixture.py are placed in the same directory, and test_fixture.py is the test case.

    The content of conftest.py is as follows:

import pytest

@pytest.fixture()
def user():
    print("获取用户名")
    a = "hillwill"
    b = "wxt"
    return (a,b)

     The content of test_fixture.py is as follows:

import pytest

def test_1(user):
    user1 = user[0]
    print("用户名1:%s" % user1)
    assert user1 == "hillwill"

def test_2(user):
    user2 = user[1]
    print("用户名2:%s" % user2)
    assert user1 == "wxt"

if __name__ == "__main__":

    pytest.main(["-s", "test_fixture.py"])

 

 

 

Guess you like

Origin blog.csdn.net/wxt_hillwill/article/details/111609285