2012-12-21 23 views
6

मैं लंबे समय से पाठक रहा हूं, लेकिन यह मेरी पहली बार पोस्टिंग है।फ्लास्क एप्लिकेशन यूनिट-टेस्ट एसेशन त्रुटि

ठीक है, इसलिए मैं फ्लास्क में एक डेमो एप्लिकेशन का परीक्षण करने की कोशिश कर रहा हूं और मुझे नहीं पता कि मैं क्या गलत कर रहा हूं।

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


@app.route('/hello/<username>') 
def hello_username(username): 
    return "Hello %s" % username 

पहले मार्ग लोड कर रहा है base.html टेम्पलेट एक "हाय" संदेश है, जो इकाई में काम कर रहा है प्रस्तुत करता है:

इन manager.py नामक एक फाइल में मेरे "मार्गों" कर रहे हैं परीक्षण लेकिन दूसरा मार्ग एक दावा त्रुटि हो जाता है।

और यह मेरा परीक्षण फ़ाइल manage_test.py है:

.F 
====================================================================== 
FAIL: test_username (tests.manage_tests.ManagerTestCase) 
---------------------------------------------------------------------- 
Traceback (most recent call last): 
    File "/Users/albertogg/Dropbox/code/Python/flask-bootstrap/tests/manage_tests.py", line 15, in test_username 
    assert "Hello alberto" in rv.data 
AssertionError 

---------------------------------------------------------------------- 
Ran 2 tests in 0.015s 

FAILED (failures=1) 

मैं तुम लोगों की मदद कर सकते है, तो जानना चाहता हूँ:

class ManagerTestCase(unittest.TestCase): 

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

    def t_username(self, username): 
     return self.app.post('/hello/<username>', follow_redirects=True) 

    def test_username(self): 
     rv = self.t_username('alberto') 
     assert "Hello alberto" in rv.data 

    def test_empty_db(self): 
     rv = self.app.get('/') 
     assert 'hi' in rv.data 

इस इकाई परीक्षण रन से उत्पादन होता है मुझे! मैं गलत क्या कर रहा हूं या लापता हूं?

संपादित

मैं इस किया था और यह


    class ManagerTestCase(unittest.TestCase):

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

    def t_username(self, username): 
     return self.app.get('/hello/%s' % (username), follow_redirects=True') 
     # either that or the Advanced string formatting from the answer are working. 

    def test_username(self): 
     rv = self.t_username('alberto') 
     assert "Hello alberto" in rv.data 

    def test_empty_db(self): 
     rv = self.app.get('/') 
     assert 'hi' in rv.data 

+0

एक बात के लिए, आपको सभी को डब्ल्यू पोस्ट पर/हैलो।दूसरे के लिए, 'hello_username' के लिए 'उपयोगकर्ता नाम' तर्क को POST डेटा स्वचालित रूप से विधि तर्कों में परिवर्तित नहीं किया जाता है। – sberry

+0

और दूसरी बात के लिए, 't_username'' उपयोगकर्ता नाम 'तर्क के साथ पोस्ट का डेटा सेट नहीं करता है। – yiding

उत्तर

2

काम कर रहा है आप अपने hello_username निम्नलिखित में बदल जानी चाहिए:

@app.route('/hello/', methods=['POST']) 
def hello_username(): 
    return "Hello %s" % request.form.get('username', 'nobody') 

from flask import request भी सुनिश्चित करें।

और एक उदाहरण है, यह काम कर रहा दिखा:

> curl -X POST -i 'http://localhost:2000/hello/' -d "username=alberto" 
HTTP/1.0 200 OK 
Content-Type: text/html; charset=utf-8 
Content-Length: 9 
Server: Werkzeug/0.8.3 Python/2.7.2 
Date: Fri, 21 Dec 2012 05:42:49 GMT 

Hello alberto 

और अपने परीक्षण की तरह दिखना चाहिए:

def test_username(self, username): 
    return self.app.post('/hello', data={"username":username}) 

संपादित
अपनी टिप्पणी के अनुसार:

@app.route('/hello/<username>', methods=['POST']) 
def hello_username(username): 
    print request.args 
    return "Hello %s" % username 

लेकिन , तो मैं नहीं करता पता है कि आप POST का उपयोग क्यों करेंगे क्योंकि यह किसी भी पोस्ट बॉडी के बिना अनिवार्य रूप से एक पोस्ट है।

> curl -X POST -i 'http://localhost:2000/hello/alberto'   
HTTP/1.0 200 OK 
Content-Type: text/html; charset=utf-8 
Content-Length: 13 
Server: Werkzeug/0.8.3 Python/2.7.2 
Date: Fri, 21 Dec 2012 06:29:25 GMT 

Hello alberto 

उस मामले में, मैं सभी को एक साथ पोस्ट डेटा के लिए आवश्यकता को दूर करेंगे:

@app.route('/hello/<username>', methods=['POST']) 
def hello_username(username): 
    print request.args 
    return "Hello %s" % username 


> curl -i 'http://localhost:2000/hello/alberto'   
HTTP/1.0 200 OK 
Content-Type: text/html; charset=utf-8 
Content-Length: 13 
Server: Werkzeug/0.8.3 Python/2.7.2 
Date: Fri, 21 Dec 2012 06:31:10 GMT 

प्राप्त का उपयोग कर परीक्षण

def test_username(self, username): 
    return self.app.get('/hello/%s' % (username), follow_redirects=True) 

होगा या, यह सोचते हैं आप 2.6+ है,

def test_username(self, username): 
    return self.app.get('/hello/{username}'.format(username=username), follow_redirects=True) 
+0

धन्यवाद, लेकिन अनुरोध को ठीक करने के लिए मुझे "मार्ग" और विधि को संशोधित क्यों करना है? मुझे पता है शायद यह वही बात है ... लेकिन यह मुझे इस तरह से भ्रमित करता है – albertogg

+0

यदि आप पोस्ट डेटा स्वीकार करने की कोशिश कर रहे हैं, तो मार्ग को POST विधि स्वीकार करने की आवश्यकता है। यह सिर्फ HTTP है। – sberry

+0

आप PO पोस्ट के साथ सही हैं, लेकिन अगर मैं "मार्ग" बदलता हूं तो यह मेरे ऐप में काम करना बंद कर देगा, यह मेरा मामला है – albertogg

 संबंधित मुद्दे

  • कोई संबंधित समस्या नहीं^_^