Super powerful, Pytest automated testing framework fixture passing parameter actual combat (case)


foreword

In order to improve reusability, we will use different fixtures when writing test cases, for example: 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 account cannot be written to death when logging in to the fixture, 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"== 账号是:{
      
      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" 测试用例的登录账号是:{
      
      login} ")

Results of the

collecting ... collected 2 items

10fixture_request.py::test_name[login_test_name is:pyy1] == 账号是:pyy1 ==
PASSED          [ 50%] 测试用例的登录账号是:pyy1 

10fixture_request.py::test_name[login_test_name is:polo] == 账号是:polo ==
PASSED          [100%] 测试用例的登录账号是:polo 

Adding the indirect=True parameter is to execute login as a function instead of a parameter, and pass data into the function as a parameter; def test_name
(login), where login is the value returned by the fixture;

Case 2: Multiple parameters

@pytest.fixture()
def logins(request):
    param = request.param
    print(f"账号是:{
      
      param['username']},密码是:{
      
      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"账号是:{
      
      logins['username']},密码是:{
      
      logins['pwd']}")

Results of the

10fixture_request.py::test_name_pwd[logins0] 账号是:name1,密码是:pwd1
PASSED                      [ 50%]账号是:name1,密码是:pwd1

10fixture_request.py::test_name_pwd[logins1] 账号是:name2,密码是:pwd2
PASSED                      [100%]账号是:name2,密码是:pwd2

If you need to pass multiple parameters, you need to pass them through a dictionary

Case 3: multiple fixtures (only one decorator)

This is more commonly used

# 多个fixture
@pytest.fixture(scope="module")
def input_user(request):
    user = request.param
    print("登录账户:%s" % user)
    return user


@pytest.fixture(scope="module")
def input_psw(request):
    psw = request.param
    print("登录密码:%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] 登录账户:name1
登录密码:pwd1
PASSED               [ 50%]fixture返回的内容: name1 pwd1

10fixture_request.py::test_more_fixture[name2-pwd2] 登录账户:name2
登录密码:pwd2
PASSED               [100%]fixture返回的内容: name2 pwd2

Case 4: Multiple fixtures (overlay decorators)

# 多个fixture
@pytest.fixture(scope="function")
def input_user(request):
    user = request.param
    print("登录账户:%s" % user)
    return user


@pytest.fixture(scope="function")
def input_psw(request):
    psw = request.param
    print("登录密码:%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] 登录账户:name1
登录密码:pwd1
PASSED               [ 25%]fixture返回的内容: name1 pwd1

10fixture_request.py::test_more_fixture[pwd1-name2] 登录账户:name2
登录密码:pwd1
PASSED               [ 50%]fixture返回的内容: name2 pwd1

10fixture_request.py::test_more_fixture[pwd2-name1] 登录账户:name1
登录密码:pwd2
PASSED               [ 75%]fixture返回的内容: name1 pwd2

10fixture_request.py::test_more_fixture[pwd2-name2] 登录账户:name2
登录密码:pwd2
PASSED               [100%]fixture返回的内容: name2 pwd2

Number of test cases=2*2=4

The following is the most complete software test engineer learning knowledge architecture system diagram in 2023 that I compiled

1. From entry to mastery of Python programming

Please add a picture description

2. Interface automation project actual combat

Please add a picture description

3. Actual Combat of Web Automation Project

Please add a picture description

4. Actual Combat of App Automation Project

Please add a picture description

5. Resume of first-tier manufacturers

Please add a picture description

6. Test and develop DevOps system

Please add a picture description

7. Commonly used automated testing tools

Please add a picture description

Eight, JMeter performance test

Please add a picture description

9. Summary (little surprise at the end)

Go forward bravely and aim high. Keep struggling and chasing your dreams. Every effort is an opportunity for self-breakthrough, persevere, and success will surely bloom under your feet. Believe in yourself, you can overcome all difficulties and create your own brilliant life!

Go forward bravely, not afraid of hardships; do your best, chase your dreams; persevere, fearless; strive endlessly, create brilliance; keep your feet on the ground, climb to the top; work hard to achieve yourself; be proactive and harvest the future; full of confidence, do nothing No; life is short and the struggle is endless.

Only persistent efforts can dispel the haze of confusion and move towards a brilliant tomorrow. No matter how bumpy the road ahead is, with dreams in mind and constant struggle; as long as you keep your feet on the ground, you will surely reap success. Believe in yourself, go beyond the limit, go forward!

Guess you like

Origin blog.csdn.net/m0_70102063/article/details/131438883