日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

unittest单元测试框架—基本实例

發(fā)布時(shí)間:2025/3/21 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 unittest单元测试框架—基本实例 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

代碼展示

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': '賬號(hào)或者密碼不正確'}, self.login('kobe', '888'))def test_not_username(self):self.assertEqual({'code': 201, 'msg': '賬號(hào)不能為空'}, 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': '賬號(hào)和密碼不能為空'}, 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': '賬號(hào)或者密碼不正確'}if username == 'kobe' and password == '':return {'code': 201, 'msg': '密碼不能為空'}if username == '' and password == '666':return {'code': 201, 'msg': '賬號(hào)不能為空'}if username == '' and password == '':return {'code': 201, 'msg': '賬號(hào)和密碼不能為空'}if __name__ == '__main__':unittest.main()

執(zhí)行結(jié)果:

繼承unittest.TestCase就創(chuàng)建了一個(gè)測(cè)試樣例,類中定義的測(cè)試方法,這些方法的命名都以 test 開(kāi)頭。這個(gè)命名約定告訴測(cè)試運(yùn)行者類的哪些方法表示測(cè)試。

每個(gè)測(cè)試的關(guān)鍵是:調(diào)用 assertEqual() 來(lái)檢查預(yù)期的輸出

通過(guò) setUp() 和 tearDown() 方法,可以設(shè)置測(cè)試開(kāi)始前與完成后需要執(zhí)行的指令。

unittest.main() 提供了一個(gè)測(cè)試腳本的命令行接口

總結(jié)

以上是生活随笔為你收集整理的unittest单元测试框架—基本实例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。