flask_testing AssertionError:现有端点函数

时间:2017-04-30 13:52:11

标签: python flask flask-testing

当我编写一些测试用例时使用flask_test,它将运行如下例外:AssertionError: View function mapping is overwriting an existing endpoint function: project

当我调试发现create_app会多次调用时,当它运行第二次测试时,将显示除外。

这是我的代码:

class CrawlServerTestCase(TestCase):
    def create_app(self):
        app = create_app(TestingConfig)
        self.client = app.test_client()
        return app

    def setUp(self):
        user_datastore.create_user(email = USEREMAIL, password = PASSWORD)
        db.session.commit()

    def login(self, email, password):
        ''' Login helper function '''
        return self.app.test_client().post('/login', data=dict(
            email=email,
            password=password
        ), follow_redirects=True)

    def logout(self):
        ''' Logout helper function '''
        return self.app.test_client().get('/logout', follow_redirects=True)

    def test_login_logout(self):
        ''' Test login and logout using help function '''
        rv = self.login(USEREMAIL, PASSWORD)
        self.assertIn(b'Dcrawl engin', rv.data)

    def test_api(self):
        self.client.get('/api/v1/projects')

异常:

Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/flask_testing/utils.py", line 137, in _pre_setup
    self.app = self.create_app()
  File "test_app.py", line 68, in create_app
    app = create_app(TestingConfig)
  File "/Users/yinyi/Documents/kyrione/kyrione's document/X-project/git/Dcrawl_engine/crawl_server/crawl_server/app.py", line 48, in create_app
    configure_extensions(app)
  File "/Users/yinyi/Documents/kyrione/kyrione's document/X-project/git/Dcrawl_engine/crawl_server/crawl_server/app.py", line 105, in configure_extensions
    api.init_app(app)
  File "/Library/Python/2.7/site-packages/flask_restful/__init__.py", line 118, in init_app
    self._init_app(app)
  File "/Library/Python/2.7/site-packages/flask_restful/__init__.py", line 200, in _init_app
    self._register_view(app, resource, *urls, **kwargs)
  File "/Library/Python/2.7/site-packages/flask_restful/__init__.py", line 467, in _register_view
    app.add_url_rule(rule, view_func=resource_func, **kwargs)
  File "/Library/Python/2.7/site-packages/flask/app.py", line 65, in wrapper_func
    return f(self, *args, **kwargs)
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1068, in add_url_rule
    'existing endpoint function: %s' % endpoint)
AssertionError: View function mapping is overwriting an existing endpoint function: project

更改这样的代码,它有效:

class CrawlServerTestCase(TestCase):
    def create_app(self):
        app = create_app(TestingConfig)
        self.client = app.test_client()
        return app

    def setUp(self):
        user_datastore.create_user(email = USEREMAIL, password = PASSWORD)
        db.session.commit()

    def login(self, email, password):
        ''' Login helper function '''
        return self.app.test_client().post('/login', data=dict(
            email=email,
            password=password
        ), follow_redirects=True)

    def logout(self):
        ''' Logout helper function '''
        return self.app.test_client().get('/logout', follow_redirects=True)

    def test_login_logout(self):
        ''' Test login and logout using help function '''
        rv = self.login(USEREMAIL, PASSWORD)
        self.assertIn(b'Dcrawl engin', rv.data)
        self.client.get('/api/v1/projects'

0 个答案:

没有答案