python測試框架 unittest 配合 flask 使用

代碼:

class TestInterface(unittest.TestCase):

    # 測試代碼執行之前調用 (方法名固定)
    def setUp(self):
        """在執行具體的測試方法前,先被調用"""
        app.testing = True  # 指定app在測試模式下運行。 (測試模式下,視圖中的意外異常可以正常打印顯示出來)
        # 使用flask提供的測試客戶端進行測試 (Flask客戶端可以模擬發送請求)
        self.client = app.test_client()
        resp = self.client.post("/api/user/login", data={"username":"xxxx", "password":"xxxx"})
        resp_json = json.loads(resp.data)
        self.cookie = resp_json.get("msg")
    def test_login(self):
        resp = self.client.post("/api/user/login", data={"username":"", "password":""})
        resp_json = json.loads(resp.data)
        self.assertEqual("1",resp_json.get("code"))
    def test_senRTAssociation(self):
        resp = self.client.post("/api/touyan/senRTAssociation",
                                data=json.dumps({"url":"http://www.baidu.com","events":["節點1","節點2"],"news_pubtime":1591085501}),
                                headers={"Authorization": self.cookie}, content_type='application/json')
        resp_json = json.loads(resp.data)
        self.assertEqual("0", resp_json.get("code"), resp_json)

命令:

cd /home/luslin/python-workspace/dataAccess/ 切換到項目目錄
source env/bin/activate 切換運行環境
python -m unittest /home/luslin/python-workspace/dataAccess/tests/test.py 運行測試

結果:

...............
----------------------------------------------------------------------
Ran 15 tests in 125.780s

OK
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章