Python单元测试之unittest读取xml文件

Python单元测试之unittest读取xml文件详细步骤:1.获取开发的代码2.导入数据3.读取数据4.编写测试用例代码(导入开发实例,需要的数据)5.创建测试套件,根据HTMLTestRunner生成报告1.开发代码案例(cczs.py)classCczs(object):def__init__(self,a,b):

大家好,欢迎来到IT知识分享网。

详细步骤:

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

 

Python单元测试之unittest读取xml文件

 

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/30481.html

(0)
上一篇 2023-10-13 18:45
下一篇 2023-10-22 15:33

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信