@pytest.fixture
def myfixture(request):
prepare_stuff()
yield 1
clean_stuff()
# time.sleep(10) # in doubt, I tried that, did not help
def test_1(myfixture):
a = somecode()
assert a==1
def test_2(myfixture):
b = somecode()
assert b==1
案例一
当这两个测试单独执行时,一切都正常,即两者都通过。
pytest ./test_module.py:test_1
接着立即执行:
pytest ./test_module.py:test_2
运行直到完成并成功通过。
情况2
但是:
pytest ./test_module.py -k "test_1 or test_2"
报告:
collected 2 items
test_module.py .
调查后发现,test_1
已经成功完成,但第二次调用 prepare_stuff
时卡住了。
问题
在我的特定设置中,prepare_stuff
、clean_stuff
和 somecode
非常复杂,即它们创建和删除一些共享内存段,如果操作不当会导致一些挂起。因此这里可能存在一些问题。
但是我的问题是:在两次调用 pytest
(情况1)之间是否发生了某些事情,而在同一“pytest进程”中从test_1
到test_2
的调用(情况2)之间没有发生,这能解释为什么“情况1”正常工作,而“情况2”在test_1
和test_2
之间卡住了吗?如果是这样,有办法在“情况2”中强制执行与 test_1
和 test_2
之间的相同“清理”过程吗?
注意:我已经尝试将“myfixture”的作用域指定为“function”,并且还仔细检查了“clean_stuff”在“case 2”中也是在“test_1”之后被调用。
prepare_stuff
/clean_stuff
中存在一些错误/死锁/全局状态污染,因此这个问题无法回答。 - anthony sottile