尝试使用LiveServerTestCase测试Flask应用程序时出现“无法pickle本地对象'LiveServerTestCase'”错误

2024-04-20 04:21:58 发布

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

有一次我尝试使用Flask_测试中的LiveServerTestCase在我的Flask应用程序上运行unittest时,遇到了标题中提到的错误

这是我的测试文件:

from app import create_app
from flask_testing import LiveServerTestCase

class TestBase(LiveServerTestCase):

    def create_app(self):
        app = create_app()
        app.config.update(LIVESERVER_PORT=8847, TESTING=True)
        return app
    
    def test_app(self):
        self.assertEqual('test', 'test')

这就是我在使用nose2运行测试时遇到的错误:

AttributeError: Can't pickle local object 'LiveServerTestCase._spawn_live_server.<locals>.worker'

During handling of the above exception, another exception occurred:

AttributeError: 'NoneType' object has no attribute 'terminate'
Internal Error: runTests aborted: 'NoneType' object has no attribute 'terminate'

我真的在网上找不到任何关于这个问题的有用信息


Tags: fromtestimportselfappflaskobjectdef
2条回答

我也遇到了这个问题,我想发一篇文章,这样其他人就可以从某个地方开始,而不需要像我这样深入调查

我在这里找到了一个部分答案,至少帮助我弄清楚发生了什么: https://github.com/pytest-dev/pytest-flask/issues/104

显然,在OSX上,您需要运行以下代码以将多处理样式切换为fork而不是spawn,这是新的默认样式,与pickle不兼容:

multiprocessing.set_start_method("fork")

我正在Windows上开发,所以从我所知道的来看,我有点运气不佳- 但是我有另一台运行CentOs的测试服务器,所以为了验证概念,我尝试在那里运行测试,结果没有问题

最后:在另一个问题线程中,我发现以下内容可能有助于Windows人员解决问题: https://github.com/pytest-dev/pytest-flask/issues/54

无法确定原因。 但是很抱歉,您是否检查了您正在使用的flask版本,以及它当前与python版本的兼容性

相关问题 更多 >