tavern
tavern copied to clipboard
How to pass fixtures to function in verify_response_with?
In my test, I have 2 fixtures:
fixture1contains the query to use for the testfixture2contains the expected response for that query to use in the test
test_name: A helpful name
marks:
- some_mark
- usefixtures:
- fixture1
- fixture2
stages:
- name: Make request
request:
url: "{endpoint}"
json:
query: "{fixture1}"
method: POST
headers:
content-type: application/json
response:
verify_response_with:
function: path.to.verification.function:function_name
extra_kwargs:
expected: fixture2
The query: "{fixture1}" works as expected, but path.to.verification.function:function_name recieves fixture2 literally instead of the fixture.
Is there a way for me to pass either the fixture or the result of running the fixture to the verification function itself?
Does doing
extra_kwargs:
expected: "{fixture2}"
Not work?
No.
When I do the above, expected is a tavern.util.formatted_str.FormattedString copy of the fixture result.
For example, if fixture2 returns a dict {"hello": 2}, then I get a tavern.util.formatted_str.FormattedString copy of {"hello": 2} which is not a dict.
So, in function_name, if I write: assert expected == {"hello": 2}, it will fail.
Is there way to get the result of the fixture or even the fixture itself be passed to function_name?
I guess I can try assert expected == something_to_convert_to_FormattedString({"hello": 2}), but I don't know:
- What this magical
something_to_convert_to_FormattedStringshould be? - … it feels wasteful and could potentially cause mismatch due to spaces (as they will be strgins instead of objects).
I am on tavern version "1.14.0", {file = "tavern-1.14.0-py3-none-any.whl", hash = "sha256:b9f601c24dc425cfdc3d03c61f08aa13aea43164f1aff6db8680d628ea67ae76"}
Hmm, @michaelboulton, after some digging around, I triedL
extra_kwargs:
expected: !force_format_include "{fixture2}"
This casts expected from a dict to a <class 'box.box.Box'> but now, assert expected == {"hello": 2}, passes instead of failing.
From reading https://pypi.python.org/pypi/python-box, it seems like Box is a subclass of dict so in this instance, it should just work as expected.
However, if fixture2 was to return something else like an object/class instance - would this approach have worked correctly?
It should work, but it's not a use case I've ever really thought about or used personally.