+ive, -ive test for Response body containing element & array

My response body is below, how can I write below tests -

  1. Response body contains elements (‘oldestBlock’,‘reward’, ‘baseFeePerGas’,‘gasUsedRatio’), in a single test
    I tried below but it should fail instead of pass because “gasUsedRatios” contains extra ‘s’ at end, correct value = gasUsedRatio.

pm.expect(pm.response.text()).to.includes(“oldestBlock”,“reward”, “baseFeePerGas”,“gasUsedRatios”)

  1. Response body does not contains elements except above ones.

  2. reward is an array & shouldn’t be empty

  3. value in reward array should begin with “0x”

  4. Below Json schema validation

{
    "jsonrpc": "2.0",
    "id": "1",
    "result": {
        "oldestBlock": "0x128c528",
        "reward": [
            [
                "0xc2f6ed",
                "0xc2f6ed"
            ],
            [
                "0xbd4bf3",
                "0xc2f6ed"
            ],
            [
                "0xbd1d99",
                "0xbd4bf3"
            ],
            [
                "0xbd1d99",
                "0xbd1d99"
            ],
            [
                "0xf4240",
                "0xb79e27"
            ]
        ],
        "baseFeePerGas": [
            "0xb3a42c70d",
            "0xb367ecc49",
            "0xaaeb73d77",
            "0xa3480338a",
            "0xa6f4fc367",
            "0xa98e65669"
        ],
        "gasUsedRatio": [
            0.49475963333333334,
            0.3107929,
            0.3212392,
            0.5900452,
            0.5622738
        ]
    }
}

You can use the Ajv JSON Schema validator.

Validate Response Structure - Postman Learning Center

You’ll have to search the web for guides as the Postman Learning Center example is fairly basic.

Required field will cover test 1, you can set additionalProperties to false to cover test 2. You can also use regex patterns to validate test 4.

Also have a look at the following.

Thank You, will check out.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.