我想使用unittest包测试我的应用程序。特别是我正在尝试测试我的表格。我很难测试有关如何从表单中获取数据并将其发布的帖子请求
我的参考: https://realpython.com/python-testing/#more-advanced-testing-scenarios
我的test.py代码
from app import app
import unittest
class FlaskTestCase(unittest.TestCase):
def test_add(self):
tester = app.test_client(self)
response = tester.post('/Search/add', data=dict(fn =
"Emmanuel Ever", ln = "Telewik", email =
"everlopeztelewik@gmail.com", phone = 0912312311,
address = "ILIGAN"),
follow_redirect = True
)
self.assertEqual(response.status_code, 200)
if __name__ == '__main__':
unittest.main()
我要在app.py中测试的功能
@app.route("/Search/add", methods=['POST'])
def new():
if request.method == 'POST':
fn = request.form['fn']
ln = request.form['ln']
email = request.form['email']
phone = request.form['phone']
address = request.form['address']
conn = psycopg2.connect("dbname = 'test_db3' user = 'postgres'
password = 'shizzle7' host = 'localhost' port = '5432'")
cur = conn.cursor()
cur.execute("INSERT into user2 values(Default, %s, %s, %s, %s,
%s)",(fn,ln,email,phone,address))
conn.commit()
conn.close()
return redirect('/Search')
return render_template('index.html')