当今接口测试越来越重要,一般情况下我们总是会对接口的返回的json字符串进行验证,看返回是否跟我们的预期相符。不过很多情况下我们会遇到下面的问题
那如何解决呢?
这时候json schema就派上用场了。
JSON Schema 是一种 JSON 媒体类型,用于定义 JSON 数据的结构。JSON 模式旨在定义 JSON 数据的验证,可用于验证响应和请求 JSON。在 JSON Schema 中,我们可以验证数据类型、字段是否为必填、最小长度或最大长度等。
下面的数据代表了一个员工的信息
employeeId
employeeName
employeeAge
jobTitle
hobby
{
"employeeId": 1,
"employeeName": "Fulan",
"employeeAge": 23,
"jobTitle": "SDET",
"hobby": [
"watch movies",
"play football"
]
}
上面的定义其实是有一些疑问的,比如
我们可以通过生成JSON schema来回答上面的问题
{
"$schema": "http://json-schema.org/draft-07/schema",
"$id": "http://example.com/example.json",
"type": "object",
"title": "The root schema",
"description": "The root schema comprises the entire JSON document.",
"default": {},
"examples": [
{
"employeeId": 1,
"employeeName": "Fulan",
"employeeAge": 23,
"jobTitle": "SDET",
"hobby": [
"watch movie",
"play football"
]
}
],
"required": [
"employeeId",
"employeeName",
"employeeAge",
"jobTitle",
"hobby"
],
"properties": {
"employeeId": {
"$id": "#/properties/employeeId",
"type": "integer",
"title": "The employeeId schema",
"description": "An explanation about the purpose of this instance.",
"default": 0,
"examples": [
1
]
},
"employeeName": {
"$id": "#/properties/employeeName",
"type": "string",
"title": "The employeeName schema",
"description": "An explanation about the purpose of this instance.",
"default": "",
"examples": [
"Fulan"
]
},
"employeeAge": {
"$id": "#/properties/employeeAge",
"type": "integer",
"title": "The employeeAge schema",
"description": "An explanation about the purpose of this instance.",
"default": 0,
"examples": [
23
]
},
"jobTitle": {
"$id": "#/properties/jobTitle",
"type": "string",
"title": "The jobTitle schema",
"description": "An explanation about the purpose of this instance.",
"default": "",
"examples": [
"SDET"
]
},
"hobby": {
"$id": "#/properties/hobby",
"type": "array",
"title": "The hobby schema",
"description": "An explanation about the purpose of this instance.",
"default": [],
"examples": [
[
"watch movies",
"play football"
]
],
"additionalItems": true,
"items": {
"$id": "#/properties/hobby/items",
"anyOf": [
{
"$id": "#/properties/hobby/items/anyOf/0",
"type": "string",
"title": "The first anyOf schema",
"description": "An explanation about the purpose of this instance.",
"default": "",
"examples": [
"watch movies",
"play football"
]
}
]
}
}
},
"additionalProperties": true
}
看上去很多很乱对不对,别着急,我们慢慢看
更具体一点
properties里定义了各个字段的详情,我们可以在里面增加更多的约束
{
"$schema": "http://json-schema.org/draft-07/schema",
"$id": "http://example.com/example.json",
"type": "object",
"title": "The root schema",
"description": "The root schema comprises the entire JSON document.",
"default": {},
"examples": [
{
"employeeId": 1,
"employeeName": "Fulan",
"employeeAge": 23,
"jobTitle": "SDET",
"hobby": [
"watch movie",
"play football"
]
}
],
"required": [
"employeeId",
"employeeName",
"employeeAge",
"jobTitle",
"hobby"
],
"properties": {
"employeeId": {
"$id": "#/properties/employeeId",
"type": "integer",
"title": "The employeeId schema",
"description": "An explanation about the purpose of this instance.",
"default": 0,
"examples": [
1
]
},
"employeeName": {
"$id": "#/properties/employeeName",
"type": "string",
"title": "The employeeName schema",
"description": "An explanation about the purpose of this instance.",
"default": "",
"examples": [
"Fulan"
]
},
"employeeAge": {
"$id": "#/properties/employeeAge",
"type": "integer",
"title": "The employeeAge schema",
"description": "An explanation about the purpose of this instance.",
"default": 0,
"exclusiveMinimum": 20,
"examples": [
23
]
},
"jobTitle": {
"$id": "#/properties/jobTitle",
"type": "string",
"title": "The jobTitle schema",
"description": "An explanation about the purpose of this instance.",
"default": "",
"minLength": 4,
"examples": [
"SDET"
]
},
"hobby": {
"$id": "#/properties/hobby",
"type": "array",
"title": "The hobby schema",
"description": "An explanation about the purpose of this instance.",
"default": [],
"examples": [
[
"watch movies",
"play football"
]
],
"additionalItems": true,
"items": {
"$id": "#/properties/hobby/items",
"anyOf": [
{
"$id": "#/properties/hobby/items/anyOf/0",
"type": "string",
"title": "The first anyOf schema",
"description": "An explanation about the purpose of this instance.",
"default": "",
"examples": [
"watch movies",
"play football"
]
}
]
},
"uniqueItems": true
}
},
"additionalProperties": true
}
在上面的例子中我们规定
json schema合约可以尽可能的详细,这样模糊的点就会相对较少,验证的结果会更加的准确。
这里以java为例,首先我们引入json schema的支持,然后定义断言工具,最后在用例中使用该断言。
引入json schema支持,这里用的是https://github.com/everit-org/json-schema,pom.xml如下
4.0.0
org.example
json-schema
1.0-SNAPSHOT
8
8
org.testng
testng
7.4.0
test
org.everit.json
org.everit.json.schema
1.3.0
com.fasterxml.jackson.core
jackson-databind
2.12.4
io.rest-assured
rest-assured
4.3.0
test
view raw
定义断言工具函数
package utils;
import com.fasterxml.jackson.databind.ObjectMapper;
import functest.APITest;
import org.everit.json.schema.Schema;
import org.everit.json.schema.ValidationException;
import org.everit.json.schema.loader.SchemaLoader;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.testng.Assert;
import java.util.logging.Logger;
public class JsonSchemaUtils {
private static Logger LOGGER= Logger.getLogger(String.valueOf(JsonSchemaUtils.class));
private ObjectMapper objMapper = new ObjectMapper();
public void checkJsonSchema(String jsonSchemaPath, String jsonSubject) throws ValidationException {
JSONObject retVal = new JSONObject();
try {
JSONObject jsonSchema = new JSONObject(new JSONTokener(APITest.class.getResourceAsStream(jsonSchemaPath)));
Schema schema = SchemaLoader.load(jsonSchema);
schema.validate(objMapper.convertValue(jsonSubject, JSONObject.class));
retVal.put("errorMessage","");
} catch (ValidationException ex) {
ex.printStackTrace();
LOGGER.info("JSON Schema Error Message: " + ex.getMessage());
retVal.put("errorMessage",ex.getMessage());
Assert.assertEquals(retVal.getString("errorMessage"), "");
}
Assert.assertEquals(retVal.getString("errorMessage"), "");
}
}
在用例中使用
package functest;
import io.restassured.RestAssured;
import org.json.JSONObject;
import org.testng.annotations.Test;
import io.restassured.response.Response;
import utils.JsonSchemaUtils;
public class APITest {
private final static String JSON_SCHEMA_ACTIVITY_PATH = "/schema/apiTest-jsonSchema.response.json";
JsonSchemaUtils jsonSchemaUtils = new JsonSchemaUtils();
@Test
public void getTest() {
Response response = RestAssured.get("https://www.boredapi.com/api/activity/");
JSONObject jsonObj = new JSONObject(response.getBody().asString());
System.out.println(jsonObj.toString(4));
jsonSchemaUtils.checkJsonSchema(JSON_SCHEMA_ACTIVITY_PATH,response.asString());
}
}
JSON schema 是一个多功能库,可以帮助我们执行 API 测试,使用 JSON 文件定义 schema 要求的能力显示了这个库的强大功能。希望这些示例能让您了解如何在项目中使用模式验证。
最后: 下方这份完整的软件测试视频教程已经整理上传完成,需要的朋友们可以自行领取 【100%免费无套路】