我是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?
比你们(还有女孩)
安德烈
解决方法:
忘记先前的答案.假设您需要按值对测试进行分组,则可以使用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
我认为那是最接近的.
原文地址:https://codeday.me/bug/20191122/2059731.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。