Test JSON data format using Pytest and Schema assertions

Foreword

During the test process, the author received such a test task. After the interface was refactored, test students were required to verify the structure and type of the data to prevent an increase in the probability of hidden problems due to a field being deleted or the type being changed. So how to verify the data structure and type? With this question, let’s explore together.

JSON Schema

The problems mentioned in the preface need to be solved using JSON Schema. So what exactly is JSON Schema and how to use it?

What is JSON Schema

JSON Schema is a language for validating and describing the structure of JSON data. It provides a standardized and canonical method to define the structure, data types and related constraints of JSON data.

Basic syntax of JSON Schema

JSON Schema uses JSON objects to represent data structures and constraint rules. Here are some common JSON Schema keywords:

  • $schema: Specifies the JSON Schema specification version used.
  • type: Specify the type of data, such as string, number, Boolean value, etc.
  • properties: Define object properties and their corresponding constraint rules.
  • required: Specifies required attributes.
  • enum: Defines a list of enumeration values.
  • pattern: Use regular expressions to verify string format.
  • minimum and maximum: Define the minimum and maximum values of the value.
  • format: Define specific data formats, such as date, time, etc.
Demo case

Let’s look at this example that demonstrates how to use JSON Schema to validate user-submitted form data:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "name": {
      "type": "string"
    },
    "email": {
      "type": "string",
    },
    "age": {
      "type": "integer",
      "minimum": 18
    }
  },
  "required": ["name", "email"]
}

In the above example, we define an object containing the name, email and age properties and specify their type and necessity.

Let’s test it out:

from jsonschema import validate
?
x = {
    "name": "aomaker",
    "age": 2,
    "email": "111"
}
schema = {
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "name": {
      "type": "string"
    },
    "email": {
      "type": "string",
      "format": "email"
    },
    "age": {
      "type": "integer",
      "minimum": 18
    }
  },
  "required": ["name", "email"]
}
?
validate(x, schema)

In this code, the age value given in the x data is 2, but the schema requires a minimum value of age If it is 18, the verification should fail. The execution found that an error was reported:

jsonschema.exceptions.ValidationError: 2 is less than the minimum of 18

We set the value of age to be greater than or equal to 18, and the verification will pass.

In this way, we can verify the data submitted by the user with the above JSON Schema, so that we can ensure the legitimacy of the data and provide timely feedback to the user about errors or missing information.

Do you think everything will be fine once you get here? If the response data has many fields, it will be very difficult to write Schema one by one, and I feel like giving up. Fortunately, there is genson.

genson

genson is a Python library that generates schemas that conform to the JSON Schema specification. genson automatically generates the corresponding JSON Schema by analyzing existing JSON data.

Installation

Before use, you first need to install it. Installation is very simple, just execute the following command:

pip install genson

SchemaBuilder

SchemaBuilder is a class in the Genson library for building JSON Schema. It builds a JSON Schema object by adding elements such as objects, properties, etc. and converts it to JSON format using the to_schema method.

Usage

add_object(obj: Any) -> 'SchemaBuilder'

Add an arbitrary Python object containing data, such as a dictionary, list, or custom class. This method will generate the corresponding JSON Schema based on the incoming data.

from genson import SchemaBuilder
?
builder = SchemaBuilder()
builder.add_object({'name': 'John', 'age': 25})
schema = builder.to_schema()
print(schema)
# Output: {"type": "object", "properties":
        {"name": {"type": "string"}, "age": {"type": "integer"}},
        "required": ["age", "name"]}

add_property(name: str, value: Any) -> 'SchemaBuilder'

Adds a property to the object currently being constructed. This method receives two parameters, name represents the attribute name, and value represents the attribute value. After adding attributes, you can use the add_object method to continue adding data internally.

from genson import SchemaBuilder
?
builder = SchemaBuilder()
builder.add_property('name', 'John')
builder.add_property('age', 25)
schema = builder.to_schema()
print(schema)
# Output: {"type": "object", "properties":
        {"name": {"type": "string"}, "age": {"type": "integer"}},
        "required": ["name", "age"]}

to_schema() -> Dict[str, Any]

Convert the built JSON Schema to Python dictionary format.

from genson import SchemaBuilder
?
builder = SchemaBuilder()
builder.add_object({'name': 'John', 'age': 25})
schema = builder.to_schema()
print(type(schema)) # Output: <class 'dict'>

These are commonly used SchemaBuilder methods and usage.

It is much more convenient to have this class. We can use this class to automatically generate a jsonschema verification grammar.

Implementation case
from genson import SchemaBuilder
?
def genson(data: dict = None):
    """
    return schema data
    """
?
    builder = SchemaBuilder()
    builder.add_object(data)
    to_schema = builder.to_schema()
    return to_schema

In this code, inside the function, we create a SchemaBuilder object builder. SchemaBuilder is used to build JSON Schema objects.

Next, we add the parameter data to the builder using the add_object method. This will generate the corresponding JSON Schema based on the incoming data.

Finally, we call the to_schema method to convert the builder into a JSON Schema object and store the result in the to_schema variable.

Finally, we return the generated JSON Schema object as the return value of the function.

By calling this genson function and passing in a dictionary object, you will get the JSON Schema corresponding to the dictionary data.

x = {
    "name": "aomaker",
    "age": 18,
    "email": "[email protected]"
}
schema = genson(x)
?
print(schema)

With this piece of code, you can see the results we expect:

{'$schema': 'http://json-schema.org/schema#', 'type': 'object', 'properties': {'name ': {'type': 'string'}, 'age': {'type': 'integer'}, 'email': {'type': 'string'}}, 'required': ['age', 'email', 'name']}
?

pytestAdd assertion Schema

def assert_schema(schema, response):
    """
    Assert JSON Schema
    """
    try:
        validate(instance=response, schema=schema)
    except ValidationError as msg:
        raise AssertionError

The code defines a function called assert_schema that verifies whether the JSON data conforms to the given JSON Schema.

This function accepts two parameters:

  • schema: Represents the JSON Schema to be validated, usually a dictionary or object loaded from a JSON file.
  • response: Represents the JSON data to be validated, usually the data obtained from the API response.

The main logic of the function is to use the validate function to compare whether response and schema match. If there is a mismatch, a ValidationError exception will be raised.

have a test:

schema = {
    "type": "object",
    "properties": {
        "name": {"type": "string"},
        "age": {"type": "integer"}
    },
    "required": ["name", "age"]
}
?
response = {
    "name": "John Doe",
    "age": 25
}
?
#Call assert_schema function for verification
assert_schema(schema, response)
?

If the API response data does not conform to JSON Schema, the assert_schema function will raise an assertion error and display a detailed validation error message.

Finally

Although genson can help us quickly generate Scheam, the expected results still need to be maintained manually. Of course, in the future, when we automate the interface, we can automatically store it in the database when the interface is called, and then continue to maintain the data of this table. We will find a way to implement it later. JSON Schema can already solve the problems mentioned in our preface. JSON Schema can help us define and verify the structure, type and constraints of JSON data, ensure the validity and consistency of the data, and improve the quality and reliability of data interaction.


———————————END——————- ——–

Digression

In this era of big data, how can you keep up with scripting without mastering a programming language? Python, the hottest programming language at the moment, has a bright future! If you also want to keep up with the times and improve yourself, please take a look.

Interested friends will receive a complete set of Python learning materials, including interview questions, resume information, etc. See below for details.


CSDN gift package:The most complete “Python learning materials” on the Internet are given away for free! (Safe link, click with confidence)

1. Python learning routes in all directions

The technical points in all directions of Python have been compiled to form a summary of knowledge points in various fields. Its usefulness is that you can find corresponding learning resources according to the following knowledge points to ensure that you learn more comprehensively.

img
img

2. Python essential development tools

The tools have been organized for you, and you can get started directly after installation! img

3. Latest Python study notes

When I learn a certain basic and have my own understanding ability, I will read some books or handwritten notes compiled by my seniors. These notes record their understanding of some technical points in detail. These understandings are relatively unique and can be learned. to a different way of thinking.

img

4. Python video collection

Watch a comprehensive zero-based learning video. Watching videos is the fastest and most effective way to learn. It is easy to get started by following the teacher’s ideas in the video, from basic to in-depth.

img

5. Practical cases

What you learn on paper is ultimately shallow. You must learn to type along with the video and practice it in order to apply what you have learned into practice. At this time, you can learn from some practical cases.

img

6. Interview Guide

Resume template

CSDN gift package:The most complete “Python learning materials” on the Internet are given away for free! (Safe link, click with confidence)

If there is any infringement, please contact us for deletion.