我是python的新手,也是py.test的新手。我正在寻找一种方法来对多个项目运行多个测试,但无法找到它。我知道当你知道怎么做时,这很简单。
我已经简化了我想要做的事情,以使其易于理解。
如果我有一个Test类,他定义了一系列像这样的测试:
class SeriesOfTests:
def test_greater_than_30(self, itemNo):
assert (itemNo > 30), "not greather than 30"
def test_lesser_than_30(self, itemNo):
assert (itemNo < 30), "not lesser thant 30"
def test_modulo_2(self, itemNo):
assert (itemNo % 2) == 0, "not divisible by 2"
我想对从以下函数获得的每个项目执行此SeriesOfTest:
def getItemNo():
return [0,11,33]
我想要获得的结果是:
RESULT :
Test "itemNo = 0"
- test_greater_than_30 = failed
- test_lesser_than_30 = success
- test_modulo_2 = success
Test "itemNo = 11"
- test_greater_than_30 = failed
- test_lesser_than_30 = success
- test_modulo_2 = failed
Test "itemNo = 33"
- test_greater_than_30 = success
- test_lesser_than_30 = failed
- test_modulo_2 = failed
我怎么能用py.test做到这一点?
比你们(还有女孩)
安德烈
答案 0 :(得分:2)
使用fixture:
import pytest
@pytest.fixture(params=[0, 11, 33])
def itemNo(request):
return request.param
def test_greater_than_30(itemNo):
assert (itemNo > 30), "not greather than 30"
def test_lesser_than_30(itemNo):
assert (itemNo < 30), "not lesser thant 30"
def test_modulo_2(itemNo):
assert (itemNo % 2) == 0, "not divisible by 2"
注意:灯具功能的名称(itemNo
)和测试功能参数的名称应该相同。
请参阅Demo run。
<强>更新强>
import pytest
class FunctionWrapper(str):
def __init__(self, f):
self.f = f
def __call__(self, *args, **kwargs):
return self.f(*args, **kwargs)
def __str__(self):
return self.f.__name__
def greater_than_30(itemNo):
assert (itemNo > 30), "not greater than 30"
def lesser_than_30(itemNo):
assert (itemNo < 30), "not lesser thant 30"
def modulo_2(itemNo):
assert (itemNo % 2) == 0, "not divisible by 2"
@pytest.fixture(params=[0, 11, 33])
def itemNo(request):
return request.param
@pytest.fixture(params=map(FunctionWrapper, [
greater_than_30, lesser_than_30, modulo_2
]))
def assertion_func(request):
return request.param
def test_item_no(itemNo, assertion_func):
assertion_func(itemNo)
使用-v --tb=no
选项。
例如:
============================= test session starts ==============================
platform linux2 -- Python 2.7.5 -- pytest-2.3.5 -- /usr/bin/python
collected 9 items
test_sample.py:26: test_item_no[0-greater_than_30] FAILED
test_sample.py:26: test_item_no[0-lesser_than_30] PASSED
test_sample.py:26: test_item_no[0-modulo_2] PASSED
test_sample.py:26: test_item_no[11-greater_than_30] FAILED
test_sample.py:26: test_item_no[11-lesser_than_30] PASSED
test_sample.py:26: test_item_no[11-modulo_2] FAILED
test_sample.py:26: test_item_no[33-greater_than_30] PASSED
test_sample.py:26: test_item_no[33-lesser_than_30] FAILED
test_sample.py:26: test_item_no[33-modulo_2] FAILED
====================== 5 failed, 4 passed in 0.03 seconds ======================
答案 1 :(得分:1)
忘掉之前的回答。鉴于您需要要按值分组的测试,您可以使用scenarios。我刚刚从文档中修改了示例:
import pytest
def pytest_generate_tests(metafunc):
idlist = []
argvalues = []
for scenario in metafunc.cls.scenarios:
idlist.append(scenario[0])
items = scenario[1].items()
argnames = [x[0] for x in items]
argvalues.append(([x[1] for x in items]))
metafunc.parametrize(argnames, argvalues, ids=idlist, scope="class")
scenario1 = ('itemNo = 0', {'itemNo': 0})
scenario2 = ('itemNo = 11', {'itemNo': 11})
scenario3 = ('itemNo = 33', {'itemNo': 33})
class TestSeries:
scenarios = [scenario1, scenario2, scenario3]
def test_greater_than_30(self, itemNo):
assert (itemNo > 30), "not greather than 30"
def test_lesser_than_30(self, itemNo):
assert (itemNo < 30), "not lesser thant 30"
def test_modulo_2(self, itemNo):
assert (itemNo % 2) == 0, "not divisible by 2"
输出是:
$ py.test -v
============ test session starts ==============================================
platform linux2 -- Python 2.7.4 -- pytest-2.4.2 -- /home/jose/.virtualenvs/pytest1/bin/python
collected 9 items
test_first.py:23: TestSeries.test_greater_than_30[itemNo = 0] FAILED
test_first.py:25: TestSeries.test_lesser_than_30[itemNo = 0] PASSED
test_first.py:27: TestSeries.test_modulo_2[itemNo = 0] PASSED
test_first.py:23: TestSeries.test_greater_than_30[itemNo = 11] FAILED
test_first.py:25: TestSeries.test_lesser_than_30[itemNo = 11] PASSED
test_first.py:27: TestSeries.test_modulo_2[itemNo = 11] FAILED
test_first.py:23: TestSeries.test_greater_than_30[itemNo = 33] PASSED
test_first.py:25: TestSeries.test_lesser_than_30[itemNo = 33] FAILED
test_first.py:27: TestSeries.test_modulo_2[itemNo = 33] FAILED
我认为这是你能得到的最接近的。