python unittest单元测试
unittest介绍:它是python中自带的单元测试模块,用来做单元测试,它里面封装好了一些校验返回结果方法(断言)和一些用例执行前的初始化操作。
unittest中最核心的部分是:TestFixture、TestCase、TestSuite、TestRunner
TestFixture:用于测试环境的准备,销毁,还原。 主要方法:setUp(),tearDown(),setUpClass(),tearDownClass()
TestCase: 测试用例
TestSuite:组件,测试用例的集合
TestRunner:执行测试用例
import unittest # 导包 class UnittestTestClass(unittest.TestCase): # 继承unittest.TestCase def setUp(self): # unittest组件之一 print("------------start-----------------") def test001(self): # unittest的测试用例,必须以test开头,后面根据ascii码执行 print("It is first testcase") def test002(self): a = 2 b = 5 c = a+b self.assertEqual(c, 7) # unittest 自带的断言,类似的还有很多,该处是判断是否相等 print("It is second testcase") def tearDown(self): # unittest组件之一 结束组件 print("------------end-------------------") if __name__ =="__main__": unittest.main() #调用测试用例中以test开头的方法
准备工作:提前下载HTMLTestRunner.py文件,
HTML格式就是HTML TestRunner了,它是python标准库unittest的一个扩展,可以生成直观的HTML报告。
步骤:
# demo:开发写的需要测试的代码; # html:下载的report扩展包; # test001:存放单元测试脚本的代码;
# aaa.py class BaseClass(object): def add(self, a, b): return a * b def reduce(self, a, b): return a / b base = BaseClass() base.reduce(2, 5) base.add(4, 6)
# bianjireport.py import unittest from html.HTMLTestRunner import HTMLTestRunner from test001.unittest01 import UnittestTestClass class BjClass(object): def ceshi(self): suite = unittest.TestSuite() case_list = ["test001", "test002"] for case in case_list: suite.addTest(UnittestTestClass(case)) with open("../report.html", "wb") as f: HTMLTestRunner( stream=f, title="单元测试", description="测试一期", verbosity=2 ).run(suite) if __name__ == '__main__': ceshi = BjClass() ceshi.ceshi()
# unittest01.py import unittest from demo.aaa import BaseClass base = BaseClass() one = base.add(2, 8) tow = base.reduce(2, 8) class UnittestTestClass(unittest.TestCase): # 继承TestCase def setUp(self): print("------------start-----------------") def test001(self): self.assertEqual(10, 9) def test002(self): self.assertEqual(-6, 0) print("It is second testcase") def tearDown(self): print("------------end-------------------") if __name__ == '__main__': unittest.main()
# test.xml xml数据存放位置 # readXML 读取xml文件数据的代码 # testxml 编写读取测试用例的测试
# test.xml <?xml version="1.0"?> <note> <cheng> <cheng1>2</cheng1> <cheng2>3</cheng2> <cheng3>6</cheng3> </cheng> <chu> <chu1>5</chu1> <chu2>6</chu2> <chu3>3</chu3> </chu> </note>
# readxml.py from xml.dom import minidom # 导入xml文件读取的包 class ReadXmlClass(): def read_xml(self, filename, onename, twoname): root = minidom.parse(filename) firstname = root.getElementsByTagName(onename)[0] secondnode = firstname.getElementsByTagName(twoname)[0].firstChild.data return secondnode # readxml = ReadXmlClass() # print(readxml.read_xml("../html/test.xml", "cheng", "cheng3"))
# testxml.py import unittest from demo.aaa import * # 导入开发写的代码 from readdata.readxml import * # 导入读取xml文件的脚本 readxml = ReadXmlClass() # 实例化 zz = int(readxml.read_xml("../html/test.xml", "cheng", "cheng1")) # 取值 xx = int(readxml.read_xml("../html/test.xml", "cheng", "cheng2")) yy = int(readxml.read_xml("../html/test.xml", "cheng", "cheng3")) c = BaseClass() rr = c.cheng(zz, xx) class TestXml(unittest.TestCase): def test001(self): self.assertEqual(rr, yy) # 断点验证数据的正确性 if __name__ == '__main__': unittest.main() # main()方法直接运行测试用例
思路和xml一样
文件命名和xml相似
# test.csv 2,5,7 2,4,8
# readcsv.py import csv # 导入CSV模块 class ReadCsvClass(): def read_csv(self): list01 = [] # 定义一个空列表 c = csv.reader(open("../html/test.csv", "r")) # 得到CSV文件 for i in c: list01.append(i) return list01 # r = ReadCsvClass() # aa = r.read_csv() # print(aa)
# testcsv.py import unittest from readdata.readcsv import * # 导入数据 from demo.aaa import * r = ReadCsvClass() # 实例化 lists = r.read_csv() # 拿到返回的列表 c = BaseClass() class TestCsv(unittest.TestCase): def testcsv(self): for i in lists: rr = c.cheng(int(i[0]), int(i[1])) self.assertEqual(rr, int(i[2])) # unittest断点 if __name__ == '__main__': unittest.main()