Pytest series (10)-detailed use of parameter request in fixture

If you want to learn Pytest from scratch, you can check out this series of articles!

https://www.cnblogs.com/poloyy/category/1690628.html

 

Foreword

  • In order to improve reusability, we will use different fixtures when writing test cases, such as: the most common login operation, the precondition of most use cases is login
  • Assuming that different use cases want to log in to different test accounts, then the login fixture cannot write the account dead, and the login operation needs to be completed by passing parameters

 

Case 1: Pass a single parameter

import pytest


@pytest.fixture()
def login(request):
    name = request.param
     print (f " == account number is: {name} == " )
     return name


data = ["pyy1", "polo"]
ids = [f"login_test_name is:{name}" for name in data]


@ pytest.mark.parametrize ( " login " , data, ids = ids, indirect = True)
 def test_name (login):
     print (f " The login account for the test case is: {login} " )

Results of the

collecting ... collected 2 items

10fixture_request.py::test_name[login_test_name is : pyy1] == The account number is: pyy1 == 
PASSED [ 50% ] The login account for the test case is: pyy1

10fixture_request.py::test_name[login_test_name is : polo] == Account is: polo == 
PASSED [ 100%] The login account for the test case is: polo

Knowledge point

  • The indirect = True   parameter is added   to execute login as a function, not a parameter, and to pass data as a parameter to the function
  • def test_name (login)  , where login is to get the value returned by fixture

 

Case 2: Multiple parameters

@pytest.fixture()
def logins(request):
    param = request.param
     print (f " Account: {param ['username']}, password: {param ['pwd']} " )
     return param


data = [
    {"username": "name1", "pwd": "pwd1"},
    {"username": "name2", "pwd": "pwd2"},
]


@ pytest.mark.parametrize ( " logins " , data, indirect = True)
 def test_name_pwd (logins):
     print (f "The account number is: {logins ['username']}, the password is: {logins ['pwd']} " )

Results of the

10fixture_request.py::test_name_pwd[logins0] The account is: name1, the password is: pwd1
PASSED [ 50% ] The account number is: name1, the password is: pwd1

10fixture_request.py::test_name_pwd[logins1] The account is: name2, the password is: pwd2
PASSED [ 100%] The account is: name2, the password is: pwd2

Knowledge point

If you need to pass multiple parameters, you need to pass the dictionary

 

Case 3: Multiple fixtures (only add a decorator)

This is more commonly used

# 多个fixture
@pytest.fixture(scope="module")
def input_user(request):
    user = request.param
     print ( " login account:% s " % user)
     return user


@pytest.fixture(scope="module")
def input_psw(request):
    psw = request.param
     print ( " login password:% s " % psw)
     return psw


data = [
    ("name1", "pwd1"),
    ("name2", "pwd2")
]


@pytest.mark.parametrize("input_user,input_psw", data, indirect=True)
def test_more_fixture(input_user, input_psw):
    print("fixture返回的内容:", input_user, input_psw)

Results of the

10fixture_request.py::test_more_fixture[name1- pwd1] login account: name1
Login password: pwd1
PASSED [ 50% ] Content returned by fixture: name1 pwd1

10fixture_request.py::test_more_fixture[name2 - pwd2] login account: name2
Login password: pwd2
PASSED [ 100%] What the fixture returns: name2 pwd2

 

Case 4: Multiple fixtures (overlay decorator)

# 多个fixture
@pytest.fixture(scope="function")
def input_user(request):
    user = request.param
     print ( " login account:% s " % user)
     return user


@pytest.fixture(scope="function")
def input_psw(request):
    psw = request.param
     print ( " login password:% s " % psw)
     return psw


name = ["name1", "name2"]
pwd = ["pwd1", "pwd2"]


@pytest.mark.parametrize("input_user", name, indirect=True)
@pytest.mark.parametrize("input_psw", pwd, indirect=True)
def test_more_fixture(input_user, input_psw):
    print("fixture返回的内容:", input_user, input_psw)

Results of the

10fixture_request.py::test_more_fixture[pwd1- name1] Login account: name1
Login password: pwd1
PASSED [ 25% ] Content returned by fixture: name1 pwd1

10fixture_request.py::test_more_fixture[pwd1 - NAME2] login account: name2
Login password: pwd1
PASSED [ 50% ] Content returned by fixture: name2 pwd1

10fixture_request.py::test_more_fixture[pwd2 - NAME1] login account: name1
Login password: pwd2
PASSED [ 75% ] Content returned by fixture: name1 pwd2

10fixture_request.py::test_more_fixture[pwd2 - NAME2] login account: name2
Login password: pwd2
PASSED [ 100%] What the fixture returns: name2 pwd2

Number of test cases = 2 * 2 = 4

 

Guess you like

Origin www.cnblogs.com/poloyy/p/12685948.html