2016-11-20 10 views
0

私はフラスコ-RestplusサイトでのToDoの例をテストしようとしているが、それは基本的に私は3つのファイル持っている私に404 ...テストルーティング青写真 - フラスコRestplus

を取得し続け:

をapp.py

import sys 
import os 
import platform 
import datetime 
import logging  
from logging import Formatter 
from logging.handlers import RotatingFileHandler 

from jinja2 import Environment, PackageLoader 

from flask import Flask, url_for, render_template, abort, request, Blueprint 
from flask.ext.restplus import Api, Resource, fields 
from werkzeug.contrib.fixers import ProxyFix 


api_v1 = Blueprint('api', __name__, url_prefix='/api/1') 


ns = api.namespace('todos', description='TODO operations') 

TODOS = { 
    'todo1': {'task': 'build an API'}, 
    'todo2': {'task': '?????'}, 
    'todo3': {'task': 'profit!'}, 
} 


todo = api.model('Todo', { 
    'task': fields.String(required=True, description='The task details') 
}) 

listed_todo = api.model('ListedTodo', { 
    'id': fields.String(required=True, description='The todo ID'), 
    'todo': fields.Nested(todo, description='The Todo') 
}) 


def abort_if_todo_doesnt_exist(todo_id): 
    if todo_id not in TODOS: 
     api.abort(404, "Todo {} doesn't exist".format(todo_id)) 

parser = api.parser() 
parser.add_argument('task', type=str, required=True, help='The task details', location='form') 

@ns.route('/<string:todo_id>') 
@api.doc(responses={404: 'Todo not found'}, params={'todo_id': 'The Todo ID'}) 
class Todo(Resource): 
    '''Show a single todo item and lets you delete them''' 
    @api.doc(description='todo_id should be in {0}'.format(', '.join(TODOS.keys()))) 
    @api.marshal_with(todo) 
    def get(self, todo_id): 
     '''Fetch a given resource''' 
     abort_if_todo_doesnt_exist(todo_id) 
     return TODOS[todo_id] 

    @api.doc(responses={204: 'Todo deleted'}) 
    def delete(self, todo_id): 
     '''Delete a given resource''' 
     abort_if_todo_doesnt_exist(todo_id) 
     del TODOS[todo_id] 
     return '', 204 

    @api.doc(parser=parser) 
    @api.marshal_with(todo) 
    def put(self, todo_id): 
     '''Update a given resource''' 
     args = parser.parse_args() 
     task = {'task': args['task']} 
     TODOS[todo_id] = task 
     return task 


@ns.route('/') 
class TodoList(Resource): 
    '''Shows a list of all todos, and lets you POST to add new tasks''' 
    @api.marshal_list_with(listed_todo) 
    def get(self): 
     '''List all todos''' 
     return [{'id': id, 'todo': todo} for id, todo in TODOS.items()] 

    @api.doc(parser=parser) 
    @api.marshal_with(todo, code=201) 
    def post(self): 
     '''Create a todo''' 
     args = parser.parse_args() 
     todo_id = 'todo%d' % (len(TODOS) + 1) 
     TODOS[todo_id] = {'task': args['task']} 
     return TODOS[todo_id], 201 


app = Flask(__name__) 

app.secret_key = "secretkey" 
app.config.from_pyfile('settings.py') 

if os.path.exists(os.path.join(app.root_path, 'local_settings.py')): 
    app.config.from_pyfile('local_settings.py') 


app.config['SQLALCHEMY_DATABASE_URI'] = 'postgres://%[email protected]%s/%s' % (
    app.config['DB_USER'], app.config['DB_HOST'], app.config['DB_NAME']) 

main.py

import sys 

from flask import Blueprint 

from portal.app import app 
from portal import libs 

if __name__ == '__main__': 
    if len(sys.argv) == 2: 
     port = int(sys.argv[1]) 
    else: 
     port = 5000 

    host = app.config.get('HOST', '127.0.0.1') 

    from portal.app import api_v1 
    app.register_blueprint(api_v1) 

    import os 
    app.root_path = os.getcwd() 
    print "Running in", app.root_path, " with DEBUG=", app.config.get('DEBUG', False) 
    app.run(host, 
      port, 
      app.config.get('DEBUG', False), 
      use_reloader=True 
    ) 

tests.py

class ApiTests(helpers.ViewBase): 

    ############################ 
    #### setup and teardown #### 
    ############################ 

    def setUp(self): 
     super(ApiTests, self).setUp() 
     app.config['TESTING'] = True 
     app.config['WTF_CSRF_ENABLED'] = False 
     app.config['DEBUG'] = False 
     self.assertEquals(app.debug, False) 

    # executed after to each test 
    def tearDown(self): 
     pass 

    ############### 
    #### tests #### 
    ############### 
    def test_can_obtain_todos(self): 
     response = self.client.get('/api/1/todos') 
     self.assertEqual(response.status_code, 200) 

私は問題なくhttp://localhost:5000/api/1/todosにアクセスできるアプリを実行するが、私はテストを実行する場合、私は404ルーティング例外

Traceback (most recent call last): 
File "/home/internetmosquito/python_envs/portal/local/lib/python2.7/site-packages/flask/app.py", line 1639, in full_dispatch_request 
rv = self.dispatch_request() 
File "/home/internetmosquito/python_envs/portal/local/lib/python2.7/site-packages/flask/app.py", line 1617, in dispatch_request 
self.raise_routing_exception(req) 
File "/home/internetmosquito/python_envs/portal/local/lib/python2.7/site-packages/flask/app.py", line 1600, in raise_routing_exception 
raise request.routing_exception 
NotFound: 404: Not Found 
> /home/internetmosquito/git/wizbots/portal/src/portal/test/test_api.py(47)test_can_obtain_todos() 

任意のアイデアを取得しておく場合ここで私は何が欠けているのですか?ありがとう!

答えて

0

テストファイルで青写真を適切に初期化していないため、テストが失敗しました...これは、循環依存性の問題を避けるためにmain.pyで行われています。

単に青写真を再作成してテストファイルのsetUpに割り当てるだけですが、これは効率的ではありません。回避する必要があります。循環依存が起こっている理由を確認してください代わりにapp.pyファイル...

関連する問題