【问题标题】:Testin a flask enfpoint which renders an HTML using unittest测试使用 unittest 呈现 HTML 的烧瓶端点
【发布时间】:2021-12-19 10:36:24
【问题描述】:

我有一个简单的代码,它在主路由中返回 Flask 的 render_template("home.html")。我想知道如何使用unittest 对其进行测试?

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

【问题讨论】:

    标签: python-3.x flask python-unittest


    【解决方案1】:

    我个人做以下事情

    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/

    【讨论】:

      猜你喜欢
      • 2016-02-14
      • 2021-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 2016-05-14
      • 1970-01-01
      相关资源
      最近更新 更多