Validate HTTP responses against example values while running tests #1029
Replies: 6 comments 16 replies
-
Some of us run contract tests against an application with mocked dependencies where the only real part involved is the Controller layer (as in MVC). For these types of tests we are able to provide mock responses, where we can validate how the controller transforms them to JSON beyond the scope of syntax which schema validation provides (by doing this, we focus tests on the contract side, without verifying application logic, which run faster, and allow us to focus the rest of the testing efforts on unit tests, integration, and E2E or component). In addition, and this is not less important, using the actual examples in the Swagger for validation allows us to validate the documentation as well. Documentation is never going to base itself on examples that are wrong. |
Beta Was this translation helpful? Give feedback.
-
Thanks @harikrishnan83 for this much awaited improvement. my thoughts on the below
Yes it is true that contract validation should not care for specific value, but the purpose of this capability SHOULD be to validate the "shape-of-the-payload" (but it is not atm), meaning the expected combination of the keys are present in the response for a given request. I mentioned the usecase in this thread : #709 |
Beta Was this translation helpful? Give feedback.
-
@harikrishnan83 , I tested with the above flag, but unfortunately it is not helpful. As I mentioned, the contract test should not be validating the specific value, otherwise it will be flaky tests. However, my expectation is to validate response to ensure all JSON keys (including the optional) match with the expectation. |
Beta Was this translation helpful? Give feedback.
-
Hi @harikrishnan83, I can't find the version 1.3.13 in maven central repository. |
Beta Was this translation helpful? Give feedback.
-
I couldn't help but notice that the flag does not act on externalized test data Quite a problem since we have no way of specifying header example values inside the OpenAPI spec (at least specmatic will ignore header examples in the request). Also noticed that examples in OpenAPI file are "either/or", so I either run the examples on the OpenAPI file or test against JSON files in the tests folder. Am I correct in my diagnosis or might I be missing something? |
Beta Was this translation helpful? Give feedback.
-
@harikrishnan83 I have a question (bug?) about this (otherwise really awesome, thanks for the hard work!) new feature when an endpoint doesn't have any parameters. See the attached zip for a minimal example to reproduce: specmatic-no-validation.zip The endpoint As a workaround I'm currently just programmatically adding these dummy examples to requests without parameters, since I'm going to guess it has something to do with The one edge case that I can think of, is if there's more than 1 example for a given status code. But I don't see how that would be useful for automatic specmatic validation anyway, so my suggestion would be to throw an error if there's multiple examples for a parameterless request and |
Beta Was this translation helpful? Give feedback.
-
We have received several queries about why Specmatic contract tests only verify the schema of HTTP responses based on OpenAPI specs? Why do we not verify the values in these responses agains the examples in the OpenAPI spec or externalised test data JSONs.
Our response has been that verifying the schema of response validates the signature / contract of the API, however validating the value will result in Specmatic contract tests also verifying application logic. We have been trying to separate these two concerns (Contract vs API / Component tests) so far.
However as an experiment we have added below env variable as a flag to turn on response validation (default
false
).VALIDATE_RESPONSE=true
VALIDATE_RESPONSE_VALUE=true
We request your valuable feedback on this capability (available from release 1.3.1 onwards).
UPDATE: The name of the env variable above has been updated as of release 1.3.13
Beta Was this translation helpful? Give feedback.
All reactions