zoukankan      html  css  js  c++  java
  • Python单元测试之unittest读取xml文件

    详细步骤:

    1. 获取开发的代码

    2.导入数据

    3.读取数据

    4.编写测试用例代码(导入开发实例,需要的数据)

    5.创建测试套件,根据HTMLTestRunner生成报告
    1.开发代码案例(cczs.py)
    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
    2.导入数据testxml.xml
    <node>
        <multiplication>
            <multiplication1>1</multiplication1>
            <multiplication2>2</multiplication2>
            <multiplication3>2</multiplication3>
        </multiplication>
        <division>
            <division1>6</division1>
            <division2>2</division2>
            <division3>1</division3>
        </division>
    </node>
    3.读取数据(readxml.py)
    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')
    4.编写测试用例代码(导入开发实例,需要的数据)testxml.py
    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)
    5..创建测试套件,根据HTMLTestRunner生成报告
    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()

    落后就要吃土,努力吧,骚年!!!
  • 相关阅读:
    【经典】仙岛求药(一)
    6月份学习记录
    YZM的全排列
    最长公共子序列的长度
    20612统计八连块
    积木城堡
    不同组合数求和
    50136142WXY的百度地图
    50095106扔核弹
    【其他】关于海岛帝国互测系列赛总结
  • 原文地址:https://www.cnblogs.com/u-damowang1/p/14847489.html
Copyright © 2011-2022 走看看