例题取用登录模块:代码如下
def login_check(username,password): ''' 登录校验的函数 :param username:账号 :param password: 密码 :return: ''' if 6<=len(password)<=18: if username=='admin' and password=='123456': return {'code':0,'msg':'登录成功'} else: return {'code':1,'msg':'账号密码不正确'} else: return {'code':1,'msg':'密码长度在6-18之间'}
单元测试是什么:
单元测试(unit testing),是指对软件中的最小可测试单元进行检查和验证
如何进行单元测试:
第一我们得写出测试用例,而测试用例主要的要素为(编号,标题,前置条件,操作步骤,预期结果,实际结果)
unittest框架核心的四个概念
1.TestCase
:一个testcase的实例就是一个测试用例
2.TestSuite
:多个测试用例集合在一起
3.TextTestRunner
:用来执行测试用例
4.fifixture
:测试用例环境的搭建和销毁,测试前环境的搭建(setUp),执行测试代码(run)以及测试后环境
的还原(tearDown)
登录测试用例标题如下等等:
1.用户名密码正确
2.用户名不存在
3. 密码小于6位
4. 密码大于18位
5. 用户名为空然后我们执行测试用例
import unittest #引用单元测试模块 from day8.登录测试代码 import login_check #将登陆模块代码引入 class Login_test(unittest.TestCase): def setUp(self): print("执行用例前都会执行这个方法,类似前置条件") def tearDown(self): print("用例执行完后 都会执行该方法") def test_login_success(self): #登录成功用例 result=login_check('admin','123456') #这个是实际结果 expect='登录成功' #这个是预期结果 self.assertEqual(result['msg'],expect) #将实际结果和预期结果做比较,输出完全相同则通过 输出结果是OK
def test_username_nonentity(self): #用户名不存在用例 result=login_check('admininin','111222333') expect='用户名不存在' self.assertEqual(expect,result['msg']) #反之,如果不通过输出结果如下图
注意:
一个def方法就是一个测试用例
方法名就是用例的标题,用例方法名一定要已text_开头
相关学习推荐:python视频教程
以上是实例解析Python单元测试及unittest框架用法的详细内容。更多信息请关注PHP中文网其他相关文章!

Pythonarrayssupportvariousoperations:1)Slicingextractssubsets,2)Appending/Extendingaddselements,3)Insertingplaceselementsatspecificpositions,4)Removingdeleteselements,5)Sorting/Reversingchangesorder,and6)Listcomprehensionscreatenewlistsbasedonexistin

NumPyarraysareessentialforapplicationsrequiringefficientnumericalcomputationsanddatamanipulation.Theyarecrucialindatascience,machinelearning,physics,engineering,andfinanceduetotheirabilitytohandlelarge-scaledataefficiently.Forexample,infinancialanaly

useanArray.ArarayoveralistinpythonwhendeAlingwithHomeSdata,performance-Caliticalcode,orinterFacingWithCcccode.1)同质性data:arrayssavememorywithtypedelements.2)绩效code-performance-clitionalcode-clitadialcode-critical-clitical-clitical-clitical-clitaine code:araysofferferbetterperperperformenterperformanceformanceformancefornalumericalicalialical.3)

不,notalllistoperationsareSupportedByArrays,andviceversa.1)arraysdonotsupportdynamicoperationslikeappendorinsertwithoutresizing,wheremactssperformance.2)listssdonotguaranteeconeeconeconstanttanttanttanttanttanttanttanttimecomplecomecomecomplecomecomecomecomecomecomplecomectaccesslikearrikearraysodo。

toAccesselementsInapythonlist,useIndIndexing,负索引,切片,口头化。1)indexingStartSat0.2)否定indexingAccessesessessessesfomtheend.3)slicingextractsportions.4)iterationerationUsistorationUsisturessoreTionsforloopsoreNumeratorseforeporloopsorenumerate.alwaysCheckListListListListlentePtotoVoidToavoIndexIndexIndexIndexIndexIndExerror。

Arraysinpython,尤其是Vianumpy,ArecrucialInsCientificComputingfortheireftheireffertheireffertheirefferthe.1)Heasuedfornumerericalicerationalation,dataAnalysis和Machinelearning.2)Numpy'Simpy'Simpy'simplementIncressionSressirestrionsfasteroperoperoperationspasterationspasterationspasterationspasterationspasterationsthanpythonlists.3)inthanypythonlists.3)andAreseNableAblequick

你可以通过使用pyenv、venv和Anaconda来管理不同的Python版本。1)使用pyenv管理多个Python版本:安装pyenv,设置全局和本地版本。2)使用venv创建虚拟环境以隔离项目依赖。3)使用Anaconda管理数据科学项目中的Python版本。4)保留系统Python用于系统级任务。通过这些工具和策略,你可以有效地管理不同版本的Python,确保项目顺利运行。

numpyarrayshaveseveraladagesoverandastardandpythonarrays:1)基于基于duetoc的iMplation,2)2)他们的aremoremoremorymorymoremorymoremorymoremorymoremoremory,尤其是WithlargedAtasets和3)效率化,效率化,矢量化函数函数函数函数构成和稳定性构成和稳定性的操作,制造


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

Atom编辑器mac版下载
最流行的的开源编辑器

记事本++7.3.1
好用且免费的代码编辑器

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),