使用Pandaria编写API自动化测试进阶用法

栏目: 编程工具 · 发布时间: 6年前

内容简介:Pandaria本身以Java库的形式发布到Maven Central中,使用Pandaria本身只需要在构建工具中添加依赖即可,本身没有额外的安装操作。工欲善其事必先利其器,IDE能够使我们编写自动化测试事半功倍,此处推荐IntelliJ IDEA, 当然你可以选择其他你喜欢的用于开发Java的IDE。安装好IntelliJ IDEA后,我们可以创建一个标准的Maven工程。

Pandaria 是一款基于Cucumber JVM的API自动化测试工具,上一篇文章介绍了它的基本功能,包括基本的HTTP操作和数据库操作。本文将介绍使用Pandaria编写自动化测试的一些高级用法。

快速开始

安装

Pandaria本身以 Java 库的形式发布到Maven Central中,使用Pandaria本身只需要在构建 工具 中添加依赖即可,本身没有额外的安装操作。工欲善其事必先利其器,IDE能够使我们编写自动化测试事半功倍,此处推荐IntelliJ IDEA, 当然你可以选择其他你喜欢的用于开发Java的IDE。

创建工程

安装好IntelliJ IDEA后,我们可以创建一个标准的Maven工程。

使用Pandaria编写API自动化测试进阶用法

添加依赖

在build.gradle或者pom.xml中添加Pandaria依赖。

build.gradle

apply plugin: 'java'

dependencies {
    testCompile(
            "io.cucumber:cucumber-junit:4.0.0",
            'com.github.jakimli.pandaria:pandaria-core:0.2.3',
            'com.github.jakimli.pandaria:pandaria-db:0.2.3',
            'com.github.jakimli.pandaria:pandaria-mongo:0.2.3'
    )
}
复制代码

pom.xml

<dependencies>
  <dependency>
    <groupId>com.github.jakimli.pandaria</groupId>
    <artifactId>pandaria-core</artifactId>
    <version>0.2.3</version>
    <scope>test</scope>
  </dependency>
  <dependency>
    <groupId>com.github.jakimli.pandaria</groupId>
    <artifactId>pandaria-db</artifactId>
    <version>0.2.3</version>
    <scope>test</scope>
  </dependency>
  <dependency>
    <groupId>com.github.jakimli.pandaria</groupId>
    <artifactId>pandaria-mongo</artifactId>
    <version>0.2.3</version>
    <scope>test</scope>
  </dependency>
</dependencies>
复制代码

这里除了pandaria-core以外,还包含了pandaria-db和pandaria-mongo两个模块,如果你的项目不需要验证数据库,或者不需要验证mongo db,你可以不添加这两个模块。

本文使用JUnit, 依赖中也添加了cucumber-junit模块。

创建Cucumber Junit入口

在工程下面创建一个RunCucumberTest.java,这个文件使用Cucumber的Junit Runner,用于运行feature文件,使用Pandaria其实就是使用Cucumber,所有Cucumber本身的功能依然适用。

RunCucumberTest.java

package com.github.jakimli.pandaria_sample;

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
@CucumberOptions(plugin = "pretty",
        features = "classpath:features/",
        glue = {"com.github.jakimli.pandaria", "com.github.jakimli.pandaria_sample"})
public class RunCucumberTest {
}
复制代码

语法高亮和补全

现在可以开始编写第一个自动化测试了。

src/test/resource/features 下面创建以 .feature 结尾的文件,如test.feature。IntelliJ IDEA利用gherkin和Cucumber for Java两个插件提供gherkin的高亮和补全。可以在IntelliJ的插件安装中搜索这两个插件并安装,安装好后feature会高亮以及自动补全:

使用Pandaria编写API自动化测试进阶用法

HTTP

全局Http Header

Pandaria支持在配置文件( application.properties )中配置全局的Http Header,所有的Http Request都会带上这些Header。

一个典型的场景是,自动化测试运行在测试环境,当需要对API进行认真的时候,通常需要一个测试账号,将对应的认证信息放到HTTP的Authorization Header中。我们可以使用 http.headers.<name> 来配置。如:

application.properties

http.headers.Authorization=Bear Token
复制代码

上传文件

Pandaria支持文件上传,使用 attachment 关键字可以指定文件路径

Scenario: upload file
    * uri: /files
    * attachment: attachments/abc.txt
    * send: POST
    * status: 200
    * response body:
    """
    uploaded
    """
复制代码

Mongo DB

除了关系型数据库以外,Pandaria还支持对Mongo DB的操作和校验。

插入

通常我们需要往mongo的一个集合中插入测试数据,可以这么写:

* collection: 'users' insert:
"""
{"user": "jakim"}
"""
复制代码

清除

清除测试数据:

* collection: 'users' clear
复制代码

查询和验证

同样我们可以从Mongo DB中查询数据并做校验

* collection: 'users' find all
* verify: '$[0].user'="alice"
复制代码

也可以指定查询条件

* collection: 'users' find:
"""
{"age": {$gt: 17}}
"""
* verify: '$[0].user'="jakim"

* collection: 'users' find:
"""
{"age": {$lt: 17}}
"""
* verify: '$[0].user'="alice"

* collection: 'users' find: filter/greater_than_17.json
* verify: '$[0].user'="jakim"
复制代码

变量

普通变量

使用Pandaria,你可以使用基本的变量功能, ${<name>} 用于使用普通变量如:

你可以定义 envrionment 变量,然后在之后的URL中使用,这样如果你需要切换环境,就只需要改envrionment变量的值就好了。

Background:
    * var: 'environment'='test'

  Scenario: hello world
    * uri: https://${environment}/users/octocat/orgs
    * send: GET
    * status: 200
复制代码

你也可以在配置文件中指定变量的初始值

application.properties

variables.environment=test
复制代码
Scenario: initial value from configuration file
    * verify: ${environment}="test"
    * var: 'environment'="production"
    * verify: ${environment}="production"
复制代码

如上述,在feature文件中定义会覆盖配置文件中的值。

生成随机测试数据

随机的测试数据在自动化测试中很实用,Pandaria中你可以使用 #{<expression>} 的形式来生成测试数据,如:

Scenario: faker in request body as doc string
    * uri: /faker/users
    * request body:
    """
    {"name": "#{Name.fullName}", "city": "#{Address.city}"}
    """
    * send: POST
    * response body:
    """
    success
    """
复制代码

这里的 #{Name.fullName}#{Address.city} 会被替换成随机的人名和城市名。通过配置 faker.locale 可以切换语言。

上一次返回报文作为下一次请求报文

Pandaria支持将第一次HTTP请求的返回内容直接作为下一个请求的Request内容, 通过 @{<json path>} 的形式使用。

Scenario: request directly from last response

    * uri: /users/me
    * send: get
    * verify: '$.username'='jakim'
    * verify: '$.age'=18
    * verify: '$.iq'=double: 80.0

    * uri: /users
    * request body:
    """
    { "username": @{$.username}}
    """
    * send: POST
    * status: 200
    * verify: '$.id'='auto-generated'
    * verify: '$.username'='jakim'
    * verify: '$.age'=18

    * uri: /users
    * request body:
    """
    @{$}
    """
    * send: POST
    * status: 200
    * verify: '$.id'='auto-generated'
    * verify: '$.username'='jakim'
    * verify: '$.age'=18
复制代码

校验

验证JSON Schema

你可以验证一段JSON是否遵循给定的Json shcema:

* uri: /products/1
* send: get
* verify: '$' conform to:
"""
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://example.com/product.schema.json",
"title": "Product",
"description": "A product in the catalog",
"type": "object"
}
"""

* verify: '$' conform to: schema/product.schema.json

* verify: '$.tags' conform to:
"""
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "http://example.com/product.tags.schema.json",
  "title": "product tags",
  "description": "Product tags",
  "type": "array",
  "items": {
    "type": "string"
  }
}
"""
复制代码

使用Javascript自定义验证

一些基本的验证可以通过Javascript来进行,使用 code 关键字,如:

* var: 'age'=16
* var: 'iq'=90.0

* uri: http://localhost:10080/not_important
* send: get
* verify: '$.age'=code: ${age} + 2
* verify: '$.iq'=code: ${iq} - 10

* verify: '$.age'!=code: ${age} + 3
* verify: '$.iq'!=code: ${iq} - 11

* verify: '$.age'=code:
"""
${age} + 2
"""
* verify: '$.iq'=code:
"""
${iq} - 10
"""
复制代码

或者

* verify code: ${name} == ${iq} / 3
* verify code:
"""
${name} != ${iq} % 3
"""
* verify code file: verification.js
复制代码

集成CI

使用Pandaria,结合Junit,运行测试就像运行单元测试一样,你只需要在CI上运行 mvn test 即可。


以上所述就是小编给大家介绍的《使用Pandaria编写API自动化测试进阶用法》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Haskell School of Music

The Haskell School of Music

Paul Hudak、Donya Quick / Cambridge University Press / 2018-10-4 / GBP 42.99

This book teaches functional programming through creative applications in music and sound synthesis. Readers will learn the Haskell programming language and explore numerous ways to create music and d......一起来看看 《The Haskell School of Music》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

MD5 加密
MD5 加密

MD5 加密工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具