tavern
tavern copied to clipboard
[Question] Common objects at the root of test cases
I am using a plugin tavern_fastapi and that requires me to repeat this snippet across test files:
fastapi:
app:
location: "tests.fastapi_test_app"
Now I know how to include common variables like so:
includes:
- !include common_variables.yaml
but if I wanted to include the first snippet across tests, is there a way to do so?
A complete test_a.tavern.yaml look like:
---
test_name: output comparison tests
includes:
- !include common.yaml
# TODO: Move this into a common.root.yaml
fastapi:
app:
location: "tests.fastapi_test_app"
# TODO: Move this into a common.root.yaml
strict:
- headers:off
- json:off
marks:
- usefixtures:
- compare_service
stages:
- name: Compute resource
request:
url: "{endpoint}/{name}"
method: GET
headers:
content-type: application/json
response:
status_code: 200
verify_response_with:
function: tests.fastapitavern.utils:compare
For the first part (using the common 'fastapi' tag) you can either define it in one test at the top of the file and reuse it in other tests using a yaml anchor like in the MQTT tests https://github.com/taverntesting/tavern/blob/1559ce84a391dedd3f954f83dc376e512367060f/example/mqtt/test_mqtt.tavern.yaml#L8-L17 , or use a hook to set it. In your conftest.py, put something like
def pytest_tavern_beta_before_every_test_run(test_dict, variables):
test_dict["fastapi"] = {"app": {"location": "tests.fastapi_test_app"}}
and this should automatically add it into every test.
For the second one (setting the 'strict' defaults) it can be done in your pytest.ini file like this:
[pytest]
tavern-strict=
headers:off
json:off
# other options...
Right, but is there a way to refactor https://github.com/taverntesting/tavern/blob/1559ce84a391dedd3f954f83dc376e512367060f/example/mqtt/test_mqtt.tavern.yaml#L8-L17 into a separate file.
Assume that I come to realize that host, port and client_id needs to be changed. If I have that block in a separate file, that's "included" by tests, then I need to update that one file However, if I have that block insitu in every test, then I need to update all the tests
- Is there already an ability to do this?
- If not, what additional effort/work would be needed to do this?
- For example: can
!includebe extended to support this? If so, what changes/additional effort/work would be needed to do this?
use a hook to set it. In your conftest.py, put something like
that's an alternative way to do it, although it introduces some "magic" (the tester has to know the YAML file isn't exhaustive and there's additional configuration that is being done but I can put this in the documentation) - just wondering if a more explicit method like in above is possible (even if additional effort/work would be needed to do this)?
Another question: Why did you name the hook pytest_tavern_beta_before_every_test_run - especially why the beta in the hook name?
You should be able to use the include directive like this:
fastapi:
!include fastapi-settings.yaml
and have this in the fastapi-settings.yaml file
app:
location: "tests.fastapi_test_app"
I've tested this with the mqtt settings and it works so it should work for you too.