获取“无法使本地对象"LiveServerTestCase"腌制";尝试使用LiveServerTestCase测试Flask应用程序时出错 [英] Getting a "Can't pickle local object 'LiveServerTestCase'" error when trying to use LiveServerTestCase to test Flask app

查看:84
本文介绍了获取“无法使本地对象"LiveServerTestCase"腌制";尝试使用LiveServerTestCase测试Flask应用程序时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用flask_testing中的LiveServerTestCase在Flask应用上运行单元测试时,出现标题中提到的错误.

这是我的测试文件:

从应用导入

 导入create_app从flask_testing导入LiveServerTestCase类TestBase(LiveServerTestCase):def create_app(self):app = create_app()app.config.update(LIVESERVER_PORT = 8847,TESTING = True)返回应用def test_app(self):self.assertEqual('test','test') 

这是我使用鼻子2运行测试后遇到的错误:

  AttributeError:无法腌制本地对象"LiveServerTestCase._spawn_live_server.< locals> .worker"在处理上述异常期间,发生了另一个异常:AttributeError:'NoneType'对象没有属性'terminate'内部错误:runTests中止:'NoneType'对象没有属性'terminate' 

我真的找不到关于此问题的任何在线帮助

解决方案

我也遇到了这个问题,并希望发布,以便其他人可以在某个地方开始学习,而不必像我一样深入探究-

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

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

  multiprocessing.set_start_method("fork") 

我正在 Windows 上进行开发,因此,根据我的判断,我有点不走运,但是我还有另一台运行CentO的测试服务器,因此为了概念验证,我尝试在那儿运行测试,并且没有问题!

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

I am getting the error mentioned in the title once trying to run a unittest on my Flask app using LiveServerTestCase from flask_testing.

This is my test file:

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')

And this is the error I am getting once running my test using 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'

I really couldn't find anything helpful online about this issue,

解决方案

I also ran into this issue and wanted to post so others might have somewhere to start and not need to sleuth as deep as me--

I found a partial answer here that helped me figure out what was happening at least: https://github.com/pytest-dev/pytest-flask/issues/104

apparently on OSX you need to run the following code to switch the multiprocessing style to fork instead of spawn which is the new default and incompatible with pickle:

multiprocessing.set_start_method("fork")

I'm developing on Windows, so from what I could tell I'm kinda out of luck- but I have another test server running CentOs so for proof of concept I tried running the tests there and it worked without issues!

lastly: On another issue thread I found the following that might help and Windows people make a workaround: https://github.com/pytest-dev/pytest-flask/issues/54

这篇关于获取“无法使本地对象"LiveServerTestCase"腌制";尝试使用LiveServerTestCase测试Flask应用程序时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆