Python learning notes (generating xml) and python learning notes
I want to add some functions to the framework.
The first thing that comes to mind is to generate a test report.
The format of the generated file is involved here.
I am going to generate an xml file.
I learned the following code:
1 #! /Usr/bin/env python 2 #-*-coding: utf_8-*-3 4 from xml. dom import minidom 5 6 xml = minidom. document () 7 xml. appendChild (xml. createComment ("Test Report") 8 caselist = xml. createElement ("caselist") 9 xml. appendChild (caselist) 10 11 12 def add_xml (newxml): 13 case = xml. createElement ("case") 14 case. setAttribute ("id", newxml ["id"]) 15 16 17 name = xml. createElement ("name") 18 name. appendChild (xml. createTextNode (newxml ["name"]) 19 code = xml. createElement ("code") 20 code. appendChild (xml. createTextNode (newxml ["code"]) 21 result = xml. createElement ("result") 22 result. appendChild (xml. createTextNode (newxml ["result"]) 23 time = xml. createElement ("time") 24 time. appendChild (xml. createTextNode (newxml ["time"]) 25 26 case. appendChild (name) 27 case. appendChild (code) 28 case. appendChild (result) 29 case. appendChild (time) 30 31 caselist. appendChild (case) 32 33 34 add_xml ({"id": "1001", "name": "The logon username and password are correct", "code": "200 ", "result": "tested", "time": "2016-4-13"}) 35 filename = file ("case. xml "," w ") 36 xml. writexml (filename) 37 filename. close ()
The generated xml file is as follows:
1 <? Xml version = "1.0"?> 2 3 <! -- Test Report --> 4-<caselist>-<case id = "1001"> <name> correct logon username and password </name> <code> 200 </code> <result> test passed </result> <time> </case> </caselist>