最近在使用Testlink时,发现导入的用例是xml格式,且没有合适的工具转成excel格式,xml使用excel打开显示的东西也太多,网上也有相关工具转成csv格式的,结果也不合人意。

那求人不如尔己,自己写一个吧

需要用到的模块有:xml.dom.minidom(python自带)、xlwt

使用版本:

python:2.7.5

xlwt:1.0.0

一、先分析Testlink XML格式:

这是一个有两级testusuit的典型的testlink用例结构,我们只需要取testsuite name,testcase name,preconditions,actions,expectedresults

二、程序如下:

#coding:utf-8
'''
Created on 2015-8-20@author: Administrator
'''
'''
'''
import xml.etree.cElementTree as ET
import xml.dom.minidom as xx
import os,xlwt,datetimeworkbook=xlwt.Workbook(encoding="utf-8")
#
booksheet=workbook.add_sheet(u'sheet_1')
booksheet.col(0).width= 5120
booksheet.col(1).width= 5120
booksheet.col(2).width= 5120
booksheet.col(3).width= 5120
booksheet.col(4).width= 5120
booksheet.col(5).width= 5120dom=xx.parse(r'D:\\Python27\test.xml')
root = dom.documentElement
row=1
col=1borders=xlwt.Borders()
borders.left=1
borders.right=1
borders.top=1
borders.bottom=1style = xlwt.easyxf('align: wrap on,vert centre, horiz center')  #自动换行、水平居中、垂直居中
#设置标题的格式,字体方宋、加粗、背景色:菊黄#测试项的标题
title=xlwt.easyxf(u'font:name 仿宋,height 240 ,colour_index black, bold on, italic off; align: wrap on, vert centre, horiz center;pattern: pattern solid, fore_colour light_orange;')
item='测试项'
Subitem='测试分项'
CaseTitle='测试用例标题'
Condition='预置条件'
actions='操作步骤'
Result='预期结果'
booksheet.write(0,0,item,title)
booksheet.write(0,1,Subitem,title)
booksheet.write(0,2,CaseTitle,title)
booksheet.write(0,3,Condition,title)
booksheet.write(0,4,actions,title)
booksheet.write(0,5,Result,title)
#冻结首行
booksheet.panes_frozen=True
booksheet.horz_split_pos= 1#一级目录
for i in root.childNodes:testsuite=i.getAttribute('name').strip()#print testsuite#print testsuite'''写测试项'''print "row is :",rowbooksheet.write(row,col,testsuite,style)#二级目录for dd in i.childNodes:print "       %s" % dd.getAttribute('name')testsuite2=dd.getAttribute('name')if not dd.getElementsByTagName('testcase'):print "Testcase is %s" % testsuite2row=row+1booksheet.write(row,2,testsuite2,style)   #写测试分项row=row+1booksheet.write(row,1,testsuite2,style)itemlist=dd.getElementsByTagName('testcase')for subb in itemlist:#print "                 %s" %  subb.getAttribute('name')testcase=subb.getAttribute('name')row=row+1booksheet.write(row,2,testcase,style)ilist=subb.getElementsByTagName('preconditions')for ii in ilist:preconditions=ii.firstChild.data.replace("<br />"," ")col=col+1booksheet.write(row,3,preconditions,style)steplist=subb.getElementsByTagName('actions')#print steplistfor step in steplist:actions=step.firstChild.data.replace("<br />"," ")col=col+1booksheet.write(row,4,actions,style)#print "测试步骤:",steplist[0].firstChild.data.replace("<br />"," ")expectlist=subb.getElementsByTagName('expectedresults')for expect in expectlist:result=expect.childNodes[0].nodeValue.replace("<br />","" )booksheet.write(row,5,result,style)row=row+1workbook.save('demo.xls')              

写入excel的效果如下:

参考文档:

1、python对excel操作:http://www.open-open.com/lib/view/open1385174954276.html

2、python之xml操作:http://www.jb51.net/article/63780.htm

转载于:https://www.cnblogs.com/landhu/p/4978797.html

几行小代码,将Testlink的xml用例导入至excel相关推荐

  1. 【更新了资源链接】几行小代码保持浏览器活跃 辅助挖共生币

    关于保持浏览器活跃的的一段小代码--(大神勿喷) 前言 这篇文章是关于共生币的,不喜的朋友可以直接绕道 直接上代码 /*======================================== ...

  2. python excel转xml 用例_测试用例Excel转XML格式教程

    运行环境: Python版本:Python2.7.15 第三方库:pywin32 Excel版本:Excel2016 1.安装Python2.7.15 1)下载Python安装包 进入Python官网 ...

  3. Testlink解决大用例导入问题

    最近公司同事需要将别的testlink的用例迁移过来,由于现在新的服务器也在使用,不能使用数据库导入的办法,只能用xml文件进行导入,不过在导入的时候出现了个没遇到的问题,报错文件太大,无法上传. 解 ...

  4. 4行python代码做出打字机效果

    4行python代码做出打字机效果 文章目录 4行python代码做出打字机效果 前言 一.导入库 二.完整代码 前言 今天想用python做一个打字机的效果出来看看! 一.导入库 import py ...

  5. python好学吗 小木虫-25行Python代码完成人脸识别

    以下是Shantnu Tiwari的客座文章,作者有着在底层编程和嵌入式领域十年的工作经验.在发现Python语言之前的几年时间里,Shantnu Tiwari一直忍受着C/C++语言编程的不便,但P ...

  6. python写一个游戏多少代码-使用50行Python代码从零开始实现一个AI平衡小游戏

    集智导读: 本文会为大家展示机器学习专家 Mike Shi 如何用 50 行 Python 代码创建一个 AI,使用增强学习技术,玩耍一个保持杆子平衡的小游戏.所用环境为标准的 OpenAI Gym, ...

  7. python小游戏代码大全-20行python代码的入门级小游戏的详解

    背景: 作为一个python小白,今天从菜鸟教程上看了一些python的教程,看到了python的一些语法,对比起来(有其他语言功底),感觉还是非常有趣,就随手添了一点内容,改了一个小例程,当着练练手 ...

  8. python200行代码小游戏_200 行python 代码实现 2048 游戏

    创建游戏文件 2048.py 首先导入需要的包: import curses from random import randrange, choice from collections import ...

  9. java2048小游戏源码及解析_200行java代码实现2048小游戏

    本文实例为大家分享了java实现2048小游戏的具体代码,供大家参考,具体内容如下 效果图: 游戏介绍: 1.2048是一款益智类小游戏,刚开始随机出现两个数字,可以上下左右控制数字的移动. 2.当选 ...

最新文章

  1. WatchClear 方法 html5,clearWatch()
  2. Qt窗口、控件、文本等的一些常用操作
  3. 从sqlserver中数据写入mysql_[SQL Server]SQL Server数据库中如何返回INSERT INTO语句插入/写入数据后的记录值(比如ID等)?...
  4. 大数据WEB阶段(三)CSS
  5. @font-face详细用法+实例——Web响应式前端学习笔记
  6. 树莓派VNC server设置开机自启动
  7. thymeleaf html模块化,SpringBoot中使用Thymeleaf模板开发的后台管理框架
  8. 一些忘记了的....
  9. Simulink中利用Powergui进行FFT分析,但是信号源始终为空
  10. 网页版微信为什么打不开连接服务器失败,电脑能上微信,但是打不开网页怎么回事?...
  11. gif图片格式制作免费软件
  12. 【JAVAEE基础学习(16)】--简述ElasticSearch
  13. 【CSS】绘制一个任意角度的扇形
  14. ElasticSearch Aggs的一些使用方法
  15. Vue项目使用富文本之tinymce-vue
  16. QGraphicsView通过鼠标操作来绘制图元
  17. 【Robomaster】电控入门手册
  18. UE4_UE5播放视频(附工程)
  19. Python3《机器学习实战》学习笔记(三):决策树实战篇之为自己配个隐形眼镜
  20. 关于SDF deform nicp

热门文章

  1. 去除Win10资源管理器中快速访问的“最近使用的文件”和“常用文件夹”
  2. iOS利用cocoapods 和GitHub组件化序Day1
  3. YII2 save 和 insert 循环保存数据,出现间隔性丢失值 (这些值是共有相同的值) 解决: // 每次保存需要用 clone 不能使用直接用一个 实例化的model....
  4. 实战自制Linux操作系统
  5. ubuntu vnc安装
  6. Tengine(Nginx)配置SSL(https),应用服务器(Tomcat)无需配置
  7. Python自然语言处理学习笔记(30):4.2 序列
  8. 黄聪:演示 Rect、Bounds 生成 TRect 的区别
  9. 【DP + 卖股票】LeetCode 309. Best Time to Buy and Sell Stock with Cooldown
  10. Rust基础概念之数据类型