-
-
Notifications
You must be signed in to change notification settings - Fork 152
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Question] Remove testcases containing NULL or \000 when testing my API #1668
Comments
I know it's difficult to go through each issue, but can someone please direct me in the right direction on how to fix this issue |
Hi! Thank you for pinging and sorry for the delay :) The underlying data generator, hypothesis-jsonschema, generates only data that matches the schema, hence the most effective way to avoid these values is to tune the schema. However it is not always practically convenient. Leaving aside possible changes to hypothesis-jsonschema that will simplify some use cases, you can use Schemathesis hooks. I’ll share some examples shortly |
Hey @Stranger6667 , thanks for taking the time🙌. |
Also, are the hooks available in schemathesis-github-actions, maybe as arguments?
|
There are two ways to change how Schemathesis generates data.
UserMetadata:
type: object
nullable: false
properties:
metadata:
type: object
nullable: false
propertyNames:
pattern: "^[a-zA-Z]*$"
additionalProperties: true
description: Arbitrary, object-encoded user's data.
required:
- metadata Here is a hook you may use: import schemathesis
@schemathesis.hook
def before_load_schema(context, raw_schema):
# Modify the schema at the place where UserMetadata lives
raw_schema["components"]["schemas"]["UserMetadata"]["properties"]["metadata"]["propertyNames"] = {"pattern": "^[a-zA-Z]*$"}
import schemathesis
@schemathesis.hook
def before_generate_case(context, strategy):
op = context.operation
def no_nulls(case):
# Run this filter only for "PATCH /users/{user_id}/" endpoint
if op.method == "PATCH" and op.path == "/users/{user_id}/":
# Check that generated body has no nulls
return all("\x00" not in key for key in case.body["metadata"])
return True
return strategy.filter(no_nulls) Filtering is a bit clunky as you need to check potentially nested structures of different kinds. Here is how you connect hooks to your Schemathesis run. P.S. I plan to overhaul the hooks system and if you'll find something that is not convenient or you have any comments on it, please feel free to share it in this issue |
Oh, that's an oversight! Currently, |
And thank you so much for sharing your context and asking questions. It is extremely valuable for Schemathesis :) |
Thank you for all the help @Stranger6667 , this'll really speed up the progress on my task! |
I just had one more question, is it possible to run tests on multiple yml files in a single command? |
I'd suggest running tests via GH Action matrix instead as Schemathesis is designed to run against one schema at a time |
Thanks for the tip @Stranger6667 ! I'll look into this. |
Hey @AryanGodara FYI - Hooks are now supported in Schemathesis GitHub Action starting from |
Thanks for the quick update on this @Stranger6667 ! |
Checklist
I have a common problem while testing my API (written in golang). Many fields test by putting string values as
null
or\u0000
I want to turn off null values to be tested, but can't find openAPI specification docs for this. Is there an option to do so in schemathesis?
Below is a map[string]{interface} object in golang, described in openAPI.
And the following is a failing command generated by st report :-
where example of MF_TOKEN is
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2NzQzNDA5MDgsImlhdCI6MTY3NDMwNDkwOCwiaXNzIjoibWFpbmZsdXguYXV0aCIsInN1YiI6ImV4YW1wbGVAZWcuY29tIiwiaXNzdWVyX2lkIjoiNzE0NTk5MmYtMzZkZi00NjE5LWE1YzQtOGJkMzg2YjI3YmE5IiwidHlwZSI6MH0.b5_D2lpOtqJ_CNtVO4AU9ieJULNM3w4rj2-DGt-otwE
The text was updated successfully, but these errors were encountered: