6 python unit-testing xunit pytest
我有不同的测试文件夹(包).我想设置和拆除特定包(文件夹)的一些数据.
问题是set_up()在运行该文件夹的测试用例之前执行,但是在运行所有测试用例之后,tear_down没有执行.它在运行其他软件包(文件夹)的所有测试用例之后执行(在整个pytest会话之后).
[conftest.py]
@pytest.fixture(scope="session", autouse=True)
def set_up(request):
'''Test package setup'''
def tear_down():
'''Test package teardown'''
Run Code Online (Sandbox Code Playgroud)
每个文件夹都包含__init__.py明显的文件.
那么如何在执行该tear_down()文件夹中运行所有测试用例之后执行just set_up?
据我所知:scope="module"在这种情况下没用,因为我不想为每次测试设置和拆解.
任何帮助都会很棒.谢谢
pytest不直接支持包级别的灯具.unittest也没有.
至于主要测试框架,我相信鼻子是唯一一个支持包装夹具.但是,nose2正在降低包装夹具支撑.见nose2文档.
pytest支持xunit 样式灯具的模块,功能,类和方法级别的灯具.
conftest.py文件是目录级(读“包”)配置。因此,如果您将一个放在测试的根目录中,则其会话范围的固定装置将在该范围的开头运行,并且相应的固定装置tear_down将在执行之前等待该范围的结论(即整个测试会话)。如果您需要创建仅跨越子目录(子包)的固定装置,则需要conftest.py在这些级别放置其他文件(带有它们自己的scope='session'固定装置)。一个常见的示例是将数据添加到数据库。想象一下,想要purchases使用相应测试包内所有测试的一些行来填充数据库表。您可以将完成这项工作的固定装置放在里面tests.purchases.conftest.py。
shopping_app/
tests/
__init__.py
conftest.py # applies to all tests
buyers/
products/
purchases/
conftest.py # only applies to this scope and sub-scopes
__init__.py
test1.py
test2.py
payments/
refunds/
sellers/
stores/
Run Code Online (Sandbox Code Playgroud)
在里面tests.purchases.conftest.py你会有普通的装置声明。例如,用于预填充和删除数据库表行的 set_up/tear_down 组合将如下所示:
@pytest.fixture(scope='session', autouse=True)
def prep_purchases(db, data):
# set_up: fill table at beginning of scope
populate_purchase_table_with_data(db, data)
# yield, to let all tests within the scope run
yield
# tear_down: then clear table at the end of the scope
empty_purchase_table(db)
Run Code Online (Sandbox Code Playgroud)
有些装置不需要显式地注入到测试中(我们只对它们的副作用感兴趣,而不是它们的返回值),因此需要参数autouse。至于 set_up/tear_down 的上下文管理器语法(使用yield),如果您对此不满意,您也可以将该tear_down部分作为其自己的单独函数放置。
| 归档时间: |
|
| 查看次数: |
3710 次 |
| 最近记录: |