使用pytest在python-unittest中注册要在tearDown运行的方法



这里讨论了类似的问题,但对于单元测试

我知道pytest与unittest兼容。

我想知道pytest中内置的是什么,也就是在测试运行的退出事件中注册的纯pytest方式?

p.s.

我在谷歌和我们的stackoverflow上的搜索对没有帮助

一种方法是使用会话范围的fixture,该fixture在启动时产生,然后在产生后执行拆卸操作。玩具演示:

import pytest
@pytest.fixture(scope = 'session')
def db():
yield 'DbConnection()'
# Occurs after all tests run.
print('TEARING DOWN')
def test_foo(db):
print(db, id(db))
def test_bar(db):
print(db, id(db))

输出:

pytest-teardown.py DbConnection() 4489248432
.DbConnection() 4489248432
.TEARING DOWN

最新更新