How to add random integer for each case in Schemathesis?

242 views Asked by At

I'm trying to test an API endpoint with random input for mid and cids (code below). However, whenever I run the test it says missing required positional arguments. Can anyone please help?

@schema.parametrize()
@schema.given(mid=st.integers(), cids=st.lists(st.integers()))
@settings(max_examples=1)
def test_api_customised(mid, cids, case):
    case.headers = case.headers or {}
    case.headers['Authorization'] = "apiKey " + str(base64_composer)
    case.headers['Content-Type'] = "application/json"
    
    # CREATE JOB
    if case.method == "POST":
        if isinstance(case.body, dict):
            case.body['moduleId'] = mid
            case.body['clientIds'] = cids
            print(case.body)

    response = case.call()
    case.validate_response(response)

And I got this error:

    TypeError: test_api_customised() missing 2 required positional arguments: 'mid' and 'cids'

1

There are 1 answers

0
Stranger6667 On

It is likely caused by the presence of explicit examples in the API schema. See this issue.

A temporary solution would be to exclude the explicit phase:

...  # Skipped for brevity
from hypothesis import settings, Phase

# Used in `@settings`
PHASES = phases=set(Phase) - {Phase.explicit}

@schema.parametrize()
@schema.given(mid=st.integers(), cids=st.lists(st.integers()))
@settings(max_examples=1, phases=PHASES)
def test_api_customised(mid, cids, case):
    ...  # the rest of the test

A more comprehensive solution requires changes in Schemathesis (see this issue)

You could check whether it is the case for you by removing examples / example / x-examples / x-example keywords (depending on your API spec version) from the API schema. If it is not the case, I encourage you to report this issue with more details (preferably including your API schema).