python-如何使用py.test对多个项目执行多个测试
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python-如何使用py.test对多个项目执行多个测试,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2609字,纯文字阅读大概需要4分钟。
内容图文
![python-如何使用py.test对多个项目执行多个测试](/upload/InfoBanner/zyjiaocheng/659/21462663ce73459cb0161404d602316c.jpg)
我是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
我认为那是最接近的.
内容总结
以上是互联网集市为您收集整理的python-如何使用py.test对多个项目执行多个测试全部内容,希望文章能够帮你解决python-如何使用py.test对多个项目执行多个测试所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。