tavern icon indicating copy to clipboard operation
tavern copied to clipboard

[Question] Common objects at the root of test cases

Open gc-ss opened this issue 3 years ago • 3 comments

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

gc-ss avatar Apr 24 '22 03:04 gc-ss

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...

michaelboulton avatar May 02 '22 15:05 michaelboulton

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

  1. Is there already an ability to do this?
  2. If not, what additional effort/work would be needed to do this?
  3. For example: can !include be 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?

gc-ss avatar May 02 '22 15:05 gc-ss

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.

michaelboulton avatar May 08 '22 14:05 michaelboulton