python - pytest - 向所有或标记的测试函数添加断言
问题描述
obj
在大多数测试功能的末尾,
我在夹具(此处命名)上执行了常见的断言,例如:
assert obj.is_empty
assert obj.no_errors
如何将这些步骤附加到每个测试功能或仅标记的功能,以便,步骤:
- 将与
pytest.mark.parametrize
, - 当常见断言失败时会报告失败(而不是错误)吗?
解决方案
虽然我个人不喜欢在测试中运行隐藏断言的想法(显式优于隐式pytest
),但通过实现你自己的pytest_runtest_call
钩子肯定是可行的。一个简单的例子:
# conftest.py
import pytest
class Obj:
def __init__(self):
self.is_empty = True
self.no_errors = True
@pytest.fixture
def obj():
return Obj()
def pytest_runtest_call(item):
item.runtest()
try:
obj = item.funcargs['obj']
assert obj.is_empty
assert obj.no_errors
except KeyError:
pass
示例测试:
import pytest
def test_spam(obj):
assert True
def test_eggs(obj):
obj.is_empty = False
assert True
def test_bacon(obj):
obj.no_errors = False
assert True
@pytest.mark.parametrize('somearg', ['foo', 'bar', 'baz'])
def test_parametrized(obj, somearg):
assert True
运行测试产生:
$ pytest -sv
================================== test session starts ==================================
platform darwin -- Python 3.6.4, pytest-3.7.3, py-1.5.4, pluggy-0.7.1 --
cachedir: .pytest_cache
rootdir: /Users/hoefling/projects/private/stackoverflow, inifile:
plugins: cov-2.5.1
collected 3 items
test_spam.py::test_spam PASSED
test_spam.py::test_eggs FAILED
test_spam.py::test_bacon FAILED
test_spam.py::test_parametrized[foo] PASSED
test_spam.py::test_parametrized[bar] PASSED
test_spam.py::test_parametrized[baz] PASSED
======================================== FAILURES =======================================
_______________________________________ test_eggs _______________________________________
item = <Function 'test_eggs'>
def pytest_runtest_call(item):
item.runtest()
try:
obj = item.funcargs['obj']
> assert obj.is_empty
E assert False
E + where False = <conftest.Obj object at 0x105854ba8>.is_empty
conftest.py:20: AssertionError
______________________________________ test_bacon _______________________________________
item = <Function 'test_bacon'>
def pytest_runtest_call(item):
item.runtest()
try:
obj = item.funcargs['obj']
assert obj.is_empty
> assert obj.no_errors
E assert False
E + where False = <conftest.Obj object at 0x105868198>.no_errors
conftest.py:21: AssertionError
========================== 2 failed, 1 passed in 0.05 seconds ===========================
推荐阅读
- .net - asp.net core 自定义不记名令牌和 Azure Ad 令牌授权
- django - 如何在 plesk 上托管我的 django rest 框架应用程序
- java - 无法运行 Eclipse Luna
- c++ - Poco::Dynamic::Var 通过运算符 [] 创建了空对象
- mysql - 如何在mysql查询中将聚合与非聚合结合起来
- javascript - react native:如何过滤特定数据?
- javascript - 如何将图像从 URL 保存为文件夹 - TypeScript
- ajax - 在 C# dot net core 3.1 中使用 Ajax 调用时如何检查会话超时
- postgresql - 在 Airflow 中为 postgres 创建默认用户时出错
- python - 如何在 Python 中附加数据框?