我正在编写一些brightway2扩展,并在pytest中编写相应的测试。我在测试的拆卸部分遇到了问题。
与其他brightway2测试一样,我在创建fixture时使用@bw2test装饰器,请参阅here。这允许在临时目录中创建项目,并且通常会正确配置brightway2以进行测试。
我的fixture看起来像这样:
@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)这一切都会一直工作到teardown:因为这个项目是temp目录中唯一的项目,所以我得到了ValueError: Can't delete only remaining project。
但是,如果我不拆除,那么每次运行测试时创建的新测试目录都会留在磁盘上。它们不是那么大(100kB),但我仍然认为它们不应该存在。
有什么建议吗?
发布于 2019-09-05 17:29:06
不使用projects函数,只需使用shutil.rmtree完全清除目录即可。这在bw2data中是自动完成的,而不是3.5.1 (发布于5.9.2019)。
https://stackoverflow.com/questions/57789774
复制相似问题