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

Acr*_*Tom 0 python selenium pytest flask flask-testing

当我尝试使用 Flask_testing 中的 LiveServerTestCase 在 Flask 应用程序上运行单元测试时,我收到了标题中提到的错误。

这是我的测试文件:

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')
Run Code Online (Sandbox Code Playgroud)

这是我使用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'
Run Code Online (Sandbox Code Playgroud)

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

小智 7

我也遇到了这个问题,并想发帖,以便其他人可能有地方可以开始,而不需要像我一样深入调查--

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

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

multiprocessing.set_start_method("fork")
Run Code Online (Sandbox Code Playgroud)

我正在Windows上进行开发,所以据我所知,我有点运气不好 - 但我有另一个运行 CentOs 的测试服务器,所以为了概念证明,我尝试在那里运行测试,它工作没有问题!

最后:在另一个问题线程中,我发现以下内容可能会有所帮助,Windows用户可以找到解决方法: https: //github.com/pytest-dev/pytest-flask/issues/54