[Automated testing] Pytest framework - skip tests and retry on failure

1. Pytest skips test cases

During the execution of automated tests, we often have this situation: some use cases cannot be executed due to functional blockage, unrealized or environmental problems, etc. If we comment out or delete these test cases, recovery operations may be required later , then we can configure to skip these use cases.

PytestThere are two ways to skip tests in the testing framework: skipand skipif.

(1) Unconditional skipskip

skipmethod to unconditionally skip test cases.

How to use: @pytest.mark.skipMark on the test cases that need to be skipped.

(2) Conditionally skipskipif

skipifThe method is to conditionally skip the test case, and the condition is true to skip.

How to use: @pytest.mark.skipif( condition=condition for skipping , reason=reason for skipping),

Mark on test cases that need to be skipped.

  • Parameters condition: The skip condition, if it is True, skip the test, if it is False, continue to execute the test, the default is True.
  • Parameter reason: The reason for skipping the label, a required parameter.

(3) practice

"""
1.学习目标
    掌握pytest中跳过测试方法
2.操作步骤
    skipif(condition=判断条件,reason=跳过原因)
    使用时放置在需要跳过的用例之前
    @pytest.mark.skipif(条件,原因)  # 当条件为真,跳过执行
3.需求
"""
# 导入pytest
import pytest


# 编写测试用例
def login_data():
    return "jerry", "123456"


# 无条件跳过
@pytest.mark.skip
def test_register():
    """注册用例"""
    print("注册步骤")
    assert False


# 当条件为真,跳过测试
@pytest.mark.skipif(login_data()[0] == "jerry", reason="jerry用户不存在")
def test_login():
    """不记住密码登录"""
    username = login_data()[0]
    password = login_data()[1]
    print(f"输入用户名{username}")
    print(f"输入密码{password}")
    print("点击登录按钮")
    assert username == "jerry"


def test_shopping():
    """购物下单"""
    print("购物流程")
    assert True


if __name__ == '__main__':
    pytest.main()

"""
执行结果:跳过一个用例 : 1通过,2跳过

test_pytest_01.py::test_register 
test_pytest_01.py::test_login 
test_pytest_01.py::test_shopping 

======================== 1 passed, 2 skipped in 0.04s =========================

Process finished with exit code 0
SKIPPED (unconditional skip)
Skipped: unconditional skip
SKIPPED (jerry用户不存在)
Skipped: jerry用户不存在
购物流程
PASSED
"""
# 注:跳过的用例测试结果标识为s

2. Pytest fails to retry

Pytest failure retry means that when a test script is executed, if the execution result of a test case fails, the test case is re-executed.

premise:

The pytest testing framework fails to retry and requires pytest-rerunfailuresa plugin to be downloaded.

Installation method:pip install pytest-rerunfailures

The way Pytest implements failure retry:

Method 1: Use it on the command line or in the main() function

pytest.main(['-vs','test_a.py','--reruns=2'])(This method has not been successfully implemented, and it may be a problem with your own environment)

or:

pytest -vs ./test_a.py --reruns 2 --reruns-delay 2(Can)

Indicates: retry 2 times on failure, and wait for 2 seconds before each retry.

Description: rerunsIt is the number of reruns, reruns_delayand it is the interval time, the unit is s

Method 2: pytest.iniUse in the configuration file (recommended)

Add retry parameter in pytest.iniconfiguration fileaddoptsreruns

[pytest]
addopts = -s --reruns 2 --reruns-delay 2
testpaths = scripts
python_files = test_01.py
python_classes = Test*
python_functions = test*

Example: use the second way:

"""
1.学习目标
    掌握pytest中用例失败重试方法
2.操作步骤
    2.1 安装 pytest-rerunfailures
        pip install pytest-rerunfailures
    2.2 使用 在pytest.ini文件中,添加一个命令行参数  
        --reruns n # n表示重试次数
3.需求
"""
# 1.导入pytest
import pytest


# 2.编写测试用例
@pytest.mark.run(order=2)
def test_login():
    """登录用例"""
    print("登录步骤")
    assert "abcd" in "abcdefg"


@pytest.mark.run(order=1)
def test_register():
    """注册用例"""
    print("注册步骤")
    assert False


@pytest.mark.run(order=4)
def test_shopping():
    """购物下单"""
    print("购物流程")
    assert True


@pytest.mark.run(order=3)
def test_cart():
    """购物车用例"""
    print("购物车流程")
    assert True


if __name__ == '__main__':
    pytest.main(['-vs', 'test_01.py', '--reruns=2'])

# pytest ./pytest_demo/test_01.py --reruns 10 --reruns-delay 1
#
"""
执行结果:注意有两个:2 rerun
==================== 1 failed, 3 passed, 2 rerun in 0.09s =====================

test_01.py::test_register 注册步骤
RERUN
test_01.py::test_register 注册步骤
RERUN
test_01.py::test_register 注册步骤
FAILED
pytest_demo\test_01.py:20 (test_register)
@pytest.mark.run(order=1)
    def test_register():
        ""注册用例""
        print("注册步骤")
>       assert False
E       assert False

test_01.py:25: AssertionError
登录步骤
PASSED购物车流程
PASSED购物流程
PASSED
"""

Note: If the setting fails to retry 5 times, and it succeeds during the retry process, there is no need to retry all 5 times, and you need to pay attention here. 


END meager strength

Finally, I would like to thank everyone who has read my article carefully. Looking at the fans’ growth and attention all the way, there is always a need for reciprocity. Although it is not a very valuable thing, you can take it away if you need it:

These materials should be the most comprehensive and complete preparation warehouse for [software testing] friends. This warehouse has also accompanied tens of thousands of test engineers through the most difficult journey, and I hope it can help you too!

加入我的软件测试交流群:110685036免费获取~(同行大佬一起学术交流,每晚都有大佬直播分享技术知识点)

Software testing interview applet

The software test question bank maxed out by millions of people! ! ! Who is who knows! ! ! The most comprehensive quiz mini program on the whole network, you can use your mobile phone to do the quizzes, on the subway or on the bus, roll it up!

The following interview question sections are covered:

1. Basic theory of software testing, 2. web, app, interface function testing, 3. network, 4. database, 5. linux

6. web, app, interface automation, 7. performance testing, 8. programming basics, 9. hr interview questions, 10. open test questions, 11. security testing, 12. computer basics

method of obtaining:

 

 

Guess you like

Origin blog.csdn.net/jiangjunsss/article/details/130642266