060 Python文法のテスト
1755 ワード
ドキュメントテスト
# Author:Luo
def add(a, b):
"""
,
:param a:
:param b:
:return: a+b
>>> print(add(10,20))
30
"""
return a+b
import doctest
# print(add(10,10))
doctest.testmod()
ユニットテスト
# Author:Luo
def add(x, y):
return x + y
def sub(x, y):
return x - y
def mul(x, y):
return x * y
# def sub(x,y):
# return x/y
import unittest
class Test(unittest.TestCase):
def setUp(self): #
pass
def tearDown(self): #
pass
@classmethod
def setUpClass(cls): # @classmethod , test
pass
@classmethod
def tearDownClass(cls): # @classmethod , test
pass
def testAdd(self):
self.assertEqual(first=add(1, 2), second=3, msg=" ")
def testSub(self):
self.assertEqual(first=sub(2, 5), second=-3, msg=" ")
def testMul(self):
self.assertEqual(first=mul(7, 8), second=56, msg=" ?")
if __name__ == "__main__":
unittest.main() #
インスタンス化テストクラス
class data:
def __init__(self):
pass
def add(self,a,b):
return a+b
def sub(self, a, b):
return a - b
import unittest
import myclass
class mytest(unittest.TestCase):
def setUp(self):
self.tclass=myclass.data()#
pass
def tearDown(self):
pass
def testsum(self):
self.assertEquals(self.tclass.add(1,2),31," ")
pass
def testsub(self):
self.assertEquals(self.tclass.sub(1, 2), -11, " ")
pass
if __name__=="__main__":
unittest.main()