setUp():准备环境,执行每个测试用例的前置条件;
tearDown():环境还原,执行每个测试用例的后置条件;
setUpClass():必须使用@classmethod装饰器,所有case执行的前置条件,只运行一次;
tearDownClass():必须使用@classmethod装饰器,所有case运行完后只运行一次;
import unittest
#要继承unittest.TestCase
class CalcTestcase(unittest.TestCase):
def setUp(self) -> None:
print("我是setUp")
def test01(self):
print("我是的测试方法test01")
def test02(self):
print("我是的测试方法test02")
def test03(self):
print("我是的测试方法test03")
def tearDown(self) -> None:
print("我是tearDown")
@classmethod
def setUpClass(cls) -> None:
print("我是setUpClass")
@classmethod
def tearDownClass(cls) -> None:
print("我是tearDownClass")
if __name__ == '__main__':
unittest.main()
#设置套件
# suite = unittest.TestSuite()
# #把测试方法添加到集合中,然后循环取值,在添加到套件里面输出
# list = ["test01","test02","test03"]
# for i in list:
# suite.addTest(CalcTestcase(i))
开发代码:
class CalClass(object):
def add(self,a,b):
c = a+b
return c
def reduct(self,a,b):
c = a-b
return c
# c = CalClass()
# print(c.reduct(1,2))
if __name__ == '__main__':
c = CalClass()
print(c.reduct(1, 2))
进行测试开发代码:
import unittest
from demo.kaifa import CalClass
c = CalClass()
rr = c.add(2,3)
class CalcTestClass(unittest.TestCase):
def setUp(self) -> None:
print("我是setUp")
def test01(self):
self.assertEqual(5,rr)
def tearDown(self) -> None:
print("我是tearDown")
if __name__ == '__main__':
unittest.main()
# suite = unittest.TestSuite()
# lists = ["test01","test02","test03"]
# for i in lists:
# suite.addTest(CalcTestClass(i))
生成测试报告
加减操作:
开发代码:
#加减
# class CalClass(object):
# def add(self,a,b):
# c = a+b
# return c
# def reduct(self,a,b):
# c = a-b
# return c
# # c = CalClass()
# # print(c.reduct(1,2))
# if __name__ == '__main__':
# c = CalClass()
# print(c.reduct(1, 2))
生成测试报告
加减操作:
开发代码:
#加减
# class CalClass(object):
# def add(self,a,b):
# c = a+b
# return c
# def reduct(self,a,b):
# c = a-b
# return c
# # c = CalClass()
# # print(c.reduct(1,2))
# if __name__ == '__main__':
# c = CalClass()
# print(c.reduct(1, 2))
进行测试开发代码:
import unittest
from demo.kaifa import CalClass
c = CalClass()
rr = c.add(2,3)
cc=c.reduct(6,2)
class CalcTestClass(unittest.TestCase):
def setUp(self) -> None:
print("我是setUp")
def test01(self):
self.assertEqual(5,rr)
def test02(self):
self.assertEqual(9,cc)
def tearDown(self) -> None:
print("我是tearDown")
if __name__ == '__main__':
unittest.main()
乘除操作:
开发代码
class CalClass(object):
def add(self,a,b):
c = a*b
return c
def reduct(self,a,b):
c = a/b
return c
if __name__ == '__main__':
c = CalClass()
print(c.reduct(1, 2))
进行测试开发代码:
import unittest
from demo.kaifa import CalClass
c = CalClass()
rr = c.add(2,3)
cc=c.reduct(6,2)
class CalcTestClass(unittest.TestCase):
def setUp(self) -> None:
print("我是setUp")
def test01(self):
self.assertEqual(6,rr)
def test02(self):
self.assertEqual(4,cc)
def tearDown(self) -> None:
print("我是tearDown")
if __name__ == '__main__':
unittest.main()
xml文件读取数据进行单元测试并生成html格式的测试报告
import unittest
from demo.kaifa import CalClass
from readdate.readxml import Readxml
c = CalClass()
r = Readxml()
aa1 = r.read_xml("../data/testxml.xml","add","add1")
aa2 = r.read_xml("../data/testxml.xml","add","add2")
aa3 = r.read_xml("../data/testxml.xml","add","add3")
dd = c.add(int(aa1),int(aa2))
class CalcTestClass(unittest.TestCase):
def setUp(self) -> None:
print("我是setUp")
def test01(self):
self.assertEqual(int(aa3),dd)
# def test02(self):
# self.assertEqual(4,cc)
def tearDown(self) -> None:
print("我是tearDown")
if __name__ == '__main__':
unittest.main()
csv文件读取数据进行单元测试并生成html格式的测试报告
import unittest
from demo.kaifa import CalClass
from readdate.readcsv import ReadCsv
r = ReadCsv()
lists = r.read_csv()
c = CalClass()
class CalcTestClass(unittest.TestCase):
def setUp(self) -> None:
print("我是setUp")
def test01(self):
for csv_i in lists:
add1 = c.add(int(csv_i[0]),int(csv_i[1]))
self.assertEqual(add1,int(csv_i[2]))
def tearDown(self) -> None:
print("我是tearDown")
if __name__ == '__main__':
unittest.main()