Ultra-detailed, Python automated test Allure report parameterized dynamic generation title (actual combat)


foreword

By default, the test case title on the allure report is not set to the default test case name, which is not very readable;

When combined with @pytest.mark.parametrize parameterization to complete the data drive, if the title is written hard, the readability is not high, and the dynamic generation of the title is the most commonly used with high readability.

Parametric Untitled

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import allure
import pytest


@pytest.fixture()
def login(request):
    """登录"""
    param = request.param
    print(f"账号是:{
      
      param['username']},密码是:{
      
      param['pwd']}")
    # 返回
    return {
    
    "code": 0, "msg": "success!"}

datas = [
    {
    
    "username": "name1", "pwd": "pwd1"},
    {
    
    "username": "name2", "pwd": "pwd2"},
    {
    
    "username": "name3", "pwd": "pwd3"}
]

@allure.story('登录功能')
@pytest.mark.parametrize('login', datas, indirect=True)
def test_login1(login):
    """
    登录测试用例1
    """
    assert login['code'] == 0

The title is the method name + parameterized data, which is not very readable.

Parameterization has title hardcoded

Test code
Add a @allure.title to the above test code

@allure.story('登录功能')
@allure.title('登录测试用例2')
@pytest.mark.parametrize('login', datas, indirect=True)
def test_login2(login):
    """
    登录测试用例2
    """
    assert login['code'] == 0

Because parameterization can generate three use cases, all three use cases use the same title, and the readability is not very high

Parameterized use ids

test code

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import allure
import pytest


@pytest.fixture()
def login(request):
    """登录"""
    param = request.param
    print(f"账号是:{
      
      param['username']},密码是:{
      
      param['pwd']}")
    # 返回
    return {
    
    "code": 0, "msg": "success!"}

datas = [
    {
    
    "username": "name1", "pwd": "pwd1"},
    {
    
    "username": "name2", "pwd": "pwd2"},
    {
    
    "username": "name3", "pwd": "pwd3"}
]

ids = [
    "username is name1,pwd is pwd1",
    "username is name2,pwd is pwd2",
    "username is name3,pwd is pwd3"
]

@allure.story('登录功能')
@pytest.mark.parametrize('login', datas, ids=ids, indirect=True)
def test_login1(login):
    """
    登录测试用例1
    """
    assert login['code'] == 0

Parameterize dynamically generated headers

test code

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import allure
import pytest


@pytest.fixture()
def login(request):
    """登录"""
    param = request.param
    print(f"账号是:{
      
      param['username']},密码是:{
      
      param['pwd']}")
    # 返回
    return {
    
    "code": 0, "msg": "success!"}


datas = [
    {
    
    "username": "name1", "pwd": "pwd1"},
    {
    
    "username": "name2", "pwd": "pwd2"},
    {
    
    "username": "name3", "pwd": "pwd3"}
]

data2 = [
    ("name1", "123456"),
    ("name2", "123456"),
    ("name3", "123456")
]


@allure.story('分别传值')
@allure.title('登录测试用例2-账号是:{username}-密码是:{pwd}')
@pytest.mark.parametrize('username,pwd', data2)
def test_login1(username, pwd):
    """
    登录测试用例1
    """
    print(username, pwd)


@allure.story('字典参数化')
@allure.title('登录测试用例2-{dict}')
@pytest.mark.parametrize('dict', datas)
def test_login2(dict):
    """
    登录测试用例1
    """
    print(dict['username'], dict['pwd'])


@allure.story('传值进fixture')
@allure.title('登录测试用例2{login}')
@pytest.mark.parametrize('login', datas, indirect=True)
def test_login3(login):
    """
    登录测试用例2
    """
    assert login['code'] == 0

allure report

Please add a picture description

If the input is a dictionary, the full dictionary value will be displayed

The optimal scheme of parameterized dynamic generation title

test code

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import allure
import pytest

data = [
    ("name1", "123456", "name1 登录成功"),
    ("name2", "123456", "name2 登录失败"),
    ("name3", "123456", "name3 登录成功")
]


@allure.story('分别传值')
@allure.title('登录测试用例-{title}')
@pytest.mark.parametrize('username,pwd,title', data)
def test_login1(username, pwd, title):
    """
    登录测试用例1
    """
    print(username, pwd)

Advantages:
you can customize various titles;
a single value to maintain the title value;
better readability and easy maintenance;

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)

Only with unremitting efforts can we create infinite possibilities, and only with a hard-working life can we write a brilliant chapter. Believe in yourself and go forward bravely, you will be able to realize your dreams and achieve an extraordinary future!

Only by working hard can we surpass ourselves yesterday and pursue the glory of our dreams; only by working hard can we find opportunities for breakthroughs in difficult situations; only by persevering can we create our own brilliant life. Believe in yourself and go forward!

Only by constantly surpassing one's own efforts can the dream shine; only by unremitting struggle can the best future be realized. No matter how difficult it is, as long as you have faith and move forward bravely, you will surely achieve extraordinary things!

Guess you like

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