烧瓶单元测试“未能建立新的连接” [英] flask unit test "failed to establish a new connection"

查看:214
本文介绍了烧瓶单元测试“未能建立新的连接”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个烧瓶应用程序的单元测试。这个应用程序公开了REST端点,并使用了flask_restful lib。基本上,我的一个端点将会向其他端点发出请求,并进行一些处理。$ b $ {

$ p
$ b

当通过pytest执行测试时,它会返回这个错误(注意:当基本上使用curl进行测试时,这个方法是有效的):

  requests.exceptions.ConnectionError:HTTPConnectionPool(host ='127.0.0.1',
port = 5000):最大重试次数超过url:
/ ctrlm / H_INFOEXPLH:05u3v / output /
(由NewConnectionError引起('< urllib3.connection.HTTPConnection对象

0x0486D090>:无法建立新的连接:[WinError 10061]


以下是测试代码:

  class RestTests(unittest.TestCase):
测试其余模块。
################# ###########
#### setup and teardown ####
#################### ########

def setUp(self):
在每次测试之前执行
app.config ['TESTING'] = True
app.config ['WTF_CSRF_ENABLED' ] = False
app.config ['DEBUG'] = False
app.config ['THREADED'] = True

self.client = app.test_client()

#事件ID:
self.incident =INC1863137

#ctrl-m作业ID:
self.jobid_noput =server:0ceit
self.jobid_no_job =server:0ceity#作业格式肯定会失败!

def tearDown(self):
在每个测试后的每个testexecuted后执行
pass

###### #########
#### tests ####
###############

def test_ctrl_output(self):
UAT 3:找到ctrl-M作业输出。
response = self.client.post('/ servnow / incident / {}'。 self.incident),
data = json.dumps({'data':这只是一个json测试}),
headers = {'Content-Type':'application / json'}

#print(DEBUGGGG !!!!!!!!!!!!!!!!!!! ===============> {}。format(response))
self.assertIsNotNone(response)

也许由setUp()启动的瓶子实例不能被线程化...



在应用程序代码上,这是产生问题的代码:

  url =http:// 127。格式(
flask.url_for('joboutput',jobid = jobid))
resp = requests.get(url).json()

嗯,我只是想执行一个查询从一个网址从瓶...
可能,我这样做是错误的方式....

你能帮助我吗?

解决方案

Unittest的Flask测试服务器是一种模拟。它不会创建监听套接字。



因此,请求自己是不可能的。

  url =http://127.0.0.1:5000 {}.format(
flask.url_for 'joboutput',jobid = jobid))
resp = requests.get(url).json()

将以上述异常结束:

  requests.exceptions.ConnectionError:HTTPConnectionPool(host ='127.0.0.1' ,
port = 5000)
无法建立新的连接

socket 127.0 .0.1:5000只是没有创建。



好吧,这也使我认为,如果我无法测试,我正在构建的解决方案是不正确的。
我重建了它。


I am writting unit tests for a flask app. This app expose REST endpoints and uses the flask_restful lib.

Basically, one of my endpoints will do requests to other endpoints and do some processing.

While executing the tests through pytest, it returns this error (NOTE: this works when basically testing with curl) :

   requests.exceptions.ConnectionError: HTTPConnectionPool(host='127.0.0.1', 
   port=5000): Max retries exceeded with url: 
   /ctrlm/H_INFOEXPLH:05u3v/output/ 
   (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object 
   at 
   0x0486D090>: Failed to establish a new connection: [WinError 10061]

Here is the test code :

class RestTests(unittest.TestCase):
""" Test the rest module. """
    ############################
    #### setup and teardown ####
    ############################

    def setUp(self):
        """ Executed prior to each test """
        app.config['TESTING'] = True
        app.config['WTF_CSRF_ENABLED'] = False
        app.config['DEBUG'] = False
        app.config['THREADED'] = True

        self.client = app.test_client()

        # incident id:
        self.incident = "INC1863137"

        # ctrl-m job id :
        self.jobid_no_output = "server:0ceit"
        self.jobid_no_job = "server:0ceity"  # job format that will surely fail!

    def tearDown(self):
        """ executed after each testexecuted after each test """
        pass

    ###############
    #### tests ####
    ###############

    def test_ctrl_output(self):
        """ UAT 3 : ctrl-M job output is found. """
        response = self.client.post('/servnow/incident/{}'.format(self.incident),
                                data=json.dumps({'data': "This is just a json test"}),
                                headers={'Content-Type': 'application/json'}
                                )
        #print("DEBUGGGG!!!!!!!!!!!!!!!!!!! ===============> {}".format(response))
        self.assertIsNotNone(response)

Well, maybe the flask instance initiated with setUp() is not able to be threaded...

On the app code, this is this code which create the issue :

    url = "http://127.0.0.1:5000{}".format(
        flask.url_for('joboutput', jobid=jobid))
    resp = requests.get(url).json()

Well, I just would like to execute a query to an url from flask... Probably, I'm doing it the wrong way....

Could you help me, please?

解决方案

Unittest's Flask test server is a kind of a mock. It does not create listening socket.

So, making a request to itself is not possible.

Therefore :

url = "http://127.0.0.1:5000{}".format(
    flask.url_for('joboutput', jobid=jobid))
resp = requests.get(url).json()

will end in the aforementioned exception :

requests.exceptions.ConnectionError: HTTPConnectionPool(host='127.0.0.1', 
port=5000)
Failed to establish a new connection

socket 127.0.0.1:5000 is just not created.

Well, it also made me think that the solution i'm building is not right if I cannot test it. I have rebuilded it.

这篇关于烧瓶单元测试“未能建立新的连接”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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