我有一個簡單的代碼,它render_template("home.html")在主路由中回傳Flask。我想知道如何使用它進行測驗unittest?
@app.route("/", methods=["GET", "POST"])
def home():
return render_template("home.html")
uj5u.com熱心網友回復:
我個人執行以下操作
import unittest
from whereyourappisdefined import application
class TestFoo(unittest.TestCase):
# executed prior to each test
def setUp(self):
# you can change your application configuration
application.config['TESTING'] = True
# you can recover a "test cient" of your defined application
self.app = application.test_client()
# then in your test method you can use self.app.[get, post, etc.] to make the request
def test_home(self):
url_path = '/'
response = self.app.get(url_path)
self.assertEqual(response.status_code, 200)
有關測驗 Flask 應用程式的更多資訊:https : //flask.palletsprojects.com/en/2.0.x/testing/
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/353815.html
