python单元测试之unittest

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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值