मैं लंबे समय से पाठक रहा हूं, लेकिन यह मेरी पहली बार पोस्टिंग है।फ्लास्क एप्लिकेशन यूनिट-टेस्ट एसेशन त्रुटि
ठीक है, इसलिए मैं फ्लास्क में एक डेमो एप्लिकेशन का परीक्षण करने की कोशिश कर रहा हूं और मुझे नहीं पता कि मैं क्या गलत कर रहा हूं।
@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
एक बात के लिए, आपको सभी को डब्ल्यू पोस्ट पर/हैलो।दूसरे के लिए, 'hello_username' के लिए 'उपयोगकर्ता नाम' तर्क को POST डेटा स्वचालित रूप से विधि तर्कों में परिवर्तित नहीं किया जाता है। – sberry
और दूसरी बात के लिए, 't_username'' उपयोगकर्ता नाम 'तर्क के साथ पोस्ट का डेटा सेट नहीं करता है। – yiding