unittest单元测试框架—基本实例
生活随笔
收集整理的這篇文章主要介紹了
unittest单元测试框架—基本实例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
代碼展示
import unittestclass LoginTestCase(unittest.TestCase):def test_login_success(self):self.assertEqual({'code': 200, 'msg': '登錄成功'}, self.login('kobe', '666'))def test_login_fail(self):self.assertEqual({'code': 201, 'msg': '賬號或者密碼不正確'}, self.login('kobe', '888'))def test_not_username(self):self.assertEqual({'code': 201, 'msg': '賬號不能為空'}, self.login('', '666'))def test_not_password(self):self.assertEqual({'code': 201, 'msg': '密碼不能為空'}, self.login('kobe', ''))def test_not_username_password(self):self.assertEqual({'code': 201, 'msg': '賬號和密碼不能為空'}, self.login('', ''))def login(self, username, password):if username == 'kobe' and password == '666':return {'code': 200, 'msg': '登錄成功'}if username == 'kobe' and username != '' and password != '666' and password != '':return {'code': 201, 'msg': '賬號或者密碼不正確'}if username == 'kobe' and password == '':return {'code': 201, 'msg': '密碼不能為空'}if username == '' and password == '666':return {'code': 201, 'msg': '賬號不能為空'}if username == '' and password == '':return {'code': 201, 'msg': '賬號和密碼不能為空'}if __name__ == '__main__':unittest.main()執行結果:
繼承unittest.TestCase就創建了一個測試樣例,類中定義的測試方法,這些方法的命名都以 test 開頭。這個命名約定告訴測試運行者類的哪些方法表示測試。
每個測試的關鍵是:調用 assertEqual() 來檢查預期的輸出
通過 setUp() 和 tearDown() 方法,可以設置測試開始前與完成后需要執行的指令。
unittest.main() 提供了一個測試腳本的命令行接口
總結
以上是生活随笔為你收集整理的unittest单元测试框架—基本实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Flask项目2】python对象分页
- 下一篇: python—unittest—数据驱动