python: unittest
pip3 install parameterized
pip3 install pytest
pip3 install pytest-html
pip3 install coverage
pip3 install html-testRunner
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | import unittest import unittest.mock import unittest.loader import unittest.case from unittest.mock import MagicMock import sys import webbrowser class SomeWork( object ): """ """ def initenv( self ): """ :return: """ print ( "初始化环境" ) def clearenv( self ): """ :return: """ print ( "清理环境配置" ) @staticmethod def workPad(): """""" print ( "工作中" ) class Test(unittest.TestCase): """ """ somework = None someWork = None @classmethod def setUpClass( cls ): """ :return: """ cls .somework = SomeWork() cls .somework.initenv() cls .someWork = SomeWork def testcase( self ): """ :return: """ self .somework.initenv() print ( "this is case" ) def testcase2( self ): """ :return: """ self .someWork.workPad() #SomeWork.workPad() print ( "this is case2" ) @classmethod def tearDownClass( cls ): """ :return: """ #cls.somework() cls .someWork def main( self , out = sys.stderr, verbosity = 2 ): """ :param out: :param verbosity: :return: """ loader = unittest.TestLoader() suite = loader.loadTestsFromModule(sys.modules[__name__]) unittest.TextTestRunner(out, verbosity = verbosity).run(suite) if __name__ = = '__main__' : # 写入报告 with open ( 'testclass.txt' , 'w' ) as f: t = Test() t.main(f) #os.system(r'notepad testclass.txt') webbrowser. open ( 'testclass.txt' ,new = 0 ,autoraise = True ) #打开文档 unittest.main() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | # encoding: utf-8 # 版权所有 2024 涂聚文有限公司 # 许可信息查看: # 描述: # Author : geovindu,Geovin Du 涂聚文. # IDE : PyCharm 2023.1 python 3.11 # Datetime : 2024/10/12 6:29 # User : geovindu # Product : PyCharm # Project : TechnologyGame # File : cal.py # explain : 学习 __metaclass_ = type class calculator( object ): """ """ def __init__( self ,a: int ,b: int ): self ._numa = a self ._numb = b @property def numa( self ): """ :return: """ return self ._numa @numa .setter def numa( self ,a: int ): """ :param a: :return: """ self ._numa = a @property def numb( self ): """ :return: """ return self ._numb @numb .setter def numb( self ,b: int ): """ :param b: :return: """ self ._numb = b def Add( self ): """ :return: """ return self ._numa + self ._numb def Subs( self ): """ :return: """ return self ._numa - self ._numb def Multiply( self ): """ :return: """ return self ._numa * self ._numb def Divide( self ): """ :return: """ try : return self ._numa / self ._numb except ZeroDivisionError: print ( 'zero division errror' ) return 0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | # encoding: utf-8 # 版权所有 2024 涂聚文有限公司 # 许可信息查看: # 描述: pip3 install parameterized pip3 install pytest pip3 install pytest-html pip3 install coverage pip3 install html-testRunner # Author : geovindu,Geovin Du 涂聚文. # IDE : PyCharm 2023.1 python 3.11 # Datetime : 2024/10/12 6:29 # User : geovindu # Product : PyCharm # Project : TechnologyGame # File : testcal.py # explain : 学习 import unittest import unittest.mock import unittest.loader import unittest.case from unittest.mock import MagicMock import sys import os from cal import calculator import webbrowser import HTMLTestRunner import asyncio class calulatorUnittest(unittest.TestCase): """ 单元测麻 """ def setUp( self ) - > None : """ :return: """ print ( 'Test Start' ) def testBase( self ): """ :return: """ j = calculator( 4 , 2 ) self .assertEquals(j.Add(), 6 ) self .assertEquals(j.Subs(), 2 ) self .assertEquals(j.Multiply(), 8 ) self .assertEquals(j.Divide(), 2 ) def testMaxNumber( self ): """ :return: """ j = calculator( 9223372036854775808 , 9223372036854775808 ) self .assertEquals(j.Multiply(), 850705591730234615865843651865843651857942052864 ) mydata = [[ 4 , 2 , 2 ], [ 2 , 4 , - 2 ], [ 4 , 4 , 0 ]] n = 0 for i in mydata: j = calculator(mydata[n][ 0 ], mydata[n][ 1 ]) self .assertEquals(j.Multiply(), mydata[n][ 2 ]) n + = 1 def testMultiply( self ): """ :return: """ mydata = [[ 4 , 2 , 8 ], [ 4 , - 2 , - 8 ], [ - 4 , - 4 , 16 ],[ - 5 , 5 , - 25 ]] n = 0 judge = True for i in mydata: j = calculator(mydata[n][ 0 ], mydata[n][ 1 ]) self .assertEquals(j.Multiply(), mydata[n][ 2 ]) n + = 1 def testDivide( self ): """ :return: """ j = calculator( 4 , 0 ) self .assertEquals(j.Divide(), 0 ) def tearDown( self ) - > None : """ :return: """ print ( 'Test End' ) def main( self , out = sys.stderr, verbosity = 2 ): """ :param out: :param verbosity: :return: """ loader = unittest.TestLoader() suite = loader.loadTestsFromModule(sys.modules[__name__]) unittest.TextTestRunner(out, verbosity = verbosity).run(suite) class calulatorTestCase(unittest.TestCase): """ """ cals = None @classmethod def setUpClass( cls ): """ :return: """ cls .cals = calculator( 4 , 2 ) def testcaseBase( self ): """ :return: """ self .cals = calculator( 4 , 2 ) self .cals.Add() self .cals.Subs() self .cals.Multiply() self .cals.Divide() self .cals.Add = MagicMock() self .cals.Subs = MagicMock() self .cals.Multiply = MagicMock() self .cals.Divide = MagicMock() print ( 'Add' ) def testcaseMaxNumber( self ): """ :return: """ self .cals = calculator( 9223372036854775808 , 9223372036854775808 ) self .cals.Multiply() self .cals.Multiply = MagicMock() print ( 'Subs' ) def testcaseMultiply( self ): """ :return: """ mydata = [[ 4 , 2 , 8 ], [ 4 , - 2 , - 8 ], [ - 4 , - 4 , 16 ], [ - 5 , 5 , - 25 ]] n = 0 judge = True for i in mydata: self .cals = calculator(mydata[n][ 0 ], mydata[n][ 1 ]) self .cals.Multiply() self .cals.Multiply = MagicMock() n + = 1 print ( 'Multiply' ) def testcaseDivide( self ): """ :return: """ self .cals = calculator( 4 , 0 ) self .cals.Divide() self .cals.Divide = MagicMock() print ( 'Divide' ) @classmethod def tearDownClass( cls ): """ """ cls .cal() @classmethod def main( cls , out = sys.stderr, verbosity = 2 ): """ :param out: :param verbosity: :return: """ loader = unittest.TestLoader() suite = loader.loadTestsFromModule(sys.modules[__name__]) unittest.TextTestRunner(out, verbosity = verbosity).run(suite) async def writehtml(): """ 寫入報告 html """ suite = unittest.TestSuite() suite.addTest(calulatorUnittest( "testBase" )) suite.addTest(calulatorUnittest( "testMaxNumber" )) suite.addTest(calulatorUnittest( "testMultiply" )) suite.addTest(calulatorUnittest( "testDivide" )) suite.addTest(calulatorTestCase( "testcaseBase" )) suite.addTest(calulatorTestCase( "testcaseMaxNumber" )) suite.addTest(calulatorTestCase( "testcaseMultiply" )) suite.addTest(calulatorTestCase( "testcaseDivide" )) # 1 #runner = unittest.TextTestRunner() #runner.run(suite) # 2 filepath = 'testcal.html' fp = open (filepath, 'wb' ) #寫入報告 html runner = HTMLTestRunner.HTMLTestRunner(stream = fp,title = u 'cal.py test report' ,author = '涂聚文' ,description = u 'unit test case' ) runner.run(suite) fp.close() webbrowser. open ( 'testcal.html' ,new = 0 , autoraise = True ) #open html file async def writetext(): """ 寫入報告 txt """ if os.path.exists( 'testcal.txt' ): with open ( 'testcal.txt' , 'w' ) as f: t = calulatorUnittest() t.main(f) else : with open ( 'testcal.txt' , 'a' ) as f: t = calulatorTestCase() t.main(f) webbrowser. open ( 'testcal.txt' ,new = 0 , autoraise = True ) unittest.main() if __name__ = = '__main__' : """ """ yn = input ( "write html file: Y/N?" ) if yn = = 'Y' or yn = = 'y' : # 寫入報告 html asyncio.run(writehtml()) else : # 寫入報告 txt asyncio.run(writetext()) |
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
2022-10-17 CSharp: Detection Platform in donet core 6
2017-10-17 sql server:alter database name
2009-10-17 yui cookie Dynamically Change Text Size Using Javascript 动态设置字体大小,写入Cookie