详细步骤:
1. 获取开发的代码
2.导入数据
3.读取数据
4.编写测试用例代码(导入开发实例,需要的数据)
5.创建测试套件,根据HTMLTestRunner生成报告
class Cczs(object): def __init__(self, a, b): self.a = a self.b = b def multiplication(self): '''乘法''' m = self.a * self.b return m def division(self): '''除法''' d = self.a / self.b return d
<node> <multiplication> <multiplication1>1</multiplication1> <multiplication2>2</multiplication2> <multiplication3>2</multiplication3> </multiplication> <division> <division1>6</division1> <division2>2</division2> <division3>1</division3> </division> </node>
from xml.dom import minidom class Readxml(): def read_xml_multiplication(self, filename, onename, twoname): root = minidom.parse(filename) firstnode = root.getElementsByTagName(onename)[0] secondnode = firstnode.getElementsByTagName(twoname)[0].firstChild.data return secondnode def read_xml_division(self, filename, onename, twoname): root = minidom.parse(filename) firstnode = root.getElementsByTagName(onename)[-1] secondnode = firstnode.getElementsByTagName(twoname)[0].firstChild.data return secondnode # readxml = Readxml() # readxml.read_xml_multiplication('D:\pythonstudy\unittest\datedemo\testxml.xml','multiplication','multiplication1') # readxml = Readxml() # readxml.read_xml_division('D:\pythonstudy\unittest\datedemo\testxml.xml','division','division1')
import unittest from demo.cczs import Cczs from readdemo.readxml import Readxml readxml = Readxml() '''实例化读xml''' '''获取到的乘法数据''' num_m1 = int(readxml.read_xml_multiplication('D:\pythonstudy\unittest\datedemo\testxml.xml', 'multiplication', 'multiplication1')) num_m2 = int(readxml.read_xml_multiplication('D:\pythonstudy\unittest\datedemo\testxml.xml', 'multiplication', 'multiplication2')) num_m3 = int(readxml.read_xml_multiplication('D:\pythonstudy\unittest\datedemo\testxml.xml', 'multiplication', 'multiplication3')) '''获取到的除法数据''' num_d1 = int(readxml.read_xml_division('D:\pythonstudy\unittest\datedemo\testxml.xml','division','division1')) num_d2 = int(readxml.read_xml_division('D:\pythonstudy\unittest\datedemo\testxml.xml','division','division2')) num_d3 = int(readxml.read_xml_division('D:\pythonstudy\unittest\datedemo\testxml.xml','division','division3')) cc = Cczs(num_m1, num_m2).multiplication() # 实例化要测开发类乘法 dd = Cczs(num_d1, num_d2).division() # 实例化要测开发类除法 class Testxml(unittest.TestCase): '''测试xml类''' def test_multiplication(self): '''测试乘法''' self.assertEqual(cc, num_m3) def test_division(self): '''测试除法''' self.assertEqual(dd, num_d3)
from datedemo.HTMLTestRunner import HTMLTestRunner class TXmlHtml(): def txmlhtml(self): suite = unittest.TestSuite() # 创建测试套件 case_list = ['test_multiplication', 'test_division'] for case in case_list: suite.addTest(Testxml(case)) with open("../reportxml.html", "wb") as f: HTMLTestRunner( stream=f, title="单元测试xml", description="乘法除法测试", verbosity=2 ).run(suite) if __name__ == '__main__': t =TXmlHtml() t.txmlhtml()