【问题标题】:why python nose unittest teardown fixture failed为什么 python nose unittest 拆解夹具失败
【发布时间】:2010-10-12 14:43:31
【问题描述】:

我正在使用nose 测试框架。运行测试模块时,其中定义的拆卸功能失败。引发的错误表示夹具被另一个进程锁定。这是我的测试模块,test_my_module.py

... ...    
def teardown():
    if os.path.exists(test_output_dir):
        shutil.rmtree(test_output_dir)
... ...

@with_setup(init_test_db, destroy_test_db)
def test_foo1():
    eq_(foo1(),1)

@with_setup(init_test_db, destroy_test_db)
def test_foo2():
    eq_(foo2(),2)
... ...

test_output_dir 中有一个 db(sqlite3) 文件,它被用作夹具。实际上,由于它被其他进程锁定,因此无法通过拆卸删除该 db 文件。据我了解,在所有测试功能完成运行后,将始终运行拆解。那么为什么会这样呢?为什么那些测试功能仍然可以锁定 db 文件?是 sqlite3 问题还是我的测试代码有问题?

【问题讨论】:

  • 您能否提供更多关于您的init_test_dbdestroy_test_dbfooN 函数中发生的事情的详细信息?

标签: python unit-testing nose fixture


【解决方案1】:

您可以在删除 test_output_dir 之前尝试在拆卸中明确关闭 sqlite 连接。

【讨论】:

  • 即使显式关闭 sqlite 连接也不起作用。
【解决方案2】:

我相信我在 c# 单元测试中确实遇到了同样的问题。

我通过在删除数据库文件之前调用 SqliteConnection.ClearAllPools() 解决了它,所以它与连接池有关。

也许python中有等效的方法?我真的不知道。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-06
    • 2021-06-03
    相关资源
    最近更新 更多