tavern icon indicating copy to clipboard operation
tavern copied to clipboard

Ability to write variables in test file

Open ashishpatel1992 opened this issue 3 years ago • 5 comments

Looking forward for something like where users are able to declare their own variables inside tavern file. I know there is a way to use !include and declare but I want it to be specific to this test case file.

test_name: Sample Test Name
variables:
  common_url: "https://sample.com/{group_endpoint}"

stages:
  - name: List specific group with id
    request:
        url: "{common_url}/id/abc1"
        method: GET
    response:
        status_code: 200

  - name: List specific group with id2
    request:
        url: "{common_url}/id/abc2"
        method: GET
    response:
        status_code: 200

ashishpatel1992 avatar Dec 14 '21 13:12 ashishpatel1992

You can do something like by doing

test_name: test name

includes:
  - name: variables
    description: variables
    variables:
      a: b

but it's not very nice. I may just remove the need to put the 'name' and 'description' fields in a configuration file (or at least the description) as this is often not needed

michaelboulton avatar Dec 20 '21 16:12 michaelboulton

Umm.. Looks good solution but the only thing that makes me worried is I have common.yaml included in pytest.ini file, I am not able to use variables from it in local variables.

Example: I have global domain_name, port variables in common.yaml Now I want to build url in local variable

test_name: test name

includes:
  - name: variables
    description: variables
    variables:
      list_all_users: "https://{domain_name}:{port}/users/"
      select_user: "https://{domain_name}:{port}/users/55"

Now I want to use list_all_users at multiple places

ashishpatel1992 avatar Dec 24 '21 07:12 ashishpatel1992

Ah sorry, I didn't realise you meant inside one file and not just in one test.

Each test in Tavern is a separate YAML document and are all basically treated as separate files so you can't do something like that. Do you want something like:

variables:
      list_all_users: "https://{domain_name}:{port}/users/"
      select_user: "https://{domain_name}:{port}/users/55"
---
test_name: test 1
stages:
	...
---
test_name: test 2
stages:
	...

?

michaelboulton avatar Jan 08 '22 17:01 michaelboulton

@michaelboulton Have similar interest and what you described is the solution I would hope for?

jaywon avatar Jan 10 '22 06:01 jaywon

What I mean is I have a common.yaml file which contains some variables like domain name and port number. But now I want to add more variable specific to the test only and use some variables from common.yaml to build new variables specific for the current test file.

Ah sorry, I didn't realise you meant inside one file and not just in one test.

Each test in Tavern is a separate YAML document and are all basically treated as separate files so you can't do something like that. Do you want something like:

variables:
      list_all_users: "https://{domain_name}:{port}/users/"
      select_user: "https://{domain_name}:{port}/users/55"
---
test_name: test 1
stages:
	...
---
test_name: test 2
stages:
	...

?

ashishpatel1992 avatar Jan 10 '22 06:01 ashishpatel1992