brightway2测试:拆除临时测试项目(夹具)

2024-06-16 14:53:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一些brightway2扩展,并在pytest中编写相应的测试。我在测试的拆卸部分遇到了问题

与其他brightway2测试一样,我在创建夹具时使用@bw2tests装饰器,请参见here。这允许在temp dir中创建项目,并且通常为测试正确配置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),但我仍然认为它们不应该存在

有什么建议吗


Tags: the项目nametestprojectdatapytestdir