是否可以为参数化生成输入?
Is it possible to generate input for parametrize?
通过几个测试用例,我有相当大的参数集 运行。
如果可能的话,我宁愿让集合存在于其他地方而不是在参数化语句中,填充参数化。这样参数化几个测试用例不会有重复的大块测试用例参数。
如果那不可能,还有其他方法可以 "share" 这个参数化吗?为了避免重复修饰受影响的测试用例?
import pytest
# this data structure has about 20 of these
@pytest.mark.parametrize("a, b, c" [('hello' [(1,1), ('abc','abc')],[(1, 2)]....)
def test_case_a(a, b, c):
# the same data and arguments as test_case_a
@pytest.mark.parametrize("a, b, c" [('hello' [(1,1), ('abc','abc')],[(1, 2)]....)
def test_case_b(a, b, c):
只需将您的共享参数放入全局变量即可:
test.py
import pytest
SHARED_PARAMS = "a, b, c", [['hello', [(1, 1), ('abc', 'abc')], [(1, 2)]]]
@pytest.mark.parametrize(*SHARED_PARAMS)
def test_case_a(a, b, c):
pass
@pytest.mark.parametrize(*SHARED_PARAMS)
def test_case_b(a, b, c):
pass
执行结果:
$ pytest -v
=========================== test session starts =========================
platform linux -- Python 3.7.0, pytest-3.6.2, py-1.5.4, pluggy-0.6.0
-- /home/user/.virtualenvs/test3.7/bin/python3.7
cachedir: .pytest_cache
rootdir: /home/user/projects/so, inifile:
collected 2 items
so/test_api.py::test_case_a[hello-b0-c0] PASSED
so/test_api.py::test_case_b[hello-b0-c0] PASSED
======================== 2 passed in 0.01 seconds =======================
通过几个测试用例,我有相当大的参数集 运行。 如果可能的话,我宁愿让集合存在于其他地方而不是在参数化语句中,填充参数化。这样参数化几个测试用例不会有重复的大块测试用例参数。
如果那不可能,还有其他方法可以 "share" 这个参数化吗?为了避免重复修饰受影响的测试用例?
import pytest
# this data structure has about 20 of these
@pytest.mark.parametrize("a, b, c" [('hello' [(1,1), ('abc','abc')],[(1, 2)]....)
def test_case_a(a, b, c):
# the same data and arguments as test_case_a
@pytest.mark.parametrize("a, b, c" [('hello' [(1,1), ('abc','abc')],[(1, 2)]....)
def test_case_b(a, b, c):
只需将您的共享参数放入全局变量即可:
test.py
import pytest
SHARED_PARAMS = "a, b, c", [['hello', [(1, 1), ('abc', 'abc')], [(1, 2)]]]
@pytest.mark.parametrize(*SHARED_PARAMS)
def test_case_a(a, b, c):
pass
@pytest.mark.parametrize(*SHARED_PARAMS)
def test_case_b(a, b, c):
pass
执行结果:
$ pytest -v
=========================== test session starts =========================
platform linux -- Python 3.7.0, pytest-3.6.2, py-1.5.4, pluggy-0.6.0
-- /home/user/.virtualenvs/test3.7/bin/python3.7
cachedir: .pytest_cache
rootdir: /home/user/projects/so, inifile:
collected 2 items
so/test_api.py::test_case_a[hello-b0-c0] PASSED
so/test_api.py::test_case_b[hello-b0-c0] PASSED
======================== 2 passed in 0.01 seconds =======================