自动化测试平台(十二):测试报告的实现

x33g5p2x  于2022-04-07 转载在 其他  
字(4.7k)|赞(0)|评价(0)|浏览(639)

一、前言

上一张我们完成了UI自动化的执行功能,这一章我们将讲解如何定制化实现测试报告。

完整教程地址:《从0搭建自动化测试平台》

项目在线演示地址:http://121.43.43.59/ (帐号:admin 密码:123456)

本章内容实现效果如下:

二、使用antd-charts实现图表报告

测试报告需要使用图表将测试结果、数据可视化,之前大多都是用echarts来实现的,但自己框架使用的是antd ,而且他们的官方库antd-charts一样能实现一些可视化图表,所以我们完全可以使用antd-charts来实现实现我们的测试报告展示。

antd-charts官网:https://charts.ant.design/zh/examples/gallery

打开官网会发现它支持很多图表,包括一些常见的饼图,折线图,柱状图等.
不常见的漏斗图、玉珏图、分面图等也支持。而且也非常美观:

它也有非常详尽的文档帮助我们学习使用:

我们只需要根据自己的想法或实际业务需求选择对应的图表做数据渲染就OK了。也是非常的方便的!

三、测试报告源码分享

这里将实现效果的测试报告源码分享给大家参考,使用的组件:antd的栅格布局、antd-charts图表:

  1. import { useState, useEffect } from 'react';
  2. import { Row, Col, Card } from 'antd';
  3. import { Gauge, Column, Liquid, Area } from '@ant-design/plots';
  4. import style from './index.css';
  5. export default function App() {
  6. const [data, setData] = useState([]);
  7. useEffect(() => {
  8. asyncFetch();
  9. }, []);
  10. const asyncFetch = () => {
  11. fetch('https://gw.alipayobjects.com/os/bmw-prod/360c3eae-0c73-46f0-a982-4746a6095010.json')
  12. .then((response) => response.json())
  13. .then((json) => setData(json))
  14. .catch((error) => {
  15. console.log('fetch data failed', error);
  16. });
  17. };
  18. const config = {
  19. data,
  20. xField: 'timePeriod',
  21. yField: 'value',
  22. xAxis: {
  23. range: [0, 1],
  24. },
  25. };
  26. const colData: any = [
  27. {
  28. "year": "登录",
  29. "value": 1,
  30. "type": "失败"
  31. },
  32. {
  33. "year": "登录",
  34. "value": 3,
  35. "type": "通过"
  36. },
  37. {
  38. "year": "创建用户",
  39. "value": 20,
  40. "type": "通过"
  41. },
  42. {
  43. "year": "创建用户",
  44. "value": 3,
  45. "type": "失败"
  46. }, {
  47. "year": "创建订单",
  48. "value": 12,
  49. "type": "通过"
  50. },
  51. {
  52. "year": "创建订单",
  53. "value": 1,
  54. "type": "失败"
  55. }, {
  56. "year": "注册用户",
  57. "value": 16,
  58. "type": "通过"
  59. },
  60. {
  61. "year": "注册用户",
  62. "value": 6,
  63. "type": "失败"
  64. }, {
  65. "year": "修改用户信息",
  66. "value": 18,
  67. "type": "通过"
  68. },
  69. {
  70. "year": "修改用户信息",
  71. "value": 9,
  72. "type": "失败"
  73. }]
  74. const liqConfig = {
  75. percent: 0.80,
  76. color: '#30BF78',
  77. outline: {
  78. border: 4,
  79. distance: 8,
  80. },
  81. wave: {
  82. length: 128,
  83. },
  84. }
  85. const colConfig: any = {
  86. data: colData,
  87. isStack: true,
  88. color: ['#F4664A', '#30BF78'],
  89. xField: 'year',
  90. yField: 'value',
  91. seriesField: 'type',
  92. label: {
  93. // 可手动配置 label 数据标签位置
  94. position: 'middle',
  95. // 'top', 'bottom', 'middle'
  96. // 可配置附加的布局方法
  97. layout: [
  98. // 柱形图数据标签位置自动调整
  99. {
  100. type: 'interval-adjust-position',
  101. }, // 数据标签防遮挡
  102. {
  103. type: 'interval-hide-overlap',
  104. }, // 数据标签文颜色自动调整
  105. ],
  106. },
  107. };
  108. const gaudeConfig = {
  109. percent: 0.75,
  110. range: {
  111. ticks: [0, 1 / 3, 2 / 3, 1],
  112. color: ['#F4664A', '#FAAD14', '#30BF78'],
  113. },
  114. indicator: {
  115. pointer: {
  116. style: {
  117. stroke: '#D0D0D0',
  118. },
  119. },
  120. pin: {
  121. style: {
  122. stroke: '#D0D0D0',
  123. },
  124. },
  125. },
  126. statistic: {
  127. content: {
  128. style: {
  129. fontSize: '36px',
  130. lineHeight: '36px',
  131. },
  132. },
  133. },
  134. };
  135. return (
  136. <div style={{ marginLeft: 120, marginTop: 20, marginRight: 120 }}>
  137. <h1 style={{ textAlign: 'center', fontWeight: 'bold' }}>
  138. 用户中心-UI测试报告
  139. </h1>
  140. <p>执行时间:202245 15:09:18</p>
  141. <Row
  142. style={{ backgroundColor: '#F1F1F1', paddingTop: 15, width: '100%' }}
  143. gutter={[20, 20]}
  144. >
  145. <Col className="wrapper" key={1} span={6}>
  146. <Card style={{ backgroundColor: '#6395f9' }}>
  147. <h2
  148. style={{ fontWeight: 600, color: 'white', textAlign: 'center' }}
  149. >执行环境:测试</h2>
  150. </Card>
  151. </Col>
  152. <Col className="wrapper" key={2} span={6}>
  153. <Card style={{ backgroundColor: '#62daab' }}>
  154. <h2
  155. style={{ fontWeight: 600, color: 'white', textAlign: 'center' }}
  156. >{`涉及用例数:10 个`}</h2>
  157. </Card>
  158. </Col>
  159. <Col className="wrapper" key={3} span={6}>
  160. <Card style={{ backgroundColor: '#657798' }}>
  161. <h2
  162. style={{ fontWeight: 600, color: 'white', textAlign: 'center' }}
  163. >{`总步骤数:120 个`}</h2>
  164. </Card>
  165. </Col>
  166. <Col className="wrapper" key={4} span={6}>
  167. <Card style={{ backgroundColor: '#f6c002' }}>
  168. <h2
  169. style={{ fontWeight: 600, color: 'white', textAlign: 'center' }}
  170. >{`总耗时:128.6 S`}</h2>
  171. </Card>
  172. </Col>
  173. <Col className="wrapper" key={7} span={8}>
  174. <Card title={<h3 className={style.cardTitle}>用例覆盖率</h3>}>
  175. <Liquid {...liqConfig} />
  176. </Card>
  177. </Col>
  178. <Col className="wrapper" key={5} span={8}>
  179. <Card title={<h3 className={style.cardTitle}>用例成功率</h3>}>
  180. <Gauge {...gaudeConfig} />
  181. </Card>
  182. </Col>
  183. <Col className="wrapper" key={6} span={8}>
  184. <Card
  185. title={<h3 className={style.cardTitle}>各用例情况统计</h3>}
  186. >
  187. <Column {...colConfig} />
  188. </Card>
  189. </Col>
  190. <Col className="wrapper" key={8} span={24}>
  191. <Card
  192. title={<h3 className={style.cardTitle}>历史通过率</h3>} >
  193. <Area {...config} />
  194. </Card>
  195. </Col>
  196. </Row>
  197. </div>
  198. );
  199. }

四、总结

测试报告是指把测试的过程和结果写成文档,对发现的问题和缺陷进行分析,为纠正软件的存在的质量问题提供依据,同时为软件验收和交付打下基础。这是尤为重要的。很多时候大家是通过allure、httprunner等模板来生成测试报告,但对于定制化强的项目、有高度定制化需求时,还是需要我们自定义测试报告,小伙伴们可以根据自己的想法去组合使用各种图表来实现更详尽美观的报告。

相关文章