How to pass the arguments to fixtures
See original GitHub issueBelow are my sample YAML and contest.py files. But can i pass argument to fixtures? So that based on argument value we can write logic inside fixture? if No what is the alternate work-around?
Note: Currently for each new senario i need to write new fixture function in contest.py file.
Used platform:
'tavern': '0.22.1',Python 3.6.7, pytest-4.2.0, py-1.7.0, pluggy-0.8.1
YAML FILE :
---
test_name: "REST Association"
marks:
- usefixtures:
- uuid_generator
stages:
- name: "Associating data for base class"
request:
headers:
content-type: application/json
json:
state: HI
serviceHAS:
- "{protocol:s}://{host}:{port}/api/{endpoint:s}/{uuid_generator}"
method: POST
url: "{protocol:s}://{host}:{port}/api/{subsc:s}"
response:
----
----
(UIV-Rest) [root@mdffun09 UIV-Rest]# cat conftest.py
# conftest.py
import pytest,json,os
@pytest.fixture
def uuid_generator():
dest=os.getcwd()+"/inputjson/services.json"
with open(dest, "r") as pfile:
array=json.load(pfile)
print(array)
print(array["id"])
return array["id"]
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
Pass a parameter to a fixture function - Stack Overflow
I tried using this solution but was having issues passing multiple parameters or using variable names other than request. I ended up using...
Read more >How to parametrize fixtures and test functions - Pytest
pytest.fixture() allows one to parametrize fixture functions. @pytest.mark.parametrize ... Parameter values are passed as-is to tests (no copy whatsoever).
Read more >Add a more obvious way to pass parameters to fixture functions
To pass parameters to another fixture, stack arguments() decorators: @cat. arguments("Mittens") @dog.
Read more >Pass Params to Pytest Fixture - Jake Trent
Let's try the pytest way. pytest has its own way to set up and tear down fixtures: import pytest @pytest.fixture ...
Read more >Adding Function Arguments to pytest Fixtures - Siv Scripts
At the end, we've defined a fixture that returns a reference to a function. Each test includes the fixture (in order to get...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
What you’re trying to do is not possible, but there is a way of getting around it:
Define an extra mark on your tests (eg,
endpoint_1
)Looks for this mark in your fixture
I’ve got one more question… in my repository I have a rather extensive structure, I use many conftests and even more fixtures… Some of this fixtures need to set autouse to True… but when I try to run tavern test this autouse ones cause an error: "AssertionError: “YamlItem do not have ‘module’ object” or ‘cls object’. Could I skip this fixtures running tavern tests? Maybe using skipif or marks / skipfixtures?