等峰也等你

博客园 首页 新随笔 联系 订阅 管理

Model-based Testing 介绍

 

Model-based Testing is an application of model-based design for designing and optionally also executing artifacts to perform software testing or system testing. Models can be used to represent the desired behavior of a system under test (SUT), or to represent testing strategies and a test environment. The picture on the right depicts the former approach.

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

Model-based Testing 中的 model

 

A model describing a SUT is usually an abstract, partial presentation of the SUT’s desired behavior. Test cases derived from such a model are functional tests on the same level of abstraction as the model. These test cases are collectively known as an abstract test suite. An abstract test suite cannot be directly executed against an SUT because the suite is on the wrong level of abstraction. An executable test suite needs to be derived from a corresponding abstract test suite. The executable test suite can communicate directly with the system under test. This is achieved by mapping the abstract test cases to concrete test cases suitable for execution. In some model-based testing environments, models contain enough information to generate executable test suites directly. In others, elements in the abstract test suite must be mapped to specific statements or method calls in the software to create a concrete test suite. This is called solving the “mapping problem”.[1]

Model-based Testing 中的模型

 

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

模型构建方法

 
  • 有限状态机
  • UML
  • 编程语言
  • 数学方法

 

Model-based Testing 中的 testing

 

Tests can be derived from models in different ways. Because testing is usually experimental and based on heuristics, there is no known single best approach for test derivation. It is common to consolidate all test derivation related parameters into a package that is often known as “test requirements”, “test purpose” or even “use case(s)”. This package can contain information about those parts of a model that should be focused on, or the conditions for finishing testing (test stopping criteria).

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

Model-based Testing 补充说明

 

Because test suites are derived from models and not from source code, model-based testing is usually seen as one form of black-box testing.
Model-based Testing for complex software systems is still an evolving field.

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

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

 
  • 基于模型的测试(英语: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 是一个 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 流程

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

启动 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 自动化测试案例

posted on 2023-12-31 22:21  等峰也等你  阅读(166)  评论(0编辑  收藏  举报