1

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

调试的时候发现create_app会调用很多次,运行第二次的时候出现except。</p>

这是我的代码:

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

像这样改代码,就可以了:</p>

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

0 回答 0