Skip to content
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

[Question] Common objects at the root of test cases #774

Open
gc-ss opened this issue Apr 24, 2022 · 3 comments
Open

[Question] Common objects at the root of test cases #774

gc-ss opened this issue Apr 24, 2022 · 3 comments

Comments

@gc-ss
Copy link

gc-ss commented Apr 24, 2022

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
@michaelboulton
Copy link
Member

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

paho-mqtt: &mqtt_spec
# tls:
# enable: true
connect:
host: localhost
port: 9001
timeout: 3
client:
transport: websockets
client_id: tavern-tester
, 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...

@gc-ss
Copy link
Author

gc-ss commented May 2, 2022

Right, but is there a way to refactor

paho-mqtt: &mqtt_spec
# tls:
# enable: true
connect:
host: localhost
port: 9001
timeout: 3
client:
transport: websockets
client_id: tavern-tester
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?

@michaelboulton
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants