首页 > 解决方案 > 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),但我仍然认为它们不应该存在。

有什么建议么?

标签: pythonpytestbrightway

解决方案


而不是使用该projects功能,只需使用 .nuke 完全删除目录即可shutil.rmtreebw2data与 3.5.1(5.9.2019 发布)相比,这现在是自动完成的。


推荐阅读