The advanced parametric Pytest

Foreword

unittest unit testing framework for data-driven testing using DDT, then as a more powerful and more flexible framework for how Pytest may not have the concept of data-driven it? In fact Pytest using @ pytest.mark.parametrize decorators to achieve data-driven testing, then today we would say in simple terms it is how data-driven testing

A single parameter

"""
------------------------------------
@Time : 2019/7/25 19:18
@Auth : linux超
@File : test_parametrize.py
@IDE  : PyCharm
@Motto: Real warriors,dare to face the bleak warning,dare to face the incisive error!
@QQ   : [email protected]
@GROUP: 878565760
------------------------------------
"""
import pytest

data = [1, 2]


@pytest.mark.parametrize('a', data)
def test_parametrize(a):
    print('\n被加载测试数据为\n{}'.format(a))


if __name__ == '__main__':
    pytest.main(['-s'])

Export

============================= test session starts ================== =========== 
Platform win32 - Python 3.7.2, pytest-4.3.1, 1.8.0-Py, pluggy-0.9 .0 
RootDir: E: \ CnblogCode \ pytest_parametrize, inifile: 
plugins: rerunfailures -7.0, Metadata-1.8.0, HTML-1.20 .0 
Collected 2 items 

test_parametrize.py 
is loaded to the test data
 a 
. 
is loaded test data
 2 
.

 =============== 2 passed =========== in 0.16 seconds The =========================== 

Process Finished with Exit code 0

Explanation

If the test only one parameter needed, we store the sequence of nested unordered list data, @ pytest.mark.parametrize ( ' A ' , Data) The first argument decorator need only one variable in each of the reception list elements, the second parameter data stored in the list, then the test cases need to use the same name as the received test data string (a example) and the number of elements in the list will be generated and the number of test case execution

a set of data

"" " 
------------------------------------ 
@time: 2019/7/25 19:18 
@Auth: linux super 
@file: test_parametrize.py 
@IDE: PyCharm 
@Motto: Real Warriors, Dare to face at The Bleak warning, Dare to face at The Incisive error! 
@ QQ: [email protected] 
@GROUP: 878 565 760 
--- --------------------------------- 
"" " 
Import pytest 

Data = [ 
    [ . 1, 2,. 3 ], 
    [ 4, 5, 9 ] 
]   # listing nested list 
# data_tuple = [ 
#      (. 1, 2,. 3), 
#      (4, 5, 9) 
# ] # listing nested tuple


pytest.mark.parametrize @ ( ' A, B, Expect ' , Data)
 DEF test_parametrize_1 (A, B, Expect): a parameter receiving a data #
     Print ( ' \ n-test data \ n {}, {}, { } ' .format (a, B, Expect)) 
    Actual = a + B
     Assert Actual == Expect 


@ pytest.mark.parametrize ( ' value ' , data)
 DEF test_parametrize_2 (value): # a parameter receiving a set of data
     Print ( ' \ n-test data \ n-{} ' .format (value)) 
    Actual = value [0] value + [. 1 ]
     Assert actual == value[2]


if __name__ == '__main__':
    pytest.main(['-s'])

Export

============================= test session starts ================== =========== 
Platform win32 - Python 3.7.2, pytest-4.3.1, 1.8.0-Py, pluggy-0.9 .0 
RootDir: E: \ CnblogCode \ pytest_parametrize, inifile: 
plugins: rerunfailures -7.0, Metadata-1.8.0, HTML-1.20 .0 
Collected . 4 items 

test_parametrize.py 
test data
 1, 2, 
. 
the test data is
 4,5,9 
. 
test data 
[ 1, 2, 3 ] 
. 
test data 
[ . 4,. 5,. 9 ] 
.

 ========================== passed. 4 in 0.17 seconds The ======== ===================

Process finished with exit code 0

Explanation

When a plurality of test cases required parameters, need to use the list (nested tuple & nested list) nested sequence to store the test data

Decorator @ pytest.mark.parametrize ( ) may use a single variable to receive data, may be received using a plurality of variables, the same, test functions need to be consistent with their

Acquires each data received when a single variable, the test data is transmitted to the internal test function for each element in the list using an index or a small list of required manner

When receiving a plurality of variable data, each element of each variable receives a small list or tuple

How many nested list listing a plurality of sets or tuples of small, measuring how many test cases generated

Illustrates a correspondence relationship

Combined data

"""
------------------------------------
@Time : 2019/7/25 19:18
@Auth : linux超
@File : test_parametrize.py
@IDE  : PyCharm
@Motto: Real warriors,dare to face the bleak warning,dare to face the incisive error!
@QQ   : [email protected]
@GROUP: 878565760
------------------------------------
"""
import pytest

data_1 = [1, 2]  
data_2 = [3, 4]  


@pytest.mark.parametrize('a', data_1)
@pytest.mark.parametrize('b', data_2)
def test_parametrize_1(a, b):
    Print ( ' \ n-test data \ n-{}, {} ' .format (A, B)) 


IF  the __name__ == ' __main__ ' : 
    pytest.main ([ ' -s ' ])

Export

============================= test session starts ================== =========== 
Platform win32 - Python 3.7.2, pytest-4.3.1, 1.8.0-Py, pluggy-0.9 .0 
RootDir: E: \ CnblogCode \ pytest_parametrize, inifile: 
plugins: rerunfailures -7.0, Metadata-1.8.0, HTML-1.20 .0 
Collected . 4 items 

test_parametrize.py 
test data
 1,3 
. 
test data
 2,3 
. 
test data
 1,4 
. 
test data
 2,4 
.

 = 4 passed ========================= in 0.24 seconds The ==================== ======= 

Process Finished with Exit code 0

Explanation

The test results, we found that the implementation of the four test cases. The reason is not difficult to analyze the results of each data of the first set of data and each set of data for each data of the second binding

Example labeled with

May be directly labeled test, parametric identification may also be decorative (labeled with cases of failure or skipped)

Labeled unconditional skips (marked as failed as xfail, try it yourself)

"""
------------------------------------
@Time : 2019/7/25 19:18
@Auth : linux超
@File : test_parametrize.py
@IDE  : PyCharm
@Motto: Real warriors,dare to face the bleak warning,dare to face the incisive error!
@QQ   : [email protected]
@GROUP: 878565760
------------------------------------
"""
import pytest

data_1 = [
    [1, 2, 3],
    pytest.param(3, 4, 8, marks=pytest.mark.skip)
]


def add(a, b):
    return a + b


@pytest.mark.parametrize('a, b, expect', data_1)
def test_parametrize_1(a, b, expect):
    print('\n测试数据为\n{},{}'.format(a, b))
    assert add(a, b) == expect


if __name__ == '__main__':
    pytest.main(['-vs'])

Export

============================= test session starts =============================
platform win32 -- Python 3.7.2, pytest-4.3.1, py-1.8.0, pluggy-0.9.0 -- C:\Programs\Python\Python37-32\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.7.2', 'Platform': 'Windows-7-6.1.7601-SP1', 'Packages': {'pytest': '4.3.1', 'py': '1.8.0', 'pluggy': '0.9.0'}, 'Plugins': {'rerunfailures': '7.0', 'metadata': '1.8.0', 'html': '1.20.0'}, 'JAVA_HOME': 'D:\\JDK'}
rootdir: E:\CnblogCode\pytest_parametrize, inifile:
plugins: rerunfailures-7.0, metadata-1.8.0, html-1.20.0
collecting ... collected 2 items

test_parametrize.py::test_parametrize_1[1-2-3] 
测试数据为
1,2
PASSED
test_parametrize.py::test_parametrize_1[3-4-8] SKIPPED

===================== 1 passed, 1 skipped in 0.17 seconds =====================

Process finished with exit code 0

Explanation

The output shows collected two use cases, one by one to be skipped, when we do not want to execute a set of test data, we can mark skip or SKIPIF; when we expect a group of data is failed, we can mark xfail Wait

to sum up

Pytest achieve data-driven is the case of

Points to note

1. The first parameter is a decorator string argument list form "a, b, c" can not be written "a", "b", "c"

2. The test sequence of data to be stored in a variable type, a nested sequence of course be used dictionaries

Examples of supplements

"""
------------------------------------
@Time : 2019/7/25 19:18
@Auth : linux超
@File : test_parametrize.py
@IDE  : PyCharm
@Motto: Real warriors,dare to face the bleak warning,dare to face the incisive error!
@QQ   : [email protected]
@GROUP: 878565760
------------------------------------
"""
import pytest

data_1 = (
    {
        'user': 1,
        'pwd': 2
     },
    {
        'user': 3,
        ''pwd: 4
    }
)


@pytest.mark.parametrize('dic', data_1)
def test_parametrize_1(dic):
    print('\n测试数据为\n{}'.format(dic))


if __name__ == '__main__':
    pytest.main(['-s'])

Export

============================= test session starts ================== =========== 
Platform win32 - Python 3.7.2, pytest-4.3.1, 1.8.0-Py, pluggy-0.9 .0 
RootDir: E: \ CnblogCode \ pytest_parametrize, inifile: 
plugins: rerunfailures -7.0, Metadata-1.8.0, HTML-1.20 .0 
Collected 2 items 

test_parametrize.py 
test data 
{ ' User ' :. 1, ' pwd ' : 2 } 
. 
test data 
{ ' User ' :. 3, ' pwd ' : 4 } 
.

========================== 2 passed in 0.20 seconds ===========================

Process finished with exit code 0

Guess you like

Origin www.cnblogs.com/linuxchao/p/linuxchao-pytest-parametrize.html