You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

py_Html.py 6.5 KiB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. # encoding: utf-8
  2. """
  3. @author: lileilei
  4. @file: py_Html.py
  5. @time: 2017/6/5 17:04
  6. """
  7. titles = '接口测试'
  8. def title(titles):
  9. title = '''<!DOCTYPE html>
  10. <html>
  11. <head>
  12. <title>%s</title>
  13. <meta charset="UTF-8">
  14. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  15. <!-- 引入 Bootstrap -->
  16. <link href="https://cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
  17. <!-- HTML5 Shim 和 Respond.js 用于让 IE8 支持 HTML5元素和媒体查询 -->
  18. <!-- 注意: 如果通过 file:// 引入 Respond.js 文件,则该文件无法起效果 -->
  19. <!--[if lt IE 9]>
  20. <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
  21. <script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
  22. <![endif]-->
  23. <style type="text/css">
  24. .hidden-detail,.hidden-tr{
  25. display:none;
  26. }
  27. </style>
  28. </head>
  29. <body>
  30. ''' % (titles)
  31. return title
  32. connent = '''
  33. <div class='col-md-4 col-md-offset-4' style='margin-left:3%;'>
  34. <h1>接口测试的结果</h1>'''
  35. def shouye(starttime, endtime, passge, fail, excepthions, weizhicuowu):
  36. beijing = '''
  37. <table class="table table-hover table-condensed">
  38. <tbody>
  39. <tr>
  40. <td><strong>开始时间:</strong> %s</td>
  41. </tr>
  42. <td><strong>结束时间:</strong> %s</td></tr>
  43. <td><strong>耗时:</strong> %s</td></tr>
  44. <td><strong>结果:</strong>
  45. <span >Pass: <strong >%s</strong>
  46. Fail: <strong >%s</strong>
  47. exception: <strong >%s</strong>
  48. weizhicuowu : <strong >%s</strong></span></td>
  49. </tr>
  50. </tbody></table>
  51. </div> ''' % (starttime, endtime, (endtime - starttime), passge, fail, excepthions, weizhicuowu)
  52. return beijing
  53. shanghai = '''<div class="row " style="margin:60px">
  54. <div style=' margin-top: 18%;' >
  55. <div class="btn-group" role="group" aria-label="...">
  56. <button type="button" id="check-all" class="btn btn-primary">所有用例</button>
  57. <button type="button" id="check-success" class="btn btn-success">成功用例</button>
  58. <button type="button" id="check-danger" class="btn btn-danger">失败用例</button>
  59. <button type="button" id="check-warning" class="btn btn-warning">错误用例</button>
  60. <button type="button" id="check-except" class="btn btn-defult">异常用例</button>
  61. </div>
  62. <div class="btn-group" role="group" aria-label="...">
  63. </div>
  64. <table class="table table-hover table-condensed table-bordered" style="word-wrap:break-word; word-break:break-all; margin-top: 7px;">
  65. <tr >
  66. <td ><strong>用例ID&nbsp;</strong></td>
  67. <td><strong>用例名字</strong></td>
  68. <td><strong>key</strong></td>
  69. <td><strong>请求内容</strong></td>
  70. <td><strong>url</strong></td>
  71. <td><strong>请求方式</strong></td>
  72. <td><strong>预期</strong></td>
  73. <td><strong>实际返回</strong></td>
  74. <td><strong>结果</strong></td>
  75. </tr>
  76. '''
  77. def passfail(tend):
  78. if tend == 'pass':
  79. htl = '''<td bgcolor="green">pass</td>'''
  80. elif tend == 'fail':
  81. htl = '''<td bgcolor="fail">fail</td>'''
  82. elif tend == 'weizhi':
  83. htl = '''<td bgcolor="red">error</td>'''
  84. else:
  85. htl = '<td bgcolor="crimson">exect</td>'
  86. return htl
  87. def ceshixiangqing(reslt, id, name, key, coneent, url, meth, yuqi, json, relust):
  88. xiangqing = '''
  89. <tr class="case-tr %s">
  90. <td>%s</td>
  91. <td>%s</td>
  92. <td>%s</td>
  93. <td>%s</td>
  94. <td>%s</td>
  95. <td>%s</td>
  96. <td>%s</td>
  97. <td>%s</td>
  98. %s
  99. </tr>
  100. ''' % (reslt, id, name, key, coneent, url, meth, yuqi, json, passfail(relust))
  101. return xiangqing
  102. weibu = '''</div></div></table><script src="https://code.jquery.com/jquery.js"></script>
  103. <script src="https://cdn.bootcss.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
  104. <script type="text/javascript">
  105. $("#check-danger").click(function(e){
  106. $(".case-tr").removeClass("hidden-tr");
  107. $(".success").addClass("hidden-tr");
  108. $(".warning").addClass("hidden-tr");
  109. $(".error").addClass("hidden-tr");
  110. });
  111. $("#check-warning").click(function(e){
  112. $(".case-tr").removeClass("hidden-tr");
  113. $(".success").addClass("hidden-tr");
  114. $(".danger").addClass("hidden-tr");
  115. $(".error").addClass("hidden-tr");
  116. });
  117. $("#check-success").click(function(e){
  118. $(".case-tr").removeClass("hidden-tr");
  119. $(".warning").addClass("hidden-tr");
  120. $(".danger").addClass("hidden-tr");
  121. $(".error").addClass("hidden-tr");
  122. });
  123. $("#check-except").click(function(e){
  124. $(".case-tr").removeClass("hidden-tr");
  125. $(".warning").addClass("hidden-tr");
  126. $(".danger").addClass("hidden-tr");
  127. $(".success").addClass("hidden-tr");
  128. });
  129. $("#check-all").click(function(e){
  130. $(".case-tr").removeClass("hidden-tr");
  131. });
  132. </script>
  133. </body></html>'''
  134. def relust(titles, starttime, endtime, passge, fail, id, name, key, coneent, url, meth, yuqi, json, relust, exceptions,
  135. weizhi):
  136. if type(name) == list:
  137. relus = ' '
  138. for i in range(len(name)):
  139. if relust[i] == "pass":
  140. clazz = "success"
  141. elif relust[i] == "fail":
  142. clazz = "warning"
  143. elif relust[i] == "weizhi":
  144. clazz = "danger"
  145. else:
  146. clazz = 'error'
  147. relus += (
  148. ceshixiangqing(clazz, id[i], name[i], key[i], coneent[i], url[i], meth[i], yuqi[i], json[i], relust[i]))
  149. text = title(titles) + connent + shouye(starttime, endtime, passge, fail, exceptions,
  150. weizhi) + shanghai + relus + weibu
  151. else:
  152. text = title(titles) + connent + shouye(starttime, endtime, passge, fail, exceptions,
  153. weizhi) + shanghai + ceshixiangqing(id, name, key, coneent, url, meth,
  154. yuqi, json, relust) + weibu
  155. return text
  156. def createHtml(filepath, titles, starttime, endtime, passge, fail, id, name, key, coneent, url, meth, yuqi, json,
  157. relusts, exceptions, weizhi):
  158. texts = relust(titles, starttime, endtime, passge, fail, id, name, key, coneent, url, meth, yuqi, json, relusts,
  159. exceptions, weizhi)
  160. with open(filepath, 'wb') as f:
  161. f.write(texts.encode('utf-8'))

Introduction

生成接口测试报告

No topics