2014-06-14 7 views
1

테스트 내 테스트 파일 : 나는 파이썬에 newb 오전이 일을 알아낼 수없는 것Newb 여기

$ nosetests tests/api/client/test_init_views.py 
F 
====================================================================== 
FAIL: test_root_route (tests.api.client.test_init_views.TestInitViews) 
---------------------------------------------------------------------- 
Traceback (most recent call last): 
    File "/Users/dmonsewicz/dev/autoresponders/tests/api/client/test_init_views.py", line 17, in test_root_route 
    self.assert_template_used('index.html') 
    File "/Users/dmonsewicz/.virtualenvs/autoresponders-api/lib/python2.7/site-packages/flask_testing.py", line 120, in assertTemplateUsed 
    raise AssertionError("template %s not used" % name) 
AssertionError: template index.html not used 

---------------------------------------------------------------------- 
Ran 1 test in 0.012s 

FAILED (failures=1) 

: 여기

from flask import Flask 
from flask.ext.testing import TestCase 


class TestInitViews(TestCase): 

    render_templates = False 

    def create_app(self): 
     app = Flask(__name__) 
     app.config['TESTING'] = True 
     return app 

    def test_root_route(self): 
     self.client.get('/') 
     self.assert_template_used('index.html') 

전체 스택 추적입니다 아웃. 내가 할 노력하고 모든 사용 된 템플릿이 index.html

시도 내가 Flask-Testing을 사용하고 LiveServerTestCase

from flask import Flask 
from flask.ext.testing import LiveServerTestCase 


class TestInitViews(LiveServerTestCase): 

    render_templates = False 

    def create_app(self): 
     app = Flask(__name__) 
     app.config['TESTING'] = True 
     app.config['LIVESERVER_PORT'] = 6765 

     return app 

    def setUp(self): 
     self.app = self.app.test_client() 

    def test_root_route(self): 
     res = self.app.get('/') 

     print(res) 

     self.assert_template_used('index.html') 

을 사용하여에 실제로 있다는 / (루트 경로) 엔드 포인트 안타 asserts 간단한 테스트를 작성하다 버전 0.4 어떤 이유로 LiveServerTestCase 내 수입에 존재하지 않는

작업 코드

from flask import Flask 
from flask.ext.testing import TestCase 
from api.client import blueprint 


class TestInitViews(TestCase): 

    render_templates = False 

    def create_app(self): 
     app = Flask(__name__) 
     app.config['TESTING'] = True 

     app.register_blueprint(blueprint) 

     return app 

    def test_root_route(self): 
     res = self.client.get('/') 
     self.assert_template_used('index.html') 

답변

1

당신은 핍 실행 점멸등을 설치하고 플라스크 버전이 0.6 이상인지 확인해야합니다. 당신의 app.config 설정을 생략처럼

는 [ '테스트']은 보이는 사실

나는 어설 사실 확인을 실행하려면 다음 테스트를 얻을 수 있었다

: 대한

#!/usr/bin/python 

import unittest 
from flask import Flask 
from flask.ext.testing import TestCase 
from flask import render_template 


class MyTest(TestCase): 

    def create_app(self): 
    app = Flask(__name__) 
    app.config['TESTING'] = True 

    @app.route('/') 
    def hello_world(): 
     return render_template('index.html') 
    return app 

    def test_root_route(self): 
    self.client.get('/') 
    self.assert_template_used('index.html') 

if __name__ == '__main__': 
    unittest.main() 
+0

주셔서 감사합니다 대답! 내 질문을 지금 업데이트 중입니다 ... 어떤 이유로'self.client.get ('/')'가 실행되면 404가 반환됩니다. – dennismonsewicz

+0

데코레이터 app.route ('/')가있는 함수가 있어야한다고 생각합니다. app을 반환하기 전에 내가 배치 한 hello world 함수를 참조하십시오. 또한 index.html을 사용하여 templates 디렉토리를 만들어야합니다. 테스트와 동일한 디렉토리에 배치하십시오. – RohitJ

+0

그래서'/'경로를 실제로 방문하지 않고 렌더링 된 레이아웃이 index.html인지 확인합니까? 나는'Rails' 배경에서 왔기 때문에 내 무지를 변명하십시오. – dennismonsewicz