How to limit the request body property to certain values? #1090
Replies: 5 comments 1 reply
-
I see a few ways of doing so with their own pros & cons.
You could manually change your API schema in place before it is loaded to Schemathesis: @schemathesis.register
def before_load_schema(context, schema) -> None:
schema["paths"]["/<operation>"]["<method>"]["parameters"][0]["schema"]["enum"] = ["Draft", "WaitingForApproval"] The example above should be adjusted according to your API spec version & the exact Pros:
Cons:
You can modify generated data via this hook: def set_status(val):
val["status"] = "Draft"
@schema.hooks.register("before_generate_body")
def schema_hook(context, strategy):
return strategy.filter(pred).map(set_status) Pros:
Cons:
@schema.parametrize()
@settings(max_examples=50, suppress_health_check=(HealthCheck.too_slow,))
def test_no_server_errors(case, flask_test_app, cookies):
case.body["status"] = "Draft"
... Pros & cons are similar to (2).
Here you can add some custom strategies without interacting with the schema at all. from hypothesis import strategies as st
@schema.parametrize()
@schema.given(data=st.data())
@settings(max_examples=50, suppress_health_check=(HealthCheck.too_slow,))
def test_no_server_errors(data, case, flask_test_app, cookies):
case.body["status"] = st.sampled_from(["Draft", "WaitingForApproval"])
... Pros:
Cons:
Personally, (4) is my favorite :) Let me know if these options work for you. |
Beta Was this translation helpful? Give feedback.
-
Yes, you might need to do it conditionally only for cases that require this property |
Beta Was this translation helpful? Give feedback.
-
%00 causes issues, is there a way to instruct schemathesis or hypothesis to exclude it? |
Beta Was this translation helpful? Give feedback.
-
Hi @KissPeter, it is one of the thing I will work on for the next Schemathesis release. Currently there are only options like changing the schema or using a hook, but Hypothesis recently added support for excluding null bytes directly, so I am going to integrate it here too. |
Beta Was this translation helpful? Give feedback.
-
See this comment for a solution that will work for the current version - #1668 (comment) |
Beta Was this translation helpful? Give feedback.
-
I'd like to limit tests request to not include
{"status": "Archived"}
. Only "Draft" and "WaitingForApproval" (POST request)What is the best way of doing it? Is it possible to define exact example "Draft" or do I need to filter it out?
test code
Beta Was this translation helpful? Give feedback.
All reactions