经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Python3 » 查看文章
【AltWalker】模型驱动:轻松实现自动化测试用例的生成和组织执行
来源:cnblogs  作者:Bingoh  时间:2023/7/28 8:49:39  对本文有异议

模型驱动的自动化测试

模型驱动的自动化测试(Model-Based Testing, 后文中我们将简称为MBT)是一种软件测试方法,它将系统的行为表示为一个或多个模型,然后从模型中自动生成和执行测试用例。这种方法的核心思想是将测试过程中的重点从手动编写测试用例转移到创建和维护描述系统行为的模型。

优势

基于MBT的测试相比较于普通的测试有以下几大优点:

优点 描述
覆盖率 模型是对系统行为的抽象表示,可以帮助测试人员更好地理解和分析系统的功能和性能。从而提高测试覆盖率。
测试效率 通过从模型中自动生成测试用例,减少手动编写测试用例的工作量,提高测试效率。
可维护性 模型驱动的测试用例易于维护,因为当系统发生变化时,只需更新模型,而无需手动修改大量测试用例。
可重用性 模型可以在多个测试项目中重用,减少重复工作并提高测试质量。

操作步骤

基于MBT的测试主要有以下几个步骤:

  • 创建模型:
    首先,需要构建一个描述系统行为的模型。这个模型通常采用图形表示,如状态机、Petri网或者流程图等。模型中的顶点表示系统的状态,边表示状态之间的转换。

  • 生成测试用例:
    通过分析模型,可以自动生成测试用例。测试用例生成算法可以根据不同的目标(如覆盖率、路径长度等)来选择。常用的算法有随机测试、覆盖所有路径、覆盖所有边等。

  • 执行测试:
    使用自动生成的测试用例对实际系统进行测试。这一步通常需要一个测试执行引擎,它可以将模型中的操作映射到实际系统中的操作。例如,使用Selenium WebDriver进行Web应用测试。

  • 验证结果:
    比较实际系统的行为与模型的预期行为,以确定系统是否满足需求。如果发现问题,可以更新模型并重新生成测试用例。

下面我们一起来看看在实际业务中使用AltWalker库进行进行基于模型的自动化测试的应用。

什么是AltWalker?

AltWalker是一个基于图模型的自动化测试框架,用于编写、执行和管理基于模型的测试。它主要用于测试复杂系统,如Web应用程序、移动应用程序等。它支持运行用 .NET/C#Python3 编写测试模型用例。

安装AltWalker

在命令行中输入以下命令来安装AltWalker:

  1. pip install altwalker
检查是否安装了正确的版本
  1. $ altwalker --version
  2. AltWalker, version 0.3.1

牛刀小试

创建一个测试项目

创建一个新的文件夹,用于存放我们的测试项目。在该文件夹中,执行以下命令创建MBT

  1. altwalker init -l python test-project

执行这个命令后,将在当前目录生成一个包含模型模板代码的目录test-project,目录结构如下:

  1. # tree
  2. test-project/
  3. ├── models/
  4. ├── default.json
  5. └── tests/
  6. ├── __init__.py
  7. └── test.py
运行测试

只需要执行以下命令,就能够运行default.json中定义的模型,覆盖模型的链路,链路执行时会运行对应在test.py中的代码

  1. $ cd test-project
  2. $ altwalker online tests -m models/default.json "random(edge_coverage(100))"
运行效果

image

自己编写图模型

上面只是执行了一个单一的链路Demo,下面我们一起写一个我们自己的模型。

模型编辑器我们可以选择在线的,也可以选择使用 vscode的插件,当然也可以选择自己搭建一个。

在线模型编辑器

我们可以使用在线的模型编辑器来编写模型。

VScode扩展

可以在扩展上搜索AltWalker Model Visualizer
,也可以自己访问链接下载扩展来编辑查看模型。

本地部署

可以参考:https://github.com/altwalker/model-editor

包含登录、选择产品、支付、退出登录的模型编写

点击查看对应模型
  1. {
  2. "name": "Pay Models",
  3. "models": [
  4. {
  5. "name": "PayModel",
  6. "generator": "random(edge_coverage(100) && vertex_coverage(100))",
  7. "startElementId": "v_init",
  8. "vertices": [
  9. {
  10. "id": "v_init",
  11. "name": "start_vertex"
  12. },
  13. {
  14. "id": "v_login",
  15. "name": "state_login"
  16. },
  17. {
  18. "id": "v_select_product",
  19. "name": "state_select_product"
  20. },
  21. {
  22. "id": "v_pay",
  23. "name": "state_pay"
  24. },
  25. {
  26. "id": "v_logout",
  27. "name": "state_logout"
  28. }
  29. ],
  30. "edges": [
  31. {
  32. "id": "e_init",
  33. "name": "action_init",
  34. "sourceVertexId": "v_init",
  35. "targetVertexId": "v_login"
  36. },
  37. {
  38. "id": "e_login",
  39. "name": "action_login",
  40. "sourceVertexId": "v_login",
  41. "targetVertexId": "v_select_product"
  42. },
  43. {
  44. "id": "e_select_product",
  45. "name": "action_select_product",
  46. "sourceVertexId": "v_select_product",
  47. "targetVertexId": "v_pay"
  48. },
  49. {
  50. "id": "e_pay",
  51. "name": "action_pay",
  52. "sourceVertexId": "v_pay",
  53. "targetVertexId": "v_logout"
  54. },
  55. {
  56. "id": "e_logout",
  57. "name": "action_logout",
  58. "sourceVertexId": "v_logout",
  59. "targetVertexId": "v_init"
  60. }
  61. ]
  62. }
  63. ]
  64. }
模型效果

image

1、校验模型

保存这个模型到新创建的项目default.json,然后执行下面的命令检查模型是否存在问题:

  1. altwalker check -m models/default.json "random(never)"
2、验证测试代码是否和模型是否匹配
  1. altwalker check -m models/default.json "random(never)"

因为我们还没有编写测试代码,所以会出现下面的报错:

image

可以看到在上面的报错中给出了建议的代码,我们把它复制到 test.py中。

  1. class PayModel:
  2. def state_pay(self):
  3. pass
  4. def action_init(self):
  5. pass
  6. def state_select_product(self):
  7. pass
  8. def state_logout(self):
  9. pass
  10. ...
3、运行测试

在命令行中执行下面的命令来执行测试:

  1. altwalker online tests -m models/default.json "random(edge_coverage(100))" --report-xml-file report.xml

这将会运行在default.jsontest.py文件中定义的所有测试用例。

4、查看测试报告

在测试执行完成后,AltWalker会生成一个名为report.xml的JUnit格式的测试报告。我们可以使用任何支持JUnit格式的测试报告工具来查看这个报告。

还有其他方式的测试报告提供,可以查看官方文档。

一个比较复杂的例子

如果我们有一个商城需要验证,有登录、用户个人中心,商品首页,商品详情页、支付界面、订单界面等,我们要针对这样的网站做一个自动化,可能会有以下这些场景:

场景
登录界面 -> Do:输入密码登录 -> 用户首页 -> Do:查看商品A -> A商品详情页 -> Do:点击第二个商品按钮 -> 商品C详情页面 -> Do:点击付款 -> 付款界面 -> Do:取消付款 -> 订单倒计时界面 -> Do:关闭浏览器 -> 浏览器关闭
登录界面 -> Do:输入密码登录 -> 用户首页 -> Do:查看商品A -> A商品详情页 -> Do:点击第二个商品按钮 -> 商品C详情页面 -> Do:点击付款 -> 付款界面 -> Do:查看订单 -> 历史订单界面 -> Do:关闭浏览器 -> 浏览器关闭
登录界面 -> Do:输入密码登录 -> 用户首页 -> Do:查看商品A -> A商品详情页 -> Do:点击第二个商品按钮 -> 商品C详情页面 -> Do:点击付款 -> 付款界面 -> Do:查看订单 -> 历史订单界面 -> Do:查看订单详情 -> 订单详情界面 -> Do:关闭浏览器 -> 浏览器关闭
登录界面 -> Do:输入密码登录 -> 用户首页 -> Do:查看商品A -> A商品详情页 -> Do:点击第二个商品按钮 -> 商品C详情页面 -> Do:点击用户中心 -> 用户中心界面 -> Do:查看订单 -> 历史订单界面 -> Do:关闭浏览器 -> 浏览器关闭
登录界面 -> Do:输入密码登录 -> 用户首页 -> Do:查看商品A -> A商品详情页 -> Do:点击第二个商品按钮 -> 商品C详情页面 -> Do:点击用户中心 -> 用户中心界面 -> Do:查看订单 -> 历史订单界面 -> Do:查看订单详情 -> 订单详情界面 -> Do:关闭浏览器 -> 浏览器关闭
登录界面 -> Do:输入密码登录 -> 用户首页 -> Do:查看商品A -> A商品详情页 -> Do:点击相似商品B -> 商品B详情页 -> Do:点击购买 -> 付款界面 -> Do:取消付款 -> 订单倒计时界面 -> Do:关闭浏览器 -> 浏览器关闭
登录界面 -> Do:输入密码登录 -> 用户首页 -> Do:查看商品A -> A商品详情页 -> Do:点击相似商品B -> 商品B详情页 -> Do:点击购买 -> 付款界面 -> Do:查看订单 -> 历史订单界面 -> Do:关闭浏览器 -> 浏览器关闭
登录界面 -> Do:输入密码登录 -> 用户首页 -> Do:查看商品A -> A商品详情页 -> Do:点击相似商品B -> 商品B详情页 -> Do:点击购买 -> 付款界面 -> Do:查看订单 -> 历史订单界面 -> Do:查看订单详情 -> 订单详情界面 -> Do:关闭浏览器 -> 浏览器关闭
...
使用 MBT 图示展示(箭头表示操作)

image

大家应该都能发现,使用altwalker画出来的图,能非常直观的展示各个页面之间可以进行的操作,并且很好扩展,这非常符合我们做页面自动化的时候所说的POM(Page Object Model)。

这也是altwalker最重要的价值所在:适合的用于测试复杂系统,如Web应用程序、移动应用程序等。

点击查看对应模型
  1. {
  2. "name": "Default Models",
  3. "models": [
  4. {
  5. "name": "DefaultModel",
  6. "generator": "random(never)",
  7. "startElementId": "v0",
  8. "vertices": [
  9. {
  10. "id": "v0",
  11. "name": "登录界面"
  12. },
  13. {
  14. "id": "v1",
  15. "name": "用户首页"
  16. },
  17. {
  18. "id": "v2",
  19. "name": "浏览器关闭"
  20. },
  21. {
  22. "id": "v3",
  23. "name": "A商品详情页"
  24. },
  25. {
  26. "id": "v6",
  27. "name": "商品C详情页面"
  28. },
  29. {
  30. "id": "v7",
  31. "name": "付款界面"
  32. },
  33. {
  34. "id": "v8",
  35. "name": "付款成功界面"
  36. },
  37. {
  38. "id": "v9",
  39. "name": "订单倒计时界面"
  40. },
  41. {
  42. "id": "v10",
  43. "name": "用户中心界面"
  44. },
  45. {
  46. "id": "v11",
  47. "name": "商品B详情页"
  48. },
  49. {
  50. "id": "v4",
  51. "name": "历史订单界面"
  52. },
  53. {
  54. "id": "v5",
  55. "name": "订单详情界面"
  56. }
  57. ],
  58. "edges": [
  59. {
  60. "id": "e0",
  61. "name": "输入密码登录",
  62. "sourceVertexId": "v0",
  63. "targetVertexId": "v1"
  64. },
  65. {
  66. "id": "e1",
  67. "name": "关闭浏览器",
  68. "sourceVertexId": "v0",
  69. "targetVertexId": "v2"
  70. },
  71. {
  72. "id": "e2",
  73. "name": "关闭浏览器",
  74. "sourceVertexId": "v1",
  75. "targetVertexId": "v2"
  76. },
  77. {
  78. "id": "e3",
  79. "name": "查看商品A",
  80. "sourceVertexId": "v1",
  81. "targetVertexId": "v3"
  82. },
  83. {
  84. "id": "e5",
  85. "name": "点击第二个商品按钮",
  86. "sourceVertexId": "v3",
  87. "targetVertexId": "v6"
  88. },
  89. {
  90. "id": "e7",
  91. "name": "点击付款",
  92. "sourceVertexId": "v6",
  93. "targetVertexId": "v7"
  94. },
  95. {
  96. "id": "e8",
  97. "name": "退出登录",
  98. "sourceVertexId": "v8",
  99. "targetVertexId": "v0"
  100. },
  101. {
  102. "id": "e9",
  103. "name": "e9",
  104. "sourceVertexId": "v3",
  105. "targetVertexId": "v8"
  106. },
  107. {
  108. "id": "e10",
  109. "name": "付款成功",
  110. "sourceVertexId": "v7",
  111. "targetVertexId": "v8"
  112. },
  113. {
  114. "id": "e11",
  115. "name": "取消付款",
  116. "sourceVertexId": "v7",
  117. "targetVertexId": "v9"
  118. },
  119. {
  120. "id": "e12",
  121. "name": "点击用户中心",
  122. "sourceVertexId": "v6",
  123. "targetVertexId": "v10"
  124. },
  125. {
  126. "id": "e13",
  127. "name": "点击购买",
  128. "sourceVertexId": "v11",
  129. "targetVertexId": "v7"
  130. },
  131. {
  132. "id": "e14",
  133. "name": "点击相似商品B",
  134. "sourceVertexId": "v3",
  135. "targetVertexId": "v11"
  136. },
  137. {
  138. "id": "e17",
  139. "name": "关闭浏览器",
  140. "sourceVertexId": "v9",
  141. "targetVertexId": "v2"
  142. },
  143. {
  144. "id": "e18",
  145. "sourceVertexId": "v10",
  146. "targetVertexId": "v4",
  147. "name": "查看订单"
  148. },
  149. {
  150. "id": "e19",
  151. "name": "点击首页按钮",
  152. "sourceVertexId": "v3",
  153. "targetVertexId": "v1"
  154. },
  155. {
  156. "id": "e20",
  157. "name": "点击首页",
  158. "sourceVertexId": "v6",
  159. "targetVertexId": "v1"
  160. },
  161. {
  162. "id": "e21",
  163. "name": "点击用户中心",
  164. "sourceVertexId": "v10",
  165. "targetVertexId": "v1"
  166. },
  167. {
  168. "id": "e4",
  169. "name": "刷新浏览器",
  170. "sourceVertexId": "v6",
  171. "targetVertexId": "v6"
  172. },
  173. {
  174. "id": "e6",
  175. "name": "查看订单",
  176. "sourceVertexId": "v7",
  177. "targetVertexId": "v4"
  178. },
  179. {
  180. "id": "e15",
  181. "name": "关闭浏览器",
  182. "sourceVertexId": "v4",
  183. "targetVertexId": "v2"
  184. },
  185. {
  186. "id": "e16",
  187. "name": "查看订单详情",
  188. "sourceVertexId": "v4",
  189. "targetVertexId": "v5"
  190. },
  191. {
  192. "id": "e22",
  193. "name": "关闭浏览器",
  194. "sourceVertexId": "v5",
  195. "targetVertexId": "v2"
  196. }
  197. ],
  198. "actions": [
  199. "fasf;fdsaf;"
  200. ]
  201. }
  202. ]
  203. }

完整的结合 POM使用的方法可以查看官方给出的示例:https://github.com/altwalker/altwalker-examples

结合已有框架使用

如果我们仅仅只想使用模型中的链路组织能力,也可以自己根据编写的模型使用下面这段代码来生成对应的链路,然后组织用例场景,可以用于自动化用例生成。

  1. graph_data = {
  2. "name": "Default Models",
  3. "models": [
  4. {
  5. "name": "DefaultModel",
  6. "generator": "random(never)",
  7. "startElementId": "v0",
  8. "vertices": [
  9. {
  10. "id": "v0",
  11. "name": "登录界面"
  12. ...
  13. ]
  14. }
  15. model = graph_data['models'][0]
  16. edges = model['edges']
  17. vertices = model['vertices']
  18. start_vertex_id = model['startElementId']
  19. # 构建图
  20. graph = {}
  21. for edge in edges:
  22. source = edge['sourceVertexId']
  23. target = edge['targetVertexId']
  24. if source not in graph:
  25. graph[source] = []
  26. graph[source].append((target, edge['name']))
  27. # 顶点ID到顶点名称的映射
  28. vertex_name_map = {vertex['id']: vertex['name'] for vertex in vertices}
  29. # 深度优先搜索
  30. def dfs(_graph, start_vertex, _path, _visited):
  31. _visited.add(start_vertex)
  32. _path.append(vertex_name_map[start_vertex])
  33. if start_vertex not in _graph:
  34. print(" -> ".join(_path))
  35. else:
  36. for neighbor, action in _graph[start_vertex]:
  37. if neighbor not in _visited:
  38. _path.append(f"Do:{action}")
  39. dfs(_graph, neighbor, _path, _visited)
  40. _path.pop()
  41. _path.pop()
  42. _visited.remove(start_vertex)
  43. # 打印所有可能的链路及其经过的操作,操作前面加上"Do"标记
  44. visited = set()
  45. path = []
  46. dfs(graph, start_vertex_id, path, visited)

总结

通过以上步骤,我们了解了如何使用AltWalker进行模型驱动的自动化测试。AltWalker是一个强大的测试框架,可以帮助我们更高效地编写、执行和管理测试用例。

当然,基于模型的测试也有一些局限性,如模型的准确性和完整性对测试结果影响较大,模型构建和维护可能需要额外的成本等。因此,在实际应用中,需要根据项目的具体需求来决定是否采用基于模型的测试方法。希望本文对你有所帮助!

原文链接:https://www.cnblogs.com/Detector/p/17583695.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号