question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to pass the arguments to fixtures

See original GitHub issue

Below 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:closed
  • Created 5 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
michaelboultoncommented, Mar 27, 2019

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)

test_name: endpoint 1 test

marks:
  - endpoint_1
  - usefixtures:
       - read_uuid
...
---
test_name: endpoint 2 test

marks:
  - endpoint_2
  - usefixtures:
       - read_uuid
...

Looks for this mark in your fixture

@pytest.fixture
def read_uuid(request):  # 'request' is a built in pytest fixture
  marks = request.node.own_markers
  mark_names = [m.name for m in marks]
  if "endpoint_1" in mark_names:
    return "uuid1"
  elif "endpoint_2" in mark_names:
    return "uuid2"
0reactions
MonRescommented, Apr 10, 2019

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?

Read more comments on GitHub >

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

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found