pytest入門到放棄8--parametrize引數化原始碼介紹
阿新 • • 發佈:2020-08-19
parametrize 允許在測試函式或類中定義多組引數和fixtures
parametrize 原始碼如下:
def parametrize(self,argnames, argvalues, indirect=False, ids=None, scope=None): """ Add new invocations to the underlying test function using the list of argvalues for the given argnames. Parametrization is performed during the collection phase. If you need to setup expensive resources see about setting indirect to do it rather at test setup time. # 使用給定argnames的argValue列表向基礎測試函式新增新的呼叫。在收集階段執行引數化。:arg argnames: a comma-separated string denoting one or more argument names, or a list/tuple of argument strings. # 引數名:使用用逗號分隔的字串,或列表或元祖,表示一個或多個引數名 :arg argvalues: The list of argvalues determines how often a test is invoked with different argument values. If only one argname was specified argvalues is a list of values. If N argnames were specified, argvalues must be a list of N-tuples, where each tuple-element specifies a value for its respective argname. # 引數值:只有一個argnames,argvalues則是值列表。有N個argnames時,每個元祖對應一組argnames,所有元祖組合成一個列表:arg indirect: The list of argnames or boolean. A list of arguments' names (self,subset of argnames). If True the list contains all names from the argnames. Each argvalue corresponding to an argname in this list will be passed as request.param to its respective argname fixture function so that it can perform more expensive setups during the setup phase of a test rather than at collection time. # indirect:當indirect=True時,若傳入的argnames是fixture函式名,此時fixture函式名將成為一個可執行的函式,argvalues作為fixture的引數,執行fixture函式,最終結果再存入 request.param;
當indirect=False時,fixture函式只作為一個引數名給測試收集階段呼叫。
# 什麼是 the setup phase 測試設定階段? 理解為配置 conftest.py 階段
# 什麼是 the collection phase 測試收集階段? 理解為 用例執行 階段
:arg ids: list of string ids, or a callable. If strings, each is corresponding to the argvalues so that they are part of the test id. If None is given as id of specific test, the automatically generated id for that argument will be used. If callable, it should take one argument (self,a single argvalue) and return a string or return None. If None, the automatically generated id for that argument will be used. If no ids are provided they will be generated automatically from the argvalues. # ids:字串列表,可以理解成標題,與用例個數保持一致 :arg scope: if specified it denotes the scope of the parameters. The scope is used for grouping tests by parameter instances. It will also override any fixture-function defined scope, allowing to set a dynamic scope using test context or configuration. # 如果指定,則表示引數的範圍。作用域用於按引數例項對測試進行分組。它還將覆蓋任何fixture函式定義的範圍,允許使用測試上下文或配置設定動態範圍。 """
以除法為例(ids 用例標題,與 argvalues 個數一致):
# File : test_demo_10.py # IDE : PyCharm import pytest def division(a, b): return int(a / b) @pytest.mark.parametrize('a, b, c', [(4, 2, 2), (0, 2, 0), (1, 0, 0), (6, 8, 0)], ids=['整除', '被除數為0', '除數為0', '非整除']) def test_1(a, b, c): res = division(a, b) assert res == c
# File : test_demo_10.py # IDE : PyCharm import pytest def division(a, b): return int(a / b) @pytest.mark.parametrize('a, b, c', [(4, 2, 2), (0, 2, 0), (1, 0, 0), (6, 8, 0)], ids=['整除', '被除數為0', '除數為0', '非整除']) def test_1(a, b, c): res = division(a, b) assert res == c
執行結果:
E:\personal\python38\python.exe E:/personal/GitWorkSpace/pytest_basic/main.py test_demo_10.py::test_1[整除] test_demo_10.py::test_1[被除數為0] test_demo_10.py::test_1[除數為0] test_demo_10.py::test_1[非整除] ..F. ================================== FAILURES =================================== ________________________________ test_1[除數為0] _________________________________ a = 1, b = 0, c = 0 @pytest.mark.parametrize('a, b, c', [(4, 2, 2), (0, 2, 0), (1, 0, 0), (6, 8, 0)], ids=['整除', '被除數為0', '除數為0', '非整除']) def test_1(a, b, c): > res = division(a, b) test_demo_10.py:15: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ a = 1, b = 0 def division(a, b): > return int(a / b) E ZeroDivisionError: division by zero test_demo_10.py:11: ZeroDivisionError =========================== short test summary info =========================== FAILED test_demo_10.py::test_1[除數為0] - ZeroDivisionError: division by zero 1 failed, 3 passed in 0.10s Process finished with exit code 0