-
Notifications
You must be signed in to change notification settings - Fork 38
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
Better Error Messages. #244
Comments
Do you mean show all existing/added mock routes that didn't match the request? |
Well, first step will be to show all the set data for the request that was make. (with the JSON/Content shorten if too long) Then there would be a matter of prioritize what is most likely right/wrong, then it can be split up into something like Binary Tree. No need to show all GET request, if it was a POST request that was make.
|
In your example you only have 1 mocked route per test case, e.g. A common pattern is to have a respx mock router that holds all available endpoints for the external api that is mocked. When a httpx request is made all routes/patterns are tested and it's only when no match is found that the assertion error mentioned is triggered. This means that it's not possible to show a single non-matching mock route, it will have to print all added mocks which may be a bit bloated? |
It will help a lot just to print the full request with all the data set. So if there was make a: httpx.get(
url='https://example.com/hey',
params={'expand':0},
headers={'session': 'TheSuperSecretKey'},
json={"code": 42},
) And it did not match anything mocked, I will get an error with all the values set like: Then it can be enhanced with it letting know the first element on the priority list that did not match. Then it do not matter how many requests was mocked, it will only be two line of error messages. |
Fair enough. Currently we're relying on the Line 250 in 58ad17e
To print more details about the actual request, we'll need to write our own httpx request repr string, which would be doable. Not sure though how or if it should be limited, e.g. like you're suggesting with post body etc. |
Have had a look into the structure of So the hardest part will be finding a string structure people can read fast and still have all the needed info. 😅 If you are okay with a msg: str = generate_error_msg(request)
raise AllMockedAssertionError(msg) Maybe something like: def generate_error_msg(rqt: httpx.Request) -> str:
limit: int = 25
end_len: int = 10
start_len: int = limit - end_len - 3
stream_data = (
f"{rqt.stream._stream}"
if len(rqt.stream._stream) <= limit else
f"{rqt.stream._stream[:start_len]}...{rqt.stream._stream[-end_len:]}"
)
rqt_repr: str = (
f"{rqt.method}, "
f"{rqt.utl}, "
f"{rqt.headers}, "
f"ByteStream({stream_data})"
)
return f"RESPX: Mocked request not found.\nRequest({msg})" Do not like accessing the stream data like: Should result in:
With long json:
|
IMO content/stream is not suited for output since it can be "whatever" and potentially give problems, e.g. binary, multipart, encoding, streaming etc.
Agree, we should touch private stuff. The current
Isn't the failing test case enough here? Or is it maybe the stack trace that is skewed due to the mocking and should be looked at if we can do something there? |
Like I said, the only other way is a loop. Can always change the way to: s_data = bytes([x for x in itertools.islice(rqt.stream, limit)])
stream_data = f"{s_data}..." The content/stream might be "whatever", but since it is printed as bytes (Which where done in the example), it do not print any none printable or any formatting characters, but instead the hex code. The headers is cluttered?
I do not see how that could be make to help.. But the case still stand that the only thing missing for the library is good error Messages. |
With given code:
it gives the same error for all test cases:
respx.models.AllMockedAssertionError: RESPX: <Request('GET', 'https://example.com/')> not mocked!
.It would be nice if it wrote where the different occurred.
The text was updated successfully, but these errors were encountered: