一、用pyh模块解析stat结点数据(output.py)
import xml. dom. minidom
import xml. etree. ElementTree
dom = xml. dom. minidom. parse( './ui/output.xml' ) ; root2 = xml. etree. ElementTree. parse( './ui/output.xml' )
root = dom. documentElementtotal = root. getElementsByTagName( 'total' ) ;
total_len = len ( total)
tag = root. getElementsByTagName( 'tag' ) ;
tag_len = len ( tag)
suite = root. getElementsByTagName( 'suite' ) ;
suite_len = len ( suite)
total2 = root2. getiterator( "total" )
total_stat_num = len ( total2[ total_len- 1 ] . getchildren( ) )
tag2 = root2. getiterator( "tag" )
tag_stat_num = len ( tag2[ tag_len- 1 ] . getchildren( ) )
suite2 = root2. getiterator( "suite" )
suite_stat_num = len ( suite2[ suite_len- 1 ] . getchildren( ) ) statlist = root. getElementsByTagName( 'stat' ) ; def get_total_statistics ( ) : list = [ ] for i in range ( 0 , total_stat_num) : d = dict ( ) d[ 'fail' ] = int ( statlist[ i] . getAttribute( "fail" ) ) d[ 'pass' ] = int ( statlist[ i] . getAttribute( "pass" ) ) d[ 'total' ] = d[ 'fail' ] + d[ 'pass' ] d[ 'text' ] = statlist[ i] . firstChild. datalist . append( d) return list
def get_statistics_by_tag ( ) : list = [ ] for i in range ( total_stat_num, total_stat_num+ tag_stat_num) : d = dict ( ) d[ 'fail' ] = int ( statlist[ i] . getAttribute( "fail" ) ) d[ 'pass' ] = int ( statlist[ i] . getAttribute( "pass" ) ) d[ 'total' ] = d[ 'fail' ] + d[ 'pass' ] d[ 'text' ] = statlist[ i] . firstChild. datalist . append( d) return list def get_statistics_by_suite ( ) : list = [ ] for i in range ( total_stat_num+ tag_stat_num, len ( statlist) ) : d = dict ( ) d[ 'fail' ] = int ( statlist[ i] . getAttribute( "fail" ) ) d[ 'pass' ] = int ( statlist[ i] . getAttribute( "pass" ) ) d[ 'total' ] = d[ 'fail' ] + d[ 'pass' ] d[ 'text' ] = statlist[ i] . firstChild. datalist . append( d) return list
二、pyh生成带表格的HTML(report.py)
在这里插入代码片
from pyh import *
from fontTools. ttLib import TTFont
from output import * page = PyH( 'My wonderful PyH page' ) page << h2( 'Total statistics' ) value = get_total_statistics( )
print ( value) mytab = page << table( border= '1' )
mytr = mytab << tr( )
mytr << th( "Total Statistics" , width= "200" , bgcolor= "#CCFFCC" ) + th( "total" , width= "100" , bgcolor= "#CCFFCC" ) + th( "pass" , width= "100" , bgcolor= "#CCFFCC" ) + th( "fail" , width= "100" , bgcolor= "#CCFFCC" )
for i in range ( len ( value) ) : mytr = mytab << tr( ) mytr << td( value[ i] [ 'text' ] ) + td( value[ i] [ 'total' ] ) + td( value[ i] [ 'pass' ] ) + td( value[ i] [ 'fail' ] ) value = get_statistics_by_tag( )
page << br( )
mytab2 = page << table( border= '1' )
mytr = mytab2 << tr( )
mytr << th( "Statistics by Tag" , width= "200" , bgcolor= "#CCFFCC" ) + th( "total" , width= "100" , bgcolor= "#CCFFCC" ) + th( "pass" , width= "100" , bgcolor= "#CCFFCC" ) + th( "fail" , width= "100" , bgcolor= "#CCFFCC" )
for i in range ( 1 , len ( value) ) : mytr = mytab2 << tr( ) mytr << td( value[ i] [ 'text' ] ) + td( value[ i] [ 'total' ] ) + td( value[ i] [ 'pass' ] ) + td( value[ i] [ 'fail' ] ) value = get_statistics_by_suite( )
page << br( )
mytab3 = page << table( border= '1' )
mytr = mytab3 << tr( )
mytr << th( "Statistics by Suite" , width= "200" , bgcolor= "#CCFFCC" ) + th( "total" , width= "100" , bgcolor= "#CCFFCC" ) + th( "pass" , width= "100" , bgcolor= "#CCFFCC" ) + th( "fail" , width= "100" , bgcolor= "#CCFFCC" )
for i in range ( 1 , len ( value) ) : mytr = mytab3 << tr( ) mytr << td( value[ i] [ 'text' ] ) + td( value[ i] [ 'total' ] ) + td( value[ i] [ 'pass' ] ) + td( value[ i] [ 'fail' ] ) page. printOut( 'table.html' )