Pytest test framework series - detailed explanation of pytest repeat case plug-in

Posted by monkey_05_06 on Mon, 24 Jan 2022 07:21:01 +0100

preface

Scenario: sometimes all or part of the use cases that we need to execute want to be executed repeatedly, and we can solve it as we look down.

Pytest provides us with a good plug-in pytest repeat, which can be implemented simply by adding some parameters. Let's learn the relevant functions of this plug-in completely.

Detailed explanation of pytest repeat

Usage:

  • Add parameter -- count=num
  • Use the decorator @ pytest. On use cases that need to be repeated mark. repeat(num)

Add parameter method -- count=num

Example:

# !/usr/bin/python3
# _*_coding:utf-8 _*_
""""
# @Time  :2021/7/10 21:05
# @Author  : king
# @File    :test_repeat.py
# @Software  :PyCharm
# @blog     :https://blog.csdn.net/u010454117
# @WeChat Official Account: [test road notes]
"""
import pytest

def test_repeat():
    assert True

def test_repeat_01():
    assert True

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

Execute pytest - V -- count = 2 test in the command window_ repeat. Py command to view the results:

Use decorator method @ pytest mark. repeat(num)

Code example:

# !/usr/bin/python3
# _*_coding:utf-8 _*_
""""
# @Time  :2021/7/10 21:05
# @Author  : king
# @File    :test_repeat.py
# @Software  :PyCharm
# @blog     :https://blog.csdn.net/u010454117
# @WeChat Official Account: [test road notes]
"""
import pytest

@pytest.mark.repeat(2)
def test_repeat():
    assert True

def test_repeat_01():
    assert True

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

Execute pytest - V test in the command window_ repeat. Py command to view the results:

Question: if you want to set the time to run the use case repeatedly, how?

For example: repeat the requirement after running a module, or repeat the requirement after running all use cases.

Check the repeat source code and find a parameter that can be configured -- repeat scope:

Description: - the - repeat scope parameter can only be used with -- count=num

def pytest_addoption(parser):
    parser.addoption(
        '--count',
        action='store',
        default=1,
        type=int,
        help='Number of times to repeat each test')

    parser.addoption(
        '--repeat-scope',
        action='store',
        default='function',
        type=str,
        choices=('function', 'class', 'module', 'session'),
        help='Scope for repeating tests')

– the parameters of repeat scope include choices = ('function ',' class', 'module', 'session') and the default is function

  • function: each use case runs repeatedly, and the next use case will be executed after all runs are completed
  • Class: take the class as the unit of use case collection, repeat the use case in the class, and execute the next use case
  • Module: repeat the use cases in the module in the unit of module to execute the next one
  • Session: repeat the whole test session and execute the test case for the second time

Let's look at -- repeat scope = session

Example code:

# !/usr/bin/python3
# _*_coding:utf-8 _*_
""""
# @Time  :2021/7/10 21:05
# @Author  : king
# @File    :test_repeat.py
# @Software  :PyCharm
# @blog     :https://blog.csdn.net/u010454117
# @WeChat Official Account: [test road notes]
"""
import pytest

def test_repeat():
    assert True

def test_repeat_01():
    assert True

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

Execute pytest - V -- count = 2 -- repeat scope = session test in the command window_ repeat. Py command to view the results: executed in a session and repeated in a session

Look at another -- repeat scope = class

Example code:

# !/usr/bin/python3
# _*_coding:utf-8 _*_
""""
# @Time  :2021/7/10 21:05
# @Author  : king
# @File    :test_repeat.py
# @Software  :PyCharm
# @blog     :https://blog.csdn.net/u010454117
# @WeChat Official Account: [test road notes]
"""
import pytest

class TestRepeat:
    def test_repeat(self):
        assert True

class TestRepeat01:
    def test_repeat01(self):
        assert True

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

Execute pytest - V -- count = 2 -- repeat scope = class test in the command window_ repeat. Py command, view the result: execute in class and repeat the use case

Look at another -- repeat scope = module

The sample code is the same as above. Execute pytest - V -- count = 2 -- repeat scope = module test in the command window_ repeat. Py command, view the result: execute in modules, and repeat the use case

summary

  • Repeat the execution of the use case by adding the parameter -- count=num and using the decorator method @ pytest mark. Repeat (Num) mode
  • The added parameter -- count=num can be used in conjunction with -- repeat scope = scope to control the time of repeated execution of the use case
  • Decorator mode @ pytest mark. Repeat (Num) mode -- repeat scope = the scope control repeat execution time is invalid
  • At that time, when using the add parameter -- count=num to execute, the repeated use cases of the decorator will not take effect, and -- count=num takes precedence

The above content is purely personal understanding. If there is any deficiency, you are welcome to correct it. Please indicate the source for reprint!

If you feel that the article is good, welcome to WeChat official account, WeChat official account push the related test technology articles everyday.