python - Brightway2 测试:拆除临时测试项目(夹具)
问题描述
我正在编写一些brightway2 扩展,并在pytest 中编写相应的测试。我在测试的拆卸部分遇到问题。
像其他 brightway2 测试一样,我在创建我的夹具时使用 @bw2tests 装饰器,请参见此处。这允许在临时目录中创建项目,并且通常会正确配置 brightway2 以进行测试。
我的夹具看起来像这样:
@pytest.fixture
@bw2test
def basic():
"""Pytest fixture with test bw2 project with test data to use in test"""
# Write test data...
# For example, for the biosphere Database:
biosphere = Database("biosphere")
biosphere.register()
biosphere.write({
("biosphere", "1"): {
'categories': ['things'],
'exchanges': [],
'name': 'an emission',
'type': 'emission',
'unit': 'kg'
})
# Once I have created all the data I need,
# I yield the data I need for my test functions...
yield {'project': projects.current, 'method_name': method_name}
# Once my tests are done, I would like to tear down the project
projects.delete_project(projects.current, delete_dir=True)
这一切都有效,直到拆卸:由于该项目是 temp 目录中唯一的一个,我得到ValueError: Can't delete only remaining project
.
但是,如果我不拆除,那么每次运行测试时创建的新测试目录都会保留在磁盘上。它们不是那么大(100kB),但我仍然认为它们不应该存在。
有什么建议么?
解决方案
而不是使用该projects
功能,只需使用 .nuke 完全删除目录即可shutil.rmtree
。bw2data
与 3.5.1(5.9.2019 发布)相比,这现在是自动完成的。
推荐阅读
- r - 在 MacOS 上安装 R 包:找不到图像错误
- javascript - ReactJs 中的地图函数内的模式有错误消息
- python - Google colab 在使用 python 和 tensorflow 进行图像分类时不显示错误、图表、任何图像结果?
- python - 如何将上下文结果作为参数传递给 django 中的另一个函数?
- python - ValueError:发现样本数量不一致的输入变量:线性回归中的 [395, 1]
- c++ - ATL C++ 抛出编译错误
- javascript - 使用嵌套在不同级别的键对对象数组进行排序
- java - 根据实体中集合的大小对结果集进行排序
- ckeditor - 如何在 Vue 3 中从在线构建器导入/使用 ckeditor 5?
- react-native - 从嵌套选项卡导航器(嵌套导航)导航父路由器的最佳方式