黑盒测试方法论-基于模型的测试

基于模型的测试方法 Model-based Testing

Model-based Testing 介绍

基于模型的测试是基于模型的设计的应用,用于设计和可选地执行工件以执行软件测试或系统测试。模型可用于表示被测系统 (SUT) 的期望行为,或表示测试策略和测试环境。

Model-based Testing 中的 model

描述 SUT 的模型通常是对 SUT 所需行为的抽象、部分表示。从这种模型派生的测试用例是与模型在同一抽象级别上的功能测试。这些测试用例统称为抽象测试套件。抽象测试套件不能直接针对 SUT 执行,因为该套件处于错误的抽象级别。可执行的测试套件需要从相应的抽象测试套件派生而来。可执行测试套件可以直接与被测系统通信。这是通过将抽象测试用例映射到适合执行的具体测试用例来实现的。在一些基于模型的测试环境中,模型包含足够的信息来直接生成可执行的测试套件。在其他情况下,抽象测试套件中的元素必须映射到软件中的特定语句或方法调用,以创建具体的测试套件。这被称为解决“映射问题”。

模型构建方法

Model-based Testing 中的 testing

测试可以通过不同的方式从模型中导出。因为测试通常是实验性的并且基于启发式,所以没有已知的单一测试推导的最佳方法。通常将所有与测试派生相关的参数合并到一个包中,该包通常被称为“测试需求”、“测试目的”甚至“用例”。这个包可以包含关于模型中应该关注的那些部分的信息,或者完成测试的条件(测试停止标准)。

Model-based Testing 补充说明

因为测试套件源自模型而不是源代码,基于模型的测试通常被视为一种形式的黑盒测试。 复杂软件系统的基于模型的测试仍然是一个不断发展的领域。

基于模型的测试方法(维基百科与百度百科)

  • 基于模型的测试(英语:Model-based Testing)属于软件测试领域的一种测试方法。按照此方法,测试用例可以完全或部分的利用模型自动产生。以上所说的模型通常是指对被测系统(SUT,system under test)某些(通常是功能性的)方面的描述。

  • 模型一般都是对被测系统预期行为动作的抽象描述。这些测试用例的集合就是抽象测试套件(abstract test suite)。抽象测试套件不可以直接执行于需测试的系统,因为,他们不在同一抽象级别。

  • 测试套件(test suites)是由模型生成,而不是由源代码生成。因此,基于模型的测试又常常被当作黑盒测试的一种形式。但从某种层面来说,这并不十分准确。毕竟,基于模型的测试是与源代码级的测试覆盖率,以及对代码的功能测试都有着很大的关系。

行业案例

MBT 在微软的实践案例

Microsoft has been successfully applying model-based testing (MBT) to its internal development process for more than a decade now. MBT has proven a successful technique for a variety of internal and external software products. Its adoption has steadily increased over the years. Relatively speaking, it has been well received in the testing community, particularly when compared with other methodologies living on the “formal” side of the testing spectrum.

十多年来,Microsoft 已成功地将基于模型的测试 (MBT) 应用到其内部开发过程中。 MBT 已被证明是一种成功的技术,适用于各种内部和外部软件产品。多年来,它的采用率稳步上升。相对而言,它在测试界很受欢迎,尤其是与其他在测试范围内“正式”方面的方法相比。

Spec Explorer

Spec Explorer 是一个 Microsoft MBT 工具,它扩展了 Visual Studio,为创建行为模型提供了一个高度集成的开发环境,以及一个用于检查这些模型的有效性并从中生成测试用例的图形分析工具。我们相信这个工具是促进 MBT 作为一种有效技术在 IT 行业中的应用、缓解自然学习曲线并提供最先进的创作环境的转折点。

GraphWalker an open-source model-based testing tool

  • Editor
  • Command line tools
  • Integrate GraphWalker in your java project

GraphWalker 实战

核心概念

  • An edge represents an action, a transition.
  • A vertex represents verification, an assertion.
  • A model is a graph, which is a set of vertices and edges

GraphWalker 安装与部署

GraphWalker 流程

  • 创建模型
  • 生成抽象测试套件
  • 映射可执行测试套件
  • 执行测试

启动 Studio 设计模型

java -jar graphwalker-studio-4.3.1.jar

测试套件生成器算法

  • 路径覆盖
    • random(edge_coverage(100))
    • random(edge_coverage(50))
  • 状态覆盖
    • random(vertex_coverage(100))
    • random(vertex_coverage(50))
  • 达到特定状态
    • a_star(reached_vertex(v_VerifyFirstAction))
    • a_star(reached_edge(e_SomeAction))

保存模型

{
  "models": [
    {
      "name": "SmallTest",
      "id": "cbca8c67-6d0c-40b4-8afb-032b069a4bc1",
      "generator": "random(edge_coverage(100))",
      "actions": [],
      "vertices": [
        {
          "id": "d9c15804-24de-47ca-9430-9cb93202d9ff",
          "name": "v_VerifyInitialState",
          "actions": [],
          "requirements": [],
          "properties": {
            "x": 206.9166717529297,
            "y": 54.41667175292969
          }
        },
        {
          "id": "fd4435f7-6a42-4a8b-a522-49c6b91a85e1",
          "name": "v_VerifyFirstAction",
          "actions": [],
          "requirements": [],
          "properties": {
            "x": 348.9166717529297,
            "y": 368.4166717529297
          }
        },
        {
          "id": "3dbb2cf6-1ac9-4858-9fd8-bc6b874c1daf",
          "name": "v_NewVertex",
          "actions": [],
          "requirements": [],
          "properties": {
            "x": 98.91667175292969,
            "y": 228.4166717529297
          }
        }
      ],
      "edges": [
        {
          "id": "9a6b8be2-3e0c-4e8d-bb38-21107b66a4d3",
          "name": "e_FirstAction",
          "actions": [],
          "requirements": [],
          "properties": [],
          "sourceVertexId": "d9c15804-24de-47ca-9430-9cb93202d9ff",
          "targetVertexId": "fd4435f7-6a42-4a8b-a522-49c6b91a85e1"
        },
        {
          "id": "6034d0f8-9850-4d67-9ddf-02a24c35890e",
          "name": "e_AnotherAction",
          "actions": [],
          "requirements": [],
          "properties": [],
          "sourceVertexId": "fd4435f7-6a42-4a8b-a522-49c6b91a85e1",
          "targetVertexId": "3dbb2cf6-1ac9-4858-9fd8-bc6b874c1daf"
        },
        {
          "id": "71d24b33-fe8e-49ab-9fcc-10cc985dcdeb",
          "name": "e_SomeAction",
          "actions": [],
          "requirements": [],
          "properties": [],
          "sourceVertexId": "3dbb2cf6-1ac9-4858-9fd8-bc6b874c1daf",
          "targetVertexId": "fd4435f7-6a42-4a8b-a522-49c6b91a85e1"
        },
        {
          "id": "2d241ce5-ac46-4f23-85b8-2253c4ce8bed",
          "name": "e_SomeOtherAction",
          "actions": [],
          "requirements": [],
          "properties": [],
          "sourceVertexId": "3dbb2cf6-1ac9-4858-9fd8-bc6b874c1daf",
          "targetVertexId": "3dbb2cf6-1ac9-4858-9fd8-bc6b874c1daf"
        }
      ],
      "startElementId": "d9c15804-24de-47ca-9430-9cb93202d9ff"
    }
  ]
}

生成可执行测试

#生成样本项目
mvn archetype:generate -B \
  -DarchetypeGroupId=org.graphwalker \
  -DarchetypeArtifactId=graphwalker-maven-archetype \
  -DgroupId=com.ceshiren.hogwarts \
  -DartifactId=HogwartsMBT \
  -DarchetypeVersion=LATEST

#保存设计好的模型文件到 src/main/resources/com/ceshiren/hogwarts/PetClinic.json
mvn graphwalker:generate-sources

运行测试

mvn graphwalker:test

GraphWalker Player 可视化运行测试用例

curl https://raw.githubusercontent.com/GraphWalker\
/graphwalker-player/master/index.html -O

#使用浏览器打开如下地址
file:///Users/seveniruby/ke/mbt/index.html?wsURI=127.0.0.1:8887

#在代码中开启 websocket server

#执行测试
mvn clean \
  graphwalker:generate-sources \
  compile \
  exec:java -Dexec.mainClass=com.company.runners.WebSocketApplication
//在代码中开启 websocket server
        WebSocketServer server = new WebSocketServer(8887, executor.getMachine());
        server.start();

GraphWalker Web 自动化测试案例