使用 unittest 测试呈现 HTML 的烧瓶端点

Testin a flask enfpoint which renders an HTML using unittest

我有一个简单的代码returns render_template("home.html") Flask 的主要路线。我想知道如何使用 unittest?

来测试它
@app.route("/", methods=["GET", "POST"])
def home():
  
    return render_template("home.html")

我个人是这样做的

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/